加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 08:21:44 所属栏目:百科 来源:网络整理
导读:我收到了错误 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 MyDat
我收到了错误

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读