我收到了错误
String or binary data would be truncated. The data for table-valued parameter doesn’t conform to the table type of the parameter.The statement has been terminated.
@H_404_5@存储过程是:
CreatePROCEDURE [dbo].[addquestion] @dt as MyDataTable readonly AS BEGIN insert into questiontbl(Question) select(Question) from @dt; END该表是:
CREATE TABLE [dbo].[questiontbl] ( [checkval] [varchar](max) NULL,[Question] [varchar](max) NULL )C#代码:
con.Close(); con.Open(); DataTable sqa = Session["questionlist"] as DataTable; sqlParameter tvparam = cmd.Parameters.AddWithValue("@dt",sqa); tvparam.sqlDbType = sqlDbType.Structured; cmd.ExecuteNonQuery();Cmd.ExecuteNonQuery()返回提到的错误.我匹配了数据类型 – 它在类型和表中也是varchar(max).
解决方法
我已经提到了许多网址,但没有得到适当的解决方案.
The main reason for this issue is,we are not passing the data in the
@H_404_5@
specified length但是在我们的实际代码中,我们将发送有效数据,但该值将不会通过并将通过上述问题.
这里的诀窍是,
While creating data table for the table valued parameter,we need to
@H_404_5@
create the column in the order we created in the table valued
parameter.请检查以下代码.
解决方案(以下将有效)
C#
DataTable users= new DataTable("Users"); users.Columns.Add("EmailAddress",typeof(string)); users.Columns.Add("Content",typeof(string)); DataTable data= users.NewRow(); data["EmailAddress"] = emailAddress; data["Content"] = content;CREATE TYPE [dbo].[ParamEulaEmailUser] AS TABLE( [EmailAddress] [nvarchar](50) NOT NULL,[Content] [nvarchar](max) NULL )以下方法无效
C#
DataTable users= new DataTable("Users"); users.Columns.Add("Content",typeof(string)); users.Columns.Add("EmailAddress",typeof(string));原因是在我们向存储过程发送数据时,表值参数采用给定顺序的值并与顺序中的现有列匹配.因此,将使用存储过程中的电子邮件地址检查内容并抛出以下错误
错误:字符串或二进制数据将被截断.表值参数的数据不符合参数的表类型