Home > Net >  C # how to connect only one database, and then run 3 times to write SQL statements to three record i
C # how to connect only one database, and then run 3 times to write SQL statements to three record i

Time:03-30

I have a database, a total of more than 50 field, I'm going to record three in turn is inserted into the database, but I have the following program, connecting the three databases, execution speed too slow, I think transformed into connection only one database, and then insert the three records in the database,

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 connection

CodePudding user response:

Positive solution on the ground floor, connection. BeginTransaction () open transactions can,

CodePudding user response:

reference 1/f, kill matt, bull response:
open connection - open transaction - insert data - to commit the transaction - close the connection


Can have a code? You say too professional!

CodePudding user response:

About like this:
 
String conStr="server=localhost; Port=3306; User Id=root; Password=8331; The database=tp; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related