Write a batch update code, in order to facilitate the search, sent up
1 classDBHelper2 {3 //Operation Profile4Configuration config =configurationmanager.openexeconfiguration (configurationuserlevel.none);5 //first fetch once, as the basis for comparison6SqlConnection conn =NewSqlConnection ();7SqlCommand Comm =NewSqlCommand ();8 //Link Database9 Public voidOpen ()Ten { Oneconn =NewSqlConnection (config. appsettings.settings["ConnStr"]. Value); A Conn. Open (); - } - //Disconnect Connection the Public voidClose () - { - Conn. Close (); - } + //executes SQL and returns the first column of the first row - Public ObjectExecuteScalar (stringsql) + { AComm.commandtext =SQL; atComm. Connection =Conn; - returnComm. ExecuteScalar (); - } - //executes the SQL and returns the execution results - PublicDataTable GetResult (stringsql) - { inSqlDataAdapter SDA =NewSqlDataAdapter (SQL, conn); -DataTable dt =NewDataTable (); to SDA. Fill (DT); + returnDT; - } the //Execute SQL * Public voidExecuteNonQuery (stringsql) $ {Panax NotoginsengComm.commandtext =SQL; - Comm. ExecuteNonQuery (); the } + //Batch Update A Public voidUpdate (DataTable DT,stringtablename) the { + using(SqlBulkCopy sqlcopy =NewSqlBulkCopy (conn)) - { $Sqlcopy. Bulkcopytimeout =10000; $Sqlcopy. DestinationTableName =TableName; - sqlcopy. WriteToServer (DT); - } the } -}
SQL Server batch Update