You may use the code from the
OleDB-SQL Utility[
^].
See also there examples of connection strings and SQL queries to Excel. For example:
SELECT NULL AS ImportID, * FROM [Sheet1$]
Here is the main function to import using SqlBulkCopy:
static void OleDbToSqlServer(string oleDbConnectionString, string oleDbSQL,
string sqlConnectionString, string sqlTableName)
{
oleDbConnectionString = ExpandConnectionStringFileName(oleDbConnectionString);
OleDbConnection oleDbConnection = new OleDbConnection(oleDbConnectionString);
try
{
oleDbConnection.Open();
OleDbCommand command = new OleDbCommand(oleDbSQL, oleDbConnection);
try
{
OleDbDataReader reader = command.ExecuteReader();
try
{
SqlConnection sqlConnection = new SqlConnection(sqlConnectionString);
try
{
sqlConnection.Open();
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnection))
{
bulkCopy.DestinationTableName = sqlTableName;
bulkCopy.WriteToServer(reader);
}
}
finally
{
sqlConnection.Close();
}
}
finally
{
reader.Close();
}
}
finally
{
command.Dispose();
}
}
finally
{
oleDbConnection.Close();
}
}