SqlConnection conn = new SqlConnection("Data Source = DATABASE-VPC\\SQL2005; User ID = sa; password = admin; Initial Catalog = Jiang; Pooling=true");
SqlTransaction mytran;//引用
conn.Open();
mytran = conn.BeginTransaction();//形如数据事务
try
{
SqlCommand cmd = new SqlCommand();
cmd.Transaction = mytran;
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into [member] values('admin')";
cmd.ExecuteNonQuery();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into [memberw] values('jiang')";
cmd.ExecuteNonQuery();
mytran.Commit();//提交数据库事务
conn.Close();
}
catch (Exception ex)
{
mytran.Rollback();//回滚(出错的时候)
}