C#中事务

来源:互联网 发布:淘宝龙泉剑哪家最好 编辑:程序博客网 时间:2024/05/22 02:05

dbconn的定义略
try
{
    dbconn.Open();

   
foreach(GridViewRow row in GridView.Rows)
    {
       
string userID = row.Cells[0].Text;
       
string userName = row.Cells[1].Text;
       
string sqlStr = "INSERT INTO user(userid, userName) VALUES('" + userID + "', '" + userName + "');
        DbCommand dbCommand = db.GetSqlStringCommand(sqlStr);
        db.ExecuteNonQuery(dbCommand);
    }

    dbtran.Commit();
}
catch
{   
    dbtran.Rollback();
}
finally
{
    dbconn.Close();
}