在程序中有事务控制流程的走向

SqlConnection conn = null;
SqlTransaction trans
= null;
try
{
  conn
= new SqlConnection("");//连库,略
  SqlCommand cmd = new SqlCommand("INSERT INTO test(title) VALUES ('"+txtTitle.Text+"')",conn);
  trans
= conn.BeginTransaction();
  cmd.Transaction
= trans;
 
int rows = cmd.ExecuteNonquery();
 
if(rows > 0)
  {
    trans.Commit();
    MessageBox.Show(
"OK","OK");
  }
}
catch (Exception e)
{
  trans.Rollback();
  MessageBox.Show(
"Error","Error")
}
finally
{
 
if(conn != null){conn.Close();}
}
阅读更多
个人分类: C#.net winfrom
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭