C#
void InsertMultiBoardBarcodeRec Private ()
{// determine the list of data can not be empty .....
IF (dataGridView1.RowCount == 0)
{
MessageBox.Show ( "Print the list is empty, please contact the puzzle SN ...", " prompt window .... ");
return;
}
List<Models.MultiBoardBarCoeRec> listPrintLists = new List<Models.MultiBoardBarCoeRec>();
//获取列表数添加到listPrintLists集合....
//获取服务器时间
Models.GetSQLServerNowTime getSQLServerNowTime = new Models.GetSQLServerNowTime();
getSQLServerNowTime = DalGetSQLServerNowTime.SQLServerNowTime(getSQLServerNowTime);
string da = getSQLServerNowTime.NowTime;
foreach (DataGridViewRow dgr in dataGridView1.Rows)
{
if (dgr.Cells[4].Value == null)
{
break;
}
listPrintLists.Add(new Models.MultiBoardBarCoeRec {
WONO = dgr.Cells[5].Value.ToString(),
ProductGroupID = ProductGroupID,
FirstMBBC = dgr.Cells[4].Value.ToString(),
EndMBBC = dgr.Cells[4].Value.ToString(),
OutputDate = da,
WorkerID=texWhoPrint.Text.Trim(),
});
}
DalProductInfo.MultiBoardBarCoeRec(listPrintLists);
//bool tkdo = DalProductInfo.MultiBoardBarCoeRec(barCoeRec);
}
public BOOL MultiBoardBarCoeRec (List <Models.MultiBoardBarCoeRec> objBoardBarCoeRec) { // String [] = {AA "AA", "BB", "CC"}; // define a set of List for holding multiple SQL statements List < String > = sqlList new new List < String > (); // sqlList.Add (AA [0] .ToString ()); // sqlList.Add (AA [. 1] .ToString ()); for ( int I = 0 ; I <objBoardBarCoeRec.Count; I ++ ) { String sqlstr = "Insert MultiBoardBarCodeRec (WONO,ProductGroupID,FirstMBBC,EndMBBC,OutputDate,WorkerID) "; sqlStr += "Values('{0}','{1}','{2}','{3}','{4}','{5}')"; sqlStr = string.Format(sqlStr, objBoardBarCoeRec[i].WONO, objBoardBarCoeRec[i].ProductGroupID, objBoardBarCoeRec[i].FirstMBBC, objBoardBarCoeRec[i].EndMBBC, objBoardBarCoeRec[i].OutputDate, i); sqlList.Add(sqlStr.ToString()); } return SQLHelper.UpdateByTran(sqlList); }
/// <the Summary> /// enable transaction submit multiple SQL statements /// </ the Summary> /// <param name = "sqlList"> SQL statement collection </ param> /// <returns A> return bool whether the type of transaction performed on behalf of success </ Returns> public static BOOL UpdateByTran (List < String > sqlList) { the SqlConnection Conn = new new the SqlConnection (connString); the SqlCommand cmd = new new the SqlCommand (); cmd.Connection = Conn; the try { conn.Open (); cmd.Transaction = conn.BeginTransaction (); // open transaction // loop execution Sql statement the foreach ( var itemSql in sqlList) { cmd.CommandText = itemSql; the cmd.ExecuteNonQuery (); } cmd.Transaction .Commit (); // commit the transaction (the real data modified from the database) return to true ; } the catch (Exception EX) { IF (cmd.Transaction =!null ) cmd.Transaction.Rollback (); // roll back the transaction (return undo "normal job") the throw new new Exception ( " Error Calling transaction method appears: " + ex.Message); the throw ; } a finally { IF (cmd.Transaction ! = null ) { cmd.Transaction = null ; // Clear transaction } conn.Close (); } }