簡體   English   中英

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

[英]String or binary data would be truncated.?

我正在使用C#Web應用程序構建輸入表單。 我可以提交它,但我得到System.Data.SqlClient.SqlException (0x80131904): String or binary data would be truncated錯誤。 我知道這是一個長度問題,但我已經仔細檢查了一切,但似乎無法在這里解決這個問題。

{
    // Open Connection
    thisConnection.Open();

    // Create INSERT statement with named parameters
    nonqueryCommand.CommandText = "INSERT  INTO InventoryInput (Date, Item, Qty, WStatus) VALUES (@Date, @Qty, @Item, @WStatus)";


    // Add Parameters to Command Parameters collection
    nonqueryCommand.Parameters.Add("@Date", System.Data.SqlDbType.DateTime);
    nonqueryCommand.Parameters.Add("@Item", System.Data.SqlDbType.VarChar, 255);
    nonqueryCommand.Parameters.Add("@QTY", System.Data.SqlDbType.NChar, 10);
    nonqueryCommand.Parameters.Add("@WStatus", System.Data.SqlDbType.VarChar, 50);


    nonqueryCommand.Parameters["@Date"].Value = TextBox1.Text;
    nonqueryCommand.Parameters["@Item"].Value = DropDownList1.Text;
    nonqueryCommand.Parameters["@QTY"].Value = TextBox2.Text;
    nonqueryCommand.Parameters["@WStatus"].Value = DropDownList3.Text;
    nonqueryCommand.ExecuteNonQuery();

}

在此輸入圖像描述

你混淆了兩個列:

(Date,  Item, Qty,   WStatus) VALUES
(@Date, @Qty, @Item, @WStatus)

你試圖將你的@Qty插入Item列(好吧),並將@Item插入Qty列(可能是錯誤的)。

我也同意一些數據類型仍然可疑的評論 - 例如非數字數量。

使用Substring()確保輸入不會太長:

nonqueryCommand.Parameters["@Date"].Value = TextBox1.Text;
nonqueryCommand.Parameters["@Item"].Value = DropDownList1.Text.Substring(0, 255);
nonqueryCommand.Parameters["@QTY"].Value = TextBox2.Text.Substring(0, 10);
nonqueryCommand.Parameters["@WStatus"].Value = DropDownList3.Text.Substring(0, 50);

暫無
暫無

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

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