Reg the SQLBulkCopy Class

Hi

for my application, I have to do a bulk transfer of records to my Db(MS SQL).

But the Records have to be fetched from different db.May be Oracle,MsAccess.

What i was doing was, opening a connection to the db and move the records to a dataset. After that moving the records to my db using for loops.

Now i came to know about the SqlBulkCopy class and found it interesting.

My Doubt is, if my db is Oracle and i have fetched the records from the Oracle Db and placed into a dataset, Can i use the SqlBulkCopy class to move the records into a table in my database

If so, kindly help with some coding samples...

Thanks in advance.

Regards,

satish.r



Answer this question

Reg the SQLBulkCopy Class

  • Gemita

    Could you help me with some sample code to do that.

    That is Moving a Oracle Table to my Sql table using SQLBulkCopy class

    Coz, i searched many sites and couldn get a sample code.

    Thanks in advance.


  • BlueBeetle

    One final query.

    What is the base class for the SqlBulkCopy. Is it available in vs 2003.

    that is which namespace should i include


  • t47

    I was just playing with this and it turns out the WriteToServer Method takes a DataTable as an argument, so you don't even have to waste your time with:

    IDataReader dr = dataset.Tables[0].CreateDataReader();

    You can simply pass the DataTable to the WriteToServer Method:

    using (SqlBulkCopy copy = new SqlBulkCopy(_destinationConnectionString))
    {
    copy.DestinationTableName = "TheDestinationTableName";
    copy.WriteToServer(dataset.Tables[0]);
    }

    Regards,

    Dave



  • Kiro Petrovski

    Hi,

    SqlBulkCopy is the class. There is no equivalent in .net 1.x.



  • Avi.harush

    Hi,

    Yes, SqlBulkCopy will work as long as source is either IDataReader or DataTable, excerpt from help file:

    "The SqlBulkCopy class can be used to write data only to SQL Server tables. But the data source is not limited to SQL Server; any data source can be used, as long as the data can be loaded to a DataTable instance or read with a IDataReader instance."

    And you don't even need to use DataSet/DataTable - instead you might use OracleDataReader.



  • eXavier

    The SqlBulkCopy is so basic that I am surprised you couldn't find an example to help you. Here is an example on my blog:

    SqlBulkCopy - Copy Table Data Between SQL Servers at High Speeds - ADO.NET 2.0 New Feature

    In your case, since you are pulling data from a table in the DataSet, your IDataReader will come from the DataTable object in the DataSet:

    IDataReader dr = dataset.Tables[0].CreateDataReader();

    In the end you would have some code that looks like:

    IDataReader dr = dataset.Tables[0].CreateDataReader();

    using (SqlBulkCopy copy = new SqlBulkCopy(_destinationConnectionString))
    {
    copy.DestinationTableName = table;
    copy.WriteToServer(dr);
    }

    Regards,

    Dave



  • Reg the SQLBulkCopy Class