SqlCommandBuilder.GetInsertCommand()
System.InvalidOperationException:'当分配给命令的连接处于挂起的本地事务中时,ExecuteReader 要求该命令具有事务。命令的 Transaction 属性尚未初始化。'我在 SqlCommandBuilder.GetInsertCommand() 之前调用 SqlConnection.BeginTrans()
SqlTransaciton
分配给
SqlDataAdapter.SelectCommand
解决了这个问题
using (SqlCommandBuilder builder = new SqlCommandBuilder(_adapter))
{
_adapter.SelectCommand.Transaction = Database.transaction; // This fix problem
_adapter.InsertCommand = builder.GetInsertCommand(); // No exception now
_adapter.UpdateCommand = builder.GetUpdateCommand(); // No exception now
_adapter.DeleteCommand = builder.GetDeleteCommand(); // No exception now
_adapter.InsertCommand.Transaction = Database.transaction;
_adapter.UpdateCommand.Transaction = Database.transaction;
_adapter.DeleteCommand.Transaction = Database.transaction;
_adapter.Update(this);
}