【问题标题】:string or binary data would be truncated error message字符串或二进制数据将被截断错误消息
【发布时间】:2014-06-22 21:19:51
【问题描述】:

我收到以下错误消息:

字符串或二进制数据将被截断

我尝试增加列大小但没有运气,我已经通过代码检查了两倍,但似乎找不到任何问题。在插入期间:

SqlCommand insert = new SqlCommand(@"INSERT 
into orderDetails 
(orderID, Name, Phone, Mobile, Email, DelName, DelRoad, DelTown, DelCity, DelCounty, DelPostCode, BilName, BilRoad, BilTown, BilCity, BilCounty, BilPostCode) 
values 
(@orderID , @Name , @Phone , @Mobile , @Email , @DelName , @DelRoad , @DelTown , @DelCity , @DelCounty , @DelPostCode , @BilName , @BilRoad , @BilTown , @BilCity , @BilCounty , @BilPostCode)", connection);
insert.Parameters.AddWithValue("@orderID", ID);
insert.Parameters.AddWithValue("@Name", name);
insert.Parameters.AddWithValue("@Phone", customer.Phone);
insert.Parameters.AddWithValue("@Mobile", customer.Mobile);
insert.Parameters.AddWithValue("@Email", customer.Email);
insert.Parameters.AddWithValue("@DelName", customer.DelName);
insert.Parameters.AddWithValue("@DelRoad", customer.DelRoad);
insert.Parameters.AddWithValue("@DelTown", customer.DelTown);
insert.Parameters.AddWithValue("@DelCity", customer.DelCity);
insert.Parameters.AddWithValue("@DelCounty", customer.DelCounty);
insert.Parameters.AddWithValue("@DelPostCode", customer.DelPostCode);
insert.Parameters.AddWithValue("@BilName", customer.BilName);
insert.Parameters.AddWithValue("@BilRoad", customer.BilRoad);
insert.Parameters.AddWithValue("@BilTown", customer.BilTown);
insert.Parameters.AddWithValue("@BilCity", customer.BilCity);
insert.Parameters.AddWithValue("@BilCounty", customer.BilCounty);
insert.Parameters.AddWithValue("@BilPostCode", customer.BilPostCode);
insert.ExecuteNonQuery();

这是我的表定义代码:

CREATE TABLE [dbo].[orderDetails] (
    [orderID]     INT         NOT NULL,
    [Name]        NCHAR (100) NULL,
    [Phone]       NCHAR (100) NULL,
    [Mobile]      NCHAR (15)  NULL,
    [Email]       NCHAR (15)  NULL,
    [DelName]     NCHAR (100) NULL,
    [DelRoad]     NCHAR (100) NULL,
    [DelTown]     NCHAR (100) NULL,
    [DelCity]     NCHAR (100) NULL,
    [DelCounty]   NCHAR (100) NULL,
    [DelPostCode] NCHAR (100) NULL,
    [BilName]     NCHAR (100) NULL,
    [BilRoad]     NCHAR (100) NULL,
    [BilTown]     NCHAR (100) NULL,
    [BilCity]     NCHAR (100) NULL,
    [BilCounty]   NCHAR (100) NULL,
    [BilPostCode] NCHAR (100) NULL,
    PRIMARY KEY CLUSTERED ([orderID] ASC)
);

客户类别

public class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Phone { get; set; }
    public string Mobile { get; set; }
    public string Email { get; set; }
    public string DelName { get; set; }
    public string DelRoad { get; set; }
    public string DelTown { get; set; }
    public string DelCity { get; set; }
    public string DelCounty { get; set; }
    public string DelPostCode { get; set; }
    public string BilName { get; set; }
    public string BilRoad { get; set; }
    public string BilTown { get; set; }
    public string BilCity { get; set; }
    public string BilCounty { get; set; }
    public string BilPostCode { get; set; }
    public bool sameasDel { get; set; }
}

【问题讨论】:

  • 你好!欢迎来到 StackOverflow。您能否发布您收到的错误消息,以及可能导致错误的 customer 对象的一些示例值?
  • 你可以在客户对象中发布值吗?
  • 我得到的错误是标题中的错误,它发生在 ExecuteNonQuery。 Customer 是一个像这样从会话中强制转换的类;客户 customer = (Customer)Session["order"]; - 客户类在主帖上
  • 增加Email列的大小并检查。
  • 感谢您的编辑。您能否尝试发布一些您分配给导致失败的 Customer 对象的 ?正如 Bharadwaj 所说,它很可能是您的 Email 专栏,但最好确定一下。

标签: c# sql sql-server


【解决方案1】:

每当您看到该错误时

    string or binary data would be truncated error message

只需了解您正在尝试将值插入到无法保存您尝试插入的值的字段中

    [Mobile]      NCHAR (15)  NULL,
[Email]       NCHAR (15)  NULL,

它们只能容纳 15 个字符,请检查自己是否要插入更多字符。

【讨论】:

    【解决方案2】:

    当您尝试插入一些对于该字段来说太大的数据时,会显示此消息。

    这里的主要候选人是Email - 您只需将其设置为15 字符,而大多数电子邮件地址会更大!将其增加到 255 并重试。

    也检查所有其他人,尤其是像Mobile 这样的小人。

    【讨论】:

    • 天哪,谢谢,我只是将错误的列设置为错误的类型-.- 谢谢大家
    猜你喜欢
    • 2015-04-29
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多