SqlDataAdapter更新错误原因
SqlDataAdapter update error cause
我有这样的代码:
DataTable dataTable;
// filling table with data from db, closing connection
// modifying table
// trying to save it
// "some_table" is the same table data was loaded from
var connection = new SqlConnection(some_connection_string);
var dataAdapter = new SqlDataAdapter("select * from some_table", connection);
var cbMar = new SqlCommandBuilder(dataAdapter);
SqlTransaction transaction = null;
dataAdapter.InsertCommand = cbMar.GetInsertCommand();
dataAdapter.UpdateCommand = cbMar.GetUpdateCommand(true);
dataAdapter.DeleteCommand = cbMar.GetDeleteCommand(true);
transaction = connection.BeginTransaction();
dataAdapter.InsertCommand.Transaction = transaction;
dataAdapter.UpdateCommand.Transaction = transaction;
dataAdapter.DeleteCommand.Transaction = transaction;
dataAdapter.Update(dataTable);
这里,在 Update
,我遇到了一个例外:
Failed to convert parameter value from a String to a Int32.
System.Exception {System.FormatException}
InnerException:
Input string was not in a correct format.
System.FormatException
据我了解,这是因为我将字符串值设置为整数数据库列。
问题:如何准确找到导致此异常的列和行?
我能够为 System.Data.dll 构建 .pdb 文件并可以进入它,但是当我尝试添加来自 System.Data.dll 的任何变量进行观察时,我收到一个错误提示被优化掉了。据我了解,System.Data.dll 没有调试版本。
正如上面的评论中所解释的,问题可能是由您的网格中的一个单元格引起的,该单元格应该包含一个整数值,但实际上不是。
为了帮助您找到哪一行包含无效单元格,您可以使用如下代码将 RowUpdated 的事件处理程序添加到您的 SqlDataAdapter
....
dataAdapter.RowUpdated += onUpdate;
....
private void onUpdate(object sender, SqlRowUpdatedEventArgs e)
{
Console.WriteLine(e.Row[0]);
if (e.Errors != null)
{
Console.WriteLine(e.Errors.Message);
Console.WriteLine("At row with key=" + e.Row["somecolumntoidentifytherow"]);
}
}
调试时,您可以在 if 块内放置一个断点,然后查看包含无效值的 e.Row 对象。 (现在 itemarray 中仍有 150 列可供查看)。您确定呈现此数据墙对您的用户有用吗?
我有这样的代码:
DataTable dataTable;
// filling table with data from db, closing connection
// modifying table
// trying to save it
// "some_table" is the same table data was loaded from
var connection = new SqlConnection(some_connection_string);
var dataAdapter = new SqlDataAdapter("select * from some_table", connection);
var cbMar = new SqlCommandBuilder(dataAdapter);
SqlTransaction transaction = null;
dataAdapter.InsertCommand = cbMar.GetInsertCommand();
dataAdapter.UpdateCommand = cbMar.GetUpdateCommand(true);
dataAdapter.DeleteCommand = cbMar.GetDeleteCommand(true);
transaction = connection.BeginTransaction();
dataAdapter.InsertCommand.Transaction = transaction;
dataAdapter.UpdateCommand.Transaction = transaction;
dataAdapter.DeleteCommand.Transaction = transaction;
dataAdapter.Update(dataTable);
这里,在 Update
,我遇到了一个例外:
Failed to convert parameter value from a String to a Int32.
System.Exception {System.FormatException}InnerException:
Input string was not in a correct format.
System.FormatException
据我了解,这是因为我将字符串值设置为整数数据库列。
问题:如何准确找到导致此异常的列和行?
我能够为 System.Data.dll 构建 .pdb 文件并可以进入它,但是当我尝试添加来自 System.Data.dll 的任何变量进行观察时,我收到一个错误提示被优化掉了。据我了解,System.Data.dll 没有调试版本。
正如上面的评论中所解释的,问题可能是由您的网格中的一个单元格引起的,该单元格应该包含一个整数值,但实际上不是。
为了帮助您找到哪一行包含无效单元格,您可以使用如下代码将 RowUpdated 的事件处理程序添加到您的 SqlDataAdapter
....
dataAdapter.RowUpdated += onUpdate;
....
private void onUpdate(object sender, SqlRowUpdatedEventArgs e)
{
Console.WriteLine(e.Row[0]);
if (e.Errors != null)
{
Console.WriteLine(e.Errors.Message);
Console.WriteLine("At row with key=" + e.Row["somecolumntoidentifytherow"]);
}
}
调试时,您可以在 if 块内放置一个断点,然后查看包含无效值的 e.Row 对象。 (现在 itemarray 中仍有 150 列可供查看)。您确定呈现此数据墙对您的用户有用吗?