如何连接数据库并创建事物 ~~~急 在线等
如何连接数据库并创建事物,请给出代码
------解决方案--------------------SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[ "配置字段 "].ConnectionString);
conn.Open();
SqlCommand comm = new SqlCommand();
SqlTransaction Mytr;
Mytr = conn.BeginTransaction();
comm.Connection=conn;
comm.Transaction = Mytr;
try
{
comm.CommandText = "UPDATE a SET a=1 ";
comm.ExecuteNonQuery;
Mytr.Commit();
Response.Write( "事务执行成功! ");
}
catch (exception ex)
{
Mytr.Rollback();
Response.Write( "错误,已回滚! ");
}
finally
{
conn.Close();
}