The example in this article describes how C # implements SQL BULK insert data into a table. Share to everyone for your reference, specific as follows:
#region Help instance: SQL Bulk Insert data multiple methods///<summary>///sqlbulkcopy Insert data into the database///</summary>///<param name= "sour cedatatable > Data source table </param>///<param name= "targettablename" > Server destination table </param>///<param name= "Mapping" > Creates a new column mapping and uses the column ordinal to refer to the column names of the source and destination columns. </param> public static void Bulktodb (DataTable sourcedatatable, string targettablename, sqlbulkcopycolumnmapping
[] Mapping {* * Call Method-November 16, 2012 write//datatable dt = Get_all_roomstate_byhid ();
sqlbulkcopycolumnmapping[] mapping = new SQLBULKCOPYCOLUMNMAPPING[4];
Mapping[0] = new Sqlbulkcopycolumnmapping ("xing_h_id", "xing_h_id");
MAPPING[1] = new Sqlbulkcopycolumnmapping ("H_name", "h_name");
MAPPING[2] = new Sqlbulkcopycolumnmapping ("H_sname", "h_sname");
MAPPING[3] = new Sqlbulkcopycolumnmapping ("H_ename", "h_ename");
Bulktodb (DT, "bak_tts_hotel_name", mapping);
* * SqlConnection conn = new SqlConnection (sqlhelper.connectionstring); SqlBulkCopy bulkcopy = new SqlBulkCopy (CONN); Efficient bulk loading of SQL Server tables with data from other sources bulkcopy.destinationtablename = Targettablename; The name of the target table on the server bulkcopy.batchsize = SourceDataTable.Rows.Count; Number of rows in each batch try {Conn.
Open (); if (sourcedatatable!= null && sourceDataTable.Rows.Count!= 0) {for (int i = 0; i < mapping. Length;
i++) BulkCopy.ColumnMappings.Add (Mapping[i]);
Copies all rows from the provided data source to the target table Bulkcopy.writetoserver (sourcedatatable);
The catch (Exception ex) {//throw ex; Common.writetextlog ("Bulktodb", ex.
message); finally {Conn.
Close ();
if (bulkcopy!= null) bulkcopy.close (); }}///<summary>///SQL2008 supports custom table types: Calling stored procedure cursors-bulk inserting data into the database, note///</summary>///<param name= "sourced Atatable "></param> public void Datatabletohoteldb (DataTable sourcedatatable) {/*-write ALTER PROC November 15, 2012 edure [dbo]. [P_insertsubject] @tempStudentID int as DECLARE Rs CURSOR local SCROLL for select h_id froM tts_hotel_name OPEN rs FETCH NEXT from RS to @tempStudentID while @ @FETCH_STATUS = 0 BEGIN Insert Student (Tempstudentid) VALUES (@tempStudentID) FETCH NEXT from RS to @tempStudentID end Close RS * * * * * * * * * * * * * * * CREATE TABLE Orders (orders_id int identity (1,1) Primary key, ItemCode nvarchar (a) not null, UM nvarchar (m) not NULL, Quantity decimal (18,6) is not NULL, U
Nitprice decimal (18,6) NOT NULL)--Creates user-defined table types,-> user-defined table types in Programmability-> type Create type Orderstabletype as table (ItemCode nvarchar not null, UM nvarchar is not null, Quantity decimal (18,6) is not NULL, unitpric e decimal (18,6) NOT NULL) GO CREATE PROCEDURE pro_orders (@OrdersCollection orderstabletype Readon ly) as insert into Orders ([Itemcode],[um],[quantity],[unitprice]) SELECT oc.[itemcode],oc.[um],[quantit Y],oc.[unitprice] from @Orderscollection as OC;
Go * */sqlparameter[] Parameters = {New SqlParameter ("@OrdersCollection", sqldbtype.structured)}; Parameters[0].
Value = sourcedatatable; New SQLHelper ().
ExecuteScalar ("P_datatable_tohoteldb", parameters, True);
} #endregion
Read more about C # Interested readers can view the site topics: "C # Data structure and algorithm tutorial", "C # Common control usage Tutorial", "C # object-oriented Program design Introductory Course" and "C # Programming Thread Usage Skills Summary"
I hope this article will help you with C # programming.