首页 > 其他 > 详细

C# 事务

时间:2014-08-05 18:41:19      阅读:352      评论:0      收藏:0      [点我收藏+]

SqlConnection conn = new SqlConnection(@"Server=.\SQL2008;database=Duties;UID=sa;Pwd=sa.");
SqlCommand cmd=new SqlCommand();
cmd.Connection=conn;
try
{
conn.Open();
SqlTransaction objTran = conn.BeginTransaction();
try
{
cmd.Transaction = objTran;
cmd.CommandText = "insert into dbo.Purchase values(‘" + txtPurchase.Text + "‘,‘" + DateTime.Parse(txtDate.Text) + "‘)";
int i = cmd.ExecuteNonQuery();

for (int j = 0; j < dataGridView1.Rows.Count; j++)
{
string sqltext = "insert into dbo.Article values(‘"+txtPurchase.Text+"‘,‘" + dataGridView1.Rows[j].Cells[0].Value.ToString() + "‘," + int.Parse(dataGridView1.Rows[j].Cells[1].Value.ToString()) + "," + float.Parse(dataGridView1.Rows[j].Cells[2].Value.ToString()) + ")";
cmd.CommandText = sqltext;
i += cmd.ExecuteNonQuery();
}

objTran.Commit();
}
catch (Exception ex)
{
objTran.Rollback();
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}

C# 事务,布布扣,bubuko.com

C# 事务

原文:http://www.cnblogs.com/ChineseMoonGod/p/3892501.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!