2014-05-06 56 views
4

我收到以下錯誤信息:字符串或二進制數據將被截斷錯誤消息

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

我試着增加柱子大小,但沒有運氣,我已經翻了一番代碼檢查,但似乎無法找到任何問題。

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

Customer類

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

嘿!歡迎來到StackOverflow。你能發佈你得到的錯誤消息嗎?如果可能的話,導致錯誤的'客戶'對象的一些示例值? –

+0

您可以發佈Customer對象中的值嗎? –

+0

我得到的錯誤是標題中的錯誤,它出現在ExecuteNonQuery中。客戶是類似於會話的類;客戶客戶=(客戶)會話[「訂單」]; - 客戶類別位於主文章 – user3607626

回答

6

顯示此消息時,您要插入一些數據是太大的領域。

這裏的主要候選人是Email - 您只將它設置爲15字符,而大多數電子郵件地址將會變得更大!將其增加到255並重試。

檢查所有其他人,特別是像Mobile這樣的小人。

+0

啊上帝謝謝,我剛剛錯誤的列設置錯誤的類型-.-謝謝大家 – user3607626

+1

,但在正面,你可以存儲一個瘋狂的長電話號碼;) –

5

每當你看到一個錯誤

string or binary data would be truncated error message 

才明白你正在試圖插入值到字段不能抱着你試圖插入

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

該值,他們只能容納15字符,檢查自己是否嘗試插入更多。

相關問題