c#& SQL Server Insert executenOnquery:哪个列会截断“字符串或二进制数据”?
我正在尝试将其插入C#程序中的表格。我有此插入命令:
var insertSql = @"INSERT INTO dbo.[Case]
VALUES (@Id, @IsDeleted, @CaseNumber, @ContactId, @AccountId, @ParentId, @SuppliedName, @SuppliedEmail, @SuppliedPhone, @SuppliedCompany, @Type, @RecordTypeId, @Status, @Reason, @Origin...
然后我有很多行添加了类似的参数:
var command = new SqlCommand(insertSql, easySoftConn);
if (case2.Id != null)
command.Parameters.AddWithValue("@Id", case2.Id);
else
command.Parameters.AddWithValue("@Id", DBNull.Value);
if (case2.IsDeleted != null)
{
if (case2.IsDeleted == "true")
command.Parameters.AddWithValue("@IsDeleted", 1);
else
command.Parameters.AddWithValue("@IsDeleted", 0);
}
else
command.Parameters.AddWithValue("@IsDeleted", DBNull.Value);
if (case2.CaseNumber != null)
command.Parameters.AddWithValue("@CaseNumber", case2.CaseNumber);
else
command.Parameters.AddWithValue("@CaseNumber", DBNull.Value);
if (case2.ContactId != null)
command.Parameters.AddWithValue("@ContactId", case2.ContactId);
else
command.Parameters.AddWithValue("@ContactId", DBNull.Value);
...
当我最终执行插入物时:
try
{
command.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException e)
{
CLog.Write(e.Message.ToString(), CLog.ErrLvl.Error);...
}
我得到错误:
字符串或二进制数据将被截断
我的问题是,错误不会告诉我哪个列将被截断。我已经插入了80列,我宁愿一对一地浏览它们。有没有办法获取错误处理来确切告诉我哪个字段抛出了错误?
编辑:我在日志文件中有一个完整的堆栈跟踪,但它仍然没有告诉我哪个列,我只是将其缩短到这里的实际错误。
I'm trying to insert into a table in a C# program. I have this insert command:
var insertSql = @"INSERT INTO dbo.[Case]
VALUES (@Id, @IsDeleted, @CaseNumber, @ContactId, @AccountId, @ParentId, @SuppliedName, @SuppliedEmail, @SuppliedPhone, @SuppliedCompany, @Type, @RecordTypeId, @Status, @Reason, @Origin...
And then I've got many lines adding in the parameters like so:
var command = new SqlCommand(insertSql, easySoftConn);
if (case2.Id != null)
command.Parameters.AddWithValue("@Id", case2.Id);
else
command.Parameters.AddWithValue("@Id", DBNull.Value);
if (case2.IsDeleted != null)
{
if (case2.IsDeleted == "true")
command.Parameters.AddWithValue("@IsDeleted", 1);
else
command.Parameters.AddWithValue("@IsDeleted", 0);
}
else
command.Parameters.AddWithValue("@IsDeleted", DBNull.Value);
if (case2.CaseNumber != null)
command.Parameters.AddWithValue("@CaseNumber", case2.CaseNumber);
else
command.Parameters.AddWithValue("@CaseNumber", DBNull.Value);
if (case2.ContactId != null)
command.Parameters.AddWithValue("@ContactId", case2.ContactId);
else
command.Parameters.AddWithValue("@ContactId", DBNull.Value);
...
When I finally execute the insert:
try
{
command.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException e)
{
CLog.Write(e.Message.ToString(), CLog.ErrLvl.Error);...
}
I get the error:
String or binary data would be truncated
My issue is, the error doesn't tell me which column would be truncated. I've got 80 columns I'm inserting into, and I'd rather not go through them one-by-one. Is there a way to get the error handling to tell me exactly which field is throwing the error?
EDIT: I have a full stack trace in my log file but it still doesn't tell me which column, I just shortened it to the actual error here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
切换到使用强键入的数据访问将更快地关注:
的查询>
select * select *从[case] < /code>
数据访问代码看起来像:
它也可以节省您的时间,编写无聊的数据访问代码
Switching to using strongly typed data access would head this one off sooner:
SELECT * FROM [Case]
Data access code looks like:
So it'll save you a boatload of time writing boring data access code too
Depending on your SQL version you can apply a KB to get this to show more data as stated here -
它有效地开始显示以下消息
这来自这篇很棒的帖子链接它可以进一步解释您可以尝试在不可能的情况下尝试搜索列。该帖子还讨论了如何进行手动搜索,尽管我想象列列表是否太大,可能是您想要避免的。
Depending on your SQL version you can apply a KB to get this to show more data as stated here - Link
It effectively starts to show messages like the following
This came from this great post Link which goes much further to explain how you can try and search for the column should this not be possible. The post also talks about how you can do manual searching although I'd imagine if the list of columns is too large that may be something you want to avoid.
看来一个或多个参数的值的长度比表单元包所包含的要多。
您应该查看表列定义。
Looks like the value of one or more of your parameters has more length than the table cell can contain.
You should to look at table column definitions.