请问谁能给我写一个带事务的底层代码。ASP.NET的,在VS2008中调用的,不是在SQL存储过程中使用事务的。
展开全部
private void btn_submit_Click(object sender, System.EventArgs e)
{
string strconn = ConfigurationSettings.AppSettings["dsn"];
SqlConnection cnn = new SqlConnection(strconn);
SqlCommand cmd = new SqlCommand();
SqlTransaction transaction = null;
try
{
cnn.Open();
// 先插入分店shop表,再插入经理Manager表,并将其作为一个事务进行处理
transaction = cnn.BeginTransaction();
cmd.Transaction = transaction;
cmd.Connection = cnn;
// 插入分店shop表
string shopstr = "insert into shop values('" + tbx_shopid.Text + "','" + tbx_shopname.Text + "','" + tbx_shopaddress.Text + "','" + tbx_shopphone.Text + "')";
cmd.CommandType = CommandType.Text;
cmd.CommandText = shopstr;
cmd.ExecuteNonQuery();
// 插入经理Manager表
string managerstr = "insert into manager values('" + tbx_managerid.Text + "','" + tbx_managerpassword.Text + "','" + tbx_managername.Text + "','" + tbx_shopid.Text + "')";
cmd.CommandType = CommandType.Text;
cmd.CommandText = managerstr;
cmd.ExecuteNonQuery();
// 提交事务
transaction.Commit();
lbl_msg.Text = "添加分店操作成功";
}
catch(Exception ex)
{
lbl_msg.Text = "添加分店操作失败";
transaction.Rollback();
}
finally
{
cnn.Close();
}
}
{
string strconn = ConfigurationSettings.AppSettings["dsn"];
SqlConnection cnn = new SqlConnection(strconn);
SqlCommand cmd = new SqlCommand();
SqlTransaction transaction = null;
try
{
cnn.Open();
// 先插入分店shop表,再插入经理Manager表,并将其作为一个事务进行处理
transaction = cnn.BeginTransaction();
cmd.Transaction = transaction;
cmd.Connection = cnn;
// 插入分店shop表
string shopstr = "insert into shop values('" + tbx_shopid.Text + "','" + tbx_shopname.Text + "','" + tbx_shopaddress.Text + "','" + tbx_shopphone.Text + "')";
cmd.CommandType = CommandType.Text;
cmd.CommandText = shopstr;
cmd.ExecuteNonQuery();
// 插入经理Manager表
string managerstr = "insert into manager values('" + tbx_managerid.Text + "','" + tbx_managerpassword.Text + "','" + tbx_managername.Text + "','" + tbx_shopid.Text + "')";
cmd.CommandType = CommandType.Text;
cmd.CommandText = managerstr;
cmd.ExecuteNonQuery();
// 提交事务
transaction.Commit();
lbl_msg.Text = "添加分店操作成功";
}
catch(Exception ex)
{
lbl_msg.Text = "添加分店操作失败";
transaction.Rollback();
}
finally
{
cnn.Close();
}
}
展开全部
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=123;database=book");
con.Open();
String sql = "delete from books";
SqlTransaction trnas = con.BeginTransaction();
SqlCommand com = new SqlCommand(sql, con);
com.Transaction = trnas;
try
{
com.ExecuteNonQuery();
trnas.Commit();
con.Close();
Response.Write("哈哈");
}
catch
{
Response.Write("hehe");
trnas.Rollback();
}
}
{
SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=123;database=book");
con.Open();
String sql = "delete from books";
SqlTransaction trnas = con.BeginTransaction();
SqlCommand com = new SqlCommand(sql, con);
com.Transaction = trnas;
try
{
com.ExecuteNonQuery();
trnas.Commit();
con.Close();
Response.Write("哈哈");
}
catch
{
Response.Write("hehe");
trnas.Rollback();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
自己用的比较少。提个建议去这里看看都是介绍事务
http://blog.myspace.cn/e/405089794.htm一起学习
http://blog.myspace.cn/e/405089794.htm一起学习
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询