Insert tens of thousands of data records at a time
1.
[CSHARP]View plaincopy
- /// <Summary>
- /// Datatable batch add (with transactions)
- /// </Summary>/
- // <Param name = "table"> data source </param>
- /// <Param name = "mapping"> define a set of relationships between data sources and target source columns </param>
- /// <Param name = "destinationtablename"> target table </param>
- Public static bool mysqlbulkcopy (datatable table, sqlbulkcopycolumnmapping [] mapping, string destinationtablename ){
- Bool = true;
- Using (sqlconnection con = new sqlconnection (connectionstring )){
- Con. open ();
- Using (sqltransaction TRAN = con. begintransaction ()){
- Using (sqlbulkcopy copy = new sqlbulkcopy (con, sqlbulkcopyoptions. keepidentity, Tran )){
- Copy. destinationtablename = destinationtablename; // specify the target table
- If (mapping! = NULL) {// If data exists
- Foreach (sqlbulkcopycolumnmapping map in mapping ){
- Copy. columnmappings. Add (MAP );}}
- Try {copy. writetoserver (table); // batch add
- Tran. Commit (); // submit the transaction}
- Catch {Tran. rollback ();
- // Roll back the transaction bool = false ;}}}}
- Return bool ;}
2. MSSQL uses sqlbulkcopy to upload the data source, table name, and column shadow. It takes 1.5 million seconds to import the data.
[CSHARP]View plaincopy
- /// <Summary>
- /// Use sqlbulkcopy for batch insert. Only sqlserver
- /// Disadvantage: no number of returned rows
- /// </Summary>
- /// <Param name = "table"> the filled datatable supports other data sources. See reload. </param>
- /// <Param name = "tablename"> name of the table corresponding to the database </param>
- /// <Param name = "columns"> insert a column name set for the table. </param>
- Public void sqlbulkcopyinsert (datatable table, string tablename, string [] columns)
- {
- Sqlbulkcopy SBC = new sqlbulkcopy ("successive strings ");
- SBC. destinationtablename = tablename;
- Foreach (string Col in columns)
- {
- SBC. columnmappings. Add (COL, col );
- }
- SBC. writetoserver (table );
- }
3. For other databases, find a datatable, fill the data in the table, and then in adpt. Update (table)
[CSHARP]View plaincopy
- /// <Summary>
- /// Insert multiple rows. Check the connection/command/dataadapter for the database type you are connected.
- /// Replace the value
- /// </Summary>
- /// <Param name = "ds"> dataset after data filling </param>
- /// <Returns> Number of affected rows </returns>
- Public int multyinsert (Dataset DS)
- {
- Int result = 0;
- Idbconnection con = new oracleconnection ("connection string ");
- Con. open ();
- Idbcommand cmd = new oraclecommand ();
- Cmd. commandtext = "insert into member (username, password) values (@ name, @ password )";
- Idbdataparameter namepar = cmd. createparameter ();
- Namepar. parametername = "@ name ";
- Namepar. sourcecolumn = "username ";
- Namepar. sourceversion = datarowversion. Original;
- Namepar. dbtype = dbtype. String;
- Cmd. Parameters. Add (namepar );
- Idbdataparameter passpar = cmd. createparameter ();
- Passpar. parametername = "@ pass ";
- Passpar. dbtype = dbtype. String;
- Passpar. sourcecolumn = "password ";
- Passpar. sourceversion = datarowversion. Original;
- Cmd. Parameters. Add (passpar );
- Idbdataadapter adpt = new oracledataadapter ();
- Adpt. insertcommand = cmd;
- Try
- {
- Result = adpt. Update (DS );
- }
- Catch (exception)
- {
- Throw;
- }
- Finally
- {
- Con. Close ();
- }
- Return result;
- }