c# - 字符串或二进制数据将被截断。?

标签 c# sql-server

我正在 C# 网络应用程序中构建输入表单。我可以很好地提交它,但我收到了 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();

}

enter image description here

最佳答案

您混淆了两个列:

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

您正在尝试将 @Qty 插入到 Item 列(好的),并将 @Item 插入到 Qty 列(可能是错误的)。

我也同意一些数据类型看起来仍然可疑的评论——例如一个非数字数量。

关于c# - 字符串或二进制数据将被截断。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12037875/

相关文章:

Sql Sum 函数每次运行查询时给出不同的数据

c# - 在 .getJSON 中传递数据。可能的?如何?

c# - 如何正确使用带参数的 Ninject 的 NamedLikeFactoryMethod?

c# - Database.ExecuteSqlCommand 的返回值是什么?

c# - 从 sql server 数据库中检索数据的正确代码

sql-server - 使用 Azure Data Studio 编写数据库脚本

c# - 无法还原 ASP.NET Core 1.0 RTM 解决方案

c# - Lock 关键字不能按预期使用字符串

c# - 如何为 Web 窗体项目中长时间运行的 Ajax 请求禁用/释放早期 session 状态阻塞

sql - 使用 "Native"SQL 连接到数据库是什么意思?