码迷,mamicode.com
首页 > 其他好文 > 详细

C# 事务

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

标签:datagridview   os   io   for   art   ar   cti   new   

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# 事务

标签:datagridview   os   io   for   art   ar   cti   new   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!