In SQL Server, many people concatenate SQL statements by repeating the Excel data read by oledb. This is not only error-prone but also inefficient. The best way is to use BCP, that is, system. data. sqlclient. sqlbulkcopy class. Not only is the speed fast, but the code is simple. The following test code imports a sheet with more than 60 thousand pieces of data, including reading (all reading is slow). It takes about 10 seconds in my development environment, the real import process takes only 4.5 seconds.
Using system;
Using system. Data;
Using system. Windows. forms;
Using system. Data. oledb;
Namespace windowsapplication2
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}
Private void button#click (Object sender, eventargs E)
{
// Test. Import Sheet 1 in Excel to sqlserver.
String connstring = "Server = localhost; uid = sa; Pwd = sqlgis; database = Master ";
System. Windows. Forms. openfiledialog FD = new openfiledialog ();
If (FD. showdialog () = dialogresult. OK)
{
Transferdata (FD. filename, "sheet1", connstring );
}
}
Public void transferdata (string excelfile, string sheetname, string connectionstring)
{
Dataset DS = new dataset ();
Try
{
// Obtain all data
String strconn = "provider = Microsoft. Jet. oledb.4.0;" + "Data Source =" + excelfile + ";" + "extended properties = Excel 8.0 ;";
Oledbconnection conn = new oledbconnection (strconn );
Conn. open ();
String strexcel = "";
Oledbdataadapter mycommand = NULL;
Strexcel = string. Format ("select * from [{0} $]", sheetname );
Mycommand = new oledbdataadapter (strexcel, strconn );
Mycommand. Fill (DS, sheetname );
// Create a table if the target table does not exist
String strsql = string. Format ("If object_id ('{0}') is null create table {0} (", sheetname );
Foreach (system. Data. datacolumn C in DS. Tables [0]. columns)
{
Strsql + = string. Format ("[{0}] varchar (255),", C. columnname );
}
Strsql = strsql. Trim (',') + ")";
Using (system. Data. sqlclient. sqlconnection sqlconn = new system. Data. sqlclient. sqlconnection (connectionstring ))
{
Sqlconn. open ();
System. Data. sqlclient. sqlcommand command = sqlconn. createcommand ();
Command. commandtext = strsql;
Command. executenonquery ();
Sqlconn. Close ();
}
// Use bcp to import data
Using (system. Data. sqlclient. sqlbulkcopy BCP = new system. Data. sqlclient. sqlbulkcopy (connectionstring ))
{
BCP. sqlrowscopied + = new system. Data. sqlclient. sqlrowscopiedeventhandler (bcp_sqlrowscopied );
BCP. batchsize = 100; // number of rows transmitted each time
BCP. policyafter = 100; // number of rows prompted by the progress
BCP. destinationtablename = sheetname; // target table
BCP. writetoserver (Ds. Tables [0]);
}
}
Catch (exception ex)
{
System. Windows. Forms. MessageBox. Show (ex. Message );
}
}
// Progress display
Void bcp_sqlrowscopied (Object sender, system. Data. sqlclient. sqlrowscopiedeventargs E)
{
This. Text = E. rowscopied. tostring ();
This. Update ();
}
}
}
The above transferdata can be used directly. If you need to consider it carefully, you can use oledb to obtain the Excel table structure and add columnmappings to set the control field, in this way, you can achieve the same effect as data transmission of sqlserver.
C # obtain the table structure information of an Excel file through oledb
This is a question from the Forum. Similarly, you can obtain table structure information of databases such as access.
Using system;
Namespace consoleapplication11
...{
Class Program
...{
Public static void main ()
...{
Getexcelfileinfo (@ "C: a.xls ");
}
Private Static void getexcelfileinfo (string path)
...{
String strconn = "provider = Microsoft. Jet. oledb.4.0;" + "Data Source =" + path + ";" + "extended properties = Excel 8.0 ;";
System. Data. oledb. oledbconnection conn = new system. Data. oledb. oledbconnection (strconn );
Conn. open ();
System. Data. datatable table = conn. getoledbschematable (system. Data. oledb. oledbschemaguid. Tables, null );
Foreach (system. Data. datarow Drow in table. Rows)
...{
String tablename = Drow ["table_name"]. tostring ();
Console. writeline (tablename + ":");
System. Data. datatable tablecolumns = conn. getoledbschematable (system. Data. oledb. oledbschemaguid. columns, new object []... {null, null, tablename, null });
Foreach (system. Data. datarow drowcolumns in tablecolumns. Rows)
...{
String columnname = drowcolumns ["column_name"]. tostring ();
Console. writeline ("" + columnname );
}
}
Console. readkey (true );
}
}
}