c# – 错误:字符串或二进制数据将被截断.表值参数的数据不符合参数的表类型

前端之家收集整理的这篇文章主要介绍了c# – 错误:字符串或二进制数据将被截断.表值参数的数据不符合参数的表类型前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我收到了错误

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 
)

C#代码:@H_301_4@

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@

请检查以下代码.@H_301_4@

解决方案(以下将有效)@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;

sql@H_301_4@

CREATE TYPE [dbo].[ParamEulaEmailUser] AS TABLE(
    [EmailAddress] [nvarchar](50) NOT NULL,[Content] [nvarchar](max) NULL
)

以下方法无效@H_301_4@

C#@H_301_4@

DataTable users= new DataTable("Users");
users.Columns.Add("Content",typeof(string));
users.Columns.Add("EmailAddress",typeof(string));

原因是在我们向存储过程发送数据时,表值参数采用给定顺序的值并与顺序中的现有列匹配.因此,将使用存储过程中的电子邮件地址检查内容并抛出以下错误@H_301_4@

错误:字符串或二进制数据将被截断.表值参数的数据不符合参数的表类型@H_301_4@

猜你在找的C#相关文章