Search code examples
c#sql-serverodbcbulk-load

ODBC Bulk Copy C#


I was using Bulk Copy like this below and it was working fine:

using (SqlTransaction bulkCopyTransaction = connector.RawConnection.BeginTransaction())
{
    using (SqlBulkCopy bk = new SqlBulkCopy(connector.RawConnection, SqlBulkCopyOptions.KeepIdentity, bulkCopyTransaction))
    {

Then I got the very ugly news that I had to revise my system to use ODBC instead of system.sql.data and there is no bulk copy. This is a huge problem because I rely on bulk copy a lot. The reason why they need me to use ODBC is because they want to change computers and passwords and servers whenever they like and not have to worry about recompiling and the app.config file cannot have an unencrypted password in it.

So my needs are that I want to take a CSV file - just as it is - all the columns in the order they come in and put them in an SQL file. The files can be very big (i.e. 1 GB+)

I have explored using a stored procedure with BCP in it but that requires a password. I have explored using Bulk Insert and that is a possibility but it will require me to make an XSD file out of every CSV variation (and that is many). I have looked at the SQL Bulk Copy package (http://www.sqlbulkcopy.org/getting-started/) but that requires column binding as well.

If you have any ideas, input or experience in this, I would love to hear your thoughts.


Solution

  • Here is the fastest way that I found to be able to be able to do this:

    ObdcCommand cmd = new ObdcCommand("insert into TableName
        select * from OpenRowset
        ('MSDASQL', 'Driver={Microsoft Access Text Driver (*.txt, *.csv)}; 
        ColNameHeader = TRUE; Format = CSVDelimited; ''','select * from" + FileName+"')", connection);