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();
}