Here is my original code:
//insert the first record
String conStr="server=localhost; Port=3306; User Id=root; Password=8331; The database=tp; Allow the User Variables=True ";
MySqlConnection conn=new MySqlConnection (conStr);
Try
{
Conn. The Open ();
MySqlCommand cmd=new MySqlCommand("insert into table1(id,d_date,d_time,td1,td2,td3,td4,td5,td6,td7,td8,td9,td10,td11,td12,td13,td14,td15,td16,td17,td18,td19,td20,td21,td22,td23,td24,td25,td26,td27,td28,td29,td30,td31,td32,td33,td34,td35,td36,td37,td38,td39,td40,pd1,pd2,pd3,pd4,pd5,pd6,pd7,pd8,pd9,pd10,a_press) VALUES(@id,@d_date,@d_time,@td1,@td2,@td3,@td4,@td5,@td6,@td7,@td8,@td9,@td10,@td11,@td12,@td13,@td14,@td15,@td16,@td17,@td18,@td19,@td20,@td21,@td22,@td23,@td24,@td25,@td26,@td27,@td28,@td29,@td30,@td31,@td32,@td33,@td34,@td35,@td36,@td37,@td38,@td39,@td40,@pd1,@pd2,@pd3,@pd4,@pd5,@pd6,@pd7,@pd8,@pd9,@pd10,@a_press);" , conn);
CMD. The Parameters. AddWithValue (" @ id, "CableNo);
CMD. The Parameters. AddWithValue (" @ d_date ", dt);
CMD. The Parameters. AddWithValue (" @ d_time ", dt);
CMD. The Parameters. AddWithValue (" @ td1, "FTD [0]).
CMD. The Parameters. AddWithValue (" @ td2 FTD [1]).
CMD. The Parameters. AddWithValue (" @ td3 ", FTD [2]).
CMD. ExecuteNonQuery ();
}
The catch (Exception ex)//check the Exception object creation
{
MessageBox. Show (ex. Message);//output error message
}
The finally
{
Conn. Close ();//close the connection
}
}
//insert the second record
String conStr="server=localhost; Port=3306; User Id=root; Password=8331; The database=tp; Allow the User Variables=True ";
MySqlConnection conn=new MySqlConnection (conStr);
Try
{
Conn. The Open ();
MySqlCommand cmd=new MySqlCommand("insert into table1(id,d_date,d_time,td1,td2,td3,td4,td5,td6,td7,td8,td9,td10,td11,td12,td13,td14,td15,td16,td17,td18,td19,td20,td21,td22,td23,td24,td25,td26,td27,td28,td29,td30,td31,td32,td33,td34,td35,td36,td37,td38,td39,td40,pd1,pd2,pd3,pd4,pd5,pd6,pd7,pd8,pd9,pd10,a_press) VALUES(@id,@d_date,@d_time,@td1,@td2,@td3,@td4,@td5,@td6,@td7,@td8,@td9,@td10,@td11,@td12,@td13,@td14,@td15,@td16,@td17,@td18,@td19,@td20,@td21,@td22,@td23,@td24,@td25,@td26,@td27,@td28,@td29,@td30,@td31,@td32,@td33,@td34,@td35,@td36,@td37,@td38,@td39,@td40,@pd1,@pd2,@pd3,@pd4,@pd5,@pd6,@pd7,@pd8,@pd9,@pd10,@a_press);" , conn);
CMD. The Parameters. AddWithValue (" @ id, "CableNo);
CMD. The Parameters. AddWithValue (" @ d_date ", dt);
CMD. The Parameters. AddWithValue (" @ d_time ", dt);
CMD. The Parameters. AddWithValue (" @ td1, "FTD [0]).
CMD. The Parameters. AddWithValue (" @ td2 FTD [1]).
CMD. The Parameters. AddWithValue (" @ td3 ", FTD [2]).
CMD. ExecuteNonQuery ();
}
The catch (Exception ex)//check the Exception object creation
{
MessageBox. Show (ex. Message);//output error message
}
The finally
{
Conn. Close ();//close the connection
}
}
//insert the third record
String conStr="server=localhost; Port=3306; User Id=root; Password=8331; The database=tp; Allow the User Variables=True ";
MySqlConnection conn=new MySqlConnection (conStr);
Try
{
Conn. The Open ();
MySqlCommand cmd=new MySqlCommand("insert into table1(id,d_date,d_time,td1,td2,td3,td4,td5,td6,td7,td8,td9,td10,td11,td12,td13,td14,td15,td16,td17,td18,td19,td20,td21,td22,td23,td24,td25,td26,td27,td28,td29,td30,td31,td32,td33,td34,td35,td36,td37,td38,td39,td40,pd1,pd2,pd3,pd4,pd5,pd6,pd7,pd8,pd9,pd10,a_press) VALUES(@id,@d_date,@d_time,@td1,@td2,@td3,@td4,@td5,@td6,@td7,@td8,@td9,@td10,@td11,@td12,@td13,@td14,@td15,@td16,@td17,@td18,@td19,@td20,@td21,@td22,@td23,@td24,@td25,@td26,@td27,@td28,@td29,@td30,@td31,@td32,@td33,@td34,@td35,@td36,@td37,@td38,@td39,@td40,@pd1,@pd2,@pd3,@pd4,@pd5,@pd6,@pd7,@pd8,@pd9,@pd10,@a_press);" , conn);
CMD. The Parameters. AddWithValue (" @ id, "CableNo);
CMD. The Parameters. AddWithValue (" @ d_date ", dt);
CMD. The Parameters. AddWithValue (" @ d_time ", dt);
CMD. The Parameters. AddWithValue (" @ td1, "FTD [0]).
CMD. The Parameters. AddWithValue (" @ td2 FTD [1]).
CMD. The Parameters. AddWithValue (" @ td3 ", FTD [2]).
CMD. ExecuteNonQuery ();
}
The catch (Exception ex)//check the Exception object creation
{
MessageBox. Show (ex. Message);//output error message
}
The finally
{
Conn. Close ();//close the connection
}
}
CodePudding user response:
Open the connection - the open transaction - insert data - to commit the transaction - closes the connectionCodePudding user response:
Positive solution on the ground floor, connection. BeginTransaction () open transactions can,CodePudding user response: