字符串或二进制数据将被截断错误消息

本文关键字:错误 消息 二进制 数据 字符串 | 更新日期: 2023-09-27 18:01:59

我得到以下错误信息:

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

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

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

字符串或二进制数据将被截断错误消息

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

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

检查所有其他的,特别是小的,如Mobile

当你看到这个错误时

    string or binary data would be truncated error message

只要理解你正在尝试插入值到一个不能容纳你想插入的值的字段

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

它们只能容纳15个字符,如果你想插入更多的字符,请检查自己。