我收到了错误
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_301_4@
存储过程是:@H_301_4@
CreatePROCEDURE [dbo].[addquestion] @dt as MyDataTable readonly AS BEGIN insert into questiontbl(Question) select(Question) from @dt; END
该表是:@H_301_4@
CREATE TABLE [dbo].[questiontbl] ( [checkval] [varchar](max) NULL,[Question] [varchar](max) NULL )
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).@H_301_4@
解决方法
我已经提到了许多网址,但没有得到适当的解决方案.
The main reason for this issue is,we are not passing the data in the
specified length@H_301_4@
但是在我们的实际代码中,我们将发送有效数据,但该值将不会通过并将通过上述问题.@H_301_4@
这里的诀窍是,@H_301_4@
While creating data table for the table valued parameter,we need to
create the column in the order we created in the table valued
parameter.@H_301_4@
C#@H_301_4@
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#@H_301_4@
DataTable users= new DataTable("Users"); users.Columns.Add("Content",typeof(string)); users.Columns.Add("EmailAddress",typeof(string));
原因是在我们向存储过程发送数据时,表值参数采用给定顺序的值并与顺序中的现有列匹配.因此,将使用存储过程中的电子邮件地址检查内容并抛出以下错误@H_301_4@