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 + "','" + maid [J]. cells [0]. value. tostring () + "'," + Int. parse (maid [J]. cells [1]. value. tostring () + "," + float. parse (maid [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 ();
}