【发布时间】: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