簡體   English   中英

字符串或二進制數據將被截斷錯誤消息

[英]string or binary data would be truncated error message

我收到以下錯誤消息:

字符串或二進制數據將被截斷

我已經嘗試增加列大小,但沒有運氣,我已經加倍檢查代碼,但似乎無法找到任何問題。 它在插入過程中:

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個字符,如果你想插入更多字符,請檢查自己。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM