当传送具有新行的 DataRow 集合时,更新要求有效的 InsertCommand
当传递具有新行的 DataRow 集合时,更新要求有效的 InsertCommand。
SqlConnection con = GetCon();
if (con.State.Equals(ConnectionState.Closed))
{
con.Open();
}
SqlDataAdapter myAdapter = new SqlDataAdapter();
SqlCommand myCommand = new SqlCommand("select * from " +table, con);
myAdapter.SelectCommand = myCommand;
SqlTransaction myTrans = con.BeginTransaction();//添加一个事务
myCommand.Transaction = myTrans;
SqlCommandBuilder SCB = new SqlCommandBuilder(myAdapter);
try
{
da.Update(ds, table);
ds.AcceptChanges();
myTrans.Commit(); //事务正常提交
return true;
}
catch (SqlException ex)
{
try
{
myTrans.Rollback(); //事务回滚
throw new Exception("SQL数据库连接失败,操作数据库产生错误: " + ex.Message);
}
catch (SqlException ey) //回滚失败
{
if (myTrans.Connection != null)
{
throw new Exception("数据回滚事务遇到一个异常类型 : " + ey.GetType());
}
}
return false;
}
finally
{
con.Close();
}
SqlConnection con = GetCon();
if (con.State.Equals(ConnectionState.Closed))
{
con.Open();
}
SqlDataAdapter myAdapter = new SqlDataAdapter();
SqlCommand myCommand = new SqlCommand("select * from " +table, con);
myAdapter.SelectCommand = myCommand;
SqlTransaction myTrans = con.BeginTransaction();//添加一个事务
myCommand.Transaction = myTrans;
SqlCommandBuilder SCB = new SqlCommandBuilder(myAdapter);
try
{
da.Update(ds, table);
ds.AcceptChanges();
myTrans.Commit(); //事务正常提交
return true;
}
catch (SqlException ex)
{
try
{
myTrans.Rollback(); //事务回滚
throw new Exception("SQL数据库连接失败,操作数据库产生错误: " + ex.Message);
}
catch (SqlException ey) //回滚失败
{
if (myTrans.Connection != null)
{
throw new Exception("数据回滚事务遇到一个异常类型 : " + ey.GetType());
}
}
return false;
}
finally
{
con.Close();
}