29

I have a DataTable that I want to push to the DB. I want to be able to say like

myDataTable.update();

But after reading the MSDN docs, apparently this does inserts row by row.

It should be noted that these statements are not performed as a batch process; each row is updated individually.

What are my alternatives?

Edit: I am using SQL Server 2005

Joda Maki
  • 4,969
  • 6
  • 25
  • 33

5 Answers5

56

If using SQL Server, SqlBulkCopy.WriteToServer(DataTable)

Or also with SQL Server, you can write it to a .csv and use BULK INSERT

If using MySQL, you could write it to a .csv and use LOAD DATA INFILE

If using Oracle, you can use the array binding feature of ODP.NET

If SQLite:

Community
  • 1
  • 1
JohnB
  • 15,593
  • 15
  • 91
  • 109
9
string connectionString= ServerName + DatabaseName + SecurityType;
using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection)) {
    connection.Open();
    bulkCopy.DestinationTableName = "TableName";
    try {
        bulkCopy.WriteToServer(dataTableName);
    } catch (Exception e) {
        Console.Write(e.Message);
    }
}

Please note that the structure of the database table and the table name should be the same or it will throw an exception.

Stephan Weinhold
  • 1,493
  • 1
  • 23
  • 34
Vishal Kotak
  • 364
  • 3
  • 10
  • 2
    +1 for example :). Rather than saying "the structure should be the same", how about including SqlBulkCopy.ColumnMappings in the example – Mike M Jan 01 '18 at 01:02
4

This is going to be largely dependent on the RDBMS you're using, and whether a .NET option even exists for that RDBMS.

If you're using SQL Server, use the SqlBulkCopy class.

For other database vendors, try googling for them specifically. For example a search for ".NET Bulk insert into Oracle" turned up some interesting results, including this link back to Stack Overflow: Bulk Insert to Oracle using .NET.

Community
  • 1
  • 1
David
  • 68,722
  • 16
  • 125
  • 165
3

Here's how I do it using a DataTable. This is a working piece of TEST code.

using (SqlConnection con = new SqlConnection(connStr))
{
    con.Open();

    // Create a table with some rows. 
    DataTable table = MakeTable();

    // Get a reference to a single row in the table. 
    DataRow[] rowArray = table.Select();

    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(con))
    {
        bulkCopy.DestinationTableName = "dbo.CarlosBulkTestTable";

        try
        {
            // Write the array of rows to the destination.
            bulkCopy.WriteToServer(rowArray);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

    }

}//using
MrCode
  • 59,851
  • 9
  • 76
  • 106
  • 4
    Why do you convert to DataRow[]? the SqlBulkCoopy can take the DataTable itself as a param, so this seems like a pointless step. – SeeMoreGain Feb 10 '14 at 22:58
1

SqlBulkCopy class is best for SQL server,

Doing Bulk Upload/Insert of DataTable to a Table in SQL server in C#

Bala
  • 71
  • 2