【问题标题】:please fix "String or binary data would be truncated.\r\nThe statement has been terminated"请修复“字符串或二进制数据将被截断。\r\n语句已终止”
【发布时间】:2016-04-24 06:09:33
【问题描述】:

我想使用 C# WPF 在我的数据库中找到旧坐标并将其替换为新坐标。我收到了这个错误

字符串或二进制数据将被截断。\r\n语句已被 终止

我找不到问题所在。这是我使用的方法:

 public void updateEvent(string oldCord,string newCord, DateTime dropDate)
    {

        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "UPDATE Event SET Date = @newDate ,Cordinate=@newCord  WHERE  Cordinate = @oldCord";
            cmd.Parameters.AddWithValue("@newDate", dropDate);
            cmd.Parameters.AddWithValue("@newCord", newCord);
            cmd.Parameters.AddWithValue("@oldCord", oldCord);             
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.Connection = connection;
            cmd.ExecuteNonQuery();
        }
    }

【问题讨论】:

  • 抛出此异常是因为您尝试将更多数据写入表的字段中,而不是适合该字段。就像在 varchar(50) 字段中包含 100 个字符的 string。所以问题是,字段Cordinate(可能应该命名为Coordinate)到底是如何定义的,你尝试写入的newCord到底有多长?
  • 您应该查看Can we stop using AddWithValue() already? 并停止使用.AddWithValue() - 它可能会导致意想不到和令人惊讶的结果...
  • 这篇文章'stackoverflow.com/questions/17312558/…'怎么样?
  • 我们需要查看表声明才能知道列值的长度。

标签: c# sql-server ado.net


【解决方案1】:

一般来说,这个问题的原因是你发送给存储过程的参数长度大于数据库中实际表中字段的大小。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 2012-01-31
    • 2018-03-28
    • 1970-01-01
    • 2013-03-19
    相关资源
    最近更新 更多