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.

存储过程是:

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
specified length

但是在我们的实际代码中,我们将发送有效数据,但该值将不会通过并将通过上述问题.

这里的诀窍是,

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.

请检查以下代码.

解决方案(以下将有效)

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;

sql

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));

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

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

相关文章

原文地址:http://msdn.microsoft.com/en-us/magazine/cc163...
前言 随着近些年微服务的流行,有越来越多的开发者和团队所采...
最近因为比较忙,好久没有写博客了,这篇主要给大家分享一下...
在多核CPU在今天和不久的将来,计算机将拥有更多的内核,Mic...
c语言输入成绩怎么判断等级
字符型数据在内存中的存储形式是什么