cjjeeper
cjjeeper

Reputation: 93

Insert rows to DataTable from SQL table

I have a Datatable in my C# program that I would like to INSERT rows from a temp table in SQL Server. The dataAdapter.FILL() method writes over the whole datatable. I need to keep the records that are in the DataTable and add records that exist in the temp table back into my DataTable.

I do not see a DA method for that, they all seem to go back to the SQL Server table except FILL. Is this possible?

Upvotes: 0

Views: 1810

Answers (2)

joe
joe

Reputation: 729

// Now, open a database connection using the Microsoft.Jet.OLEDB provider.
// The "using" statement ensures that the connection is closed no matter what.
using (var connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data      source=Northwind.mdb"))
{
    connection.Open();

// Create an OleDbDataAdapter and provide it with an INSERT command.
var adapter = new OleDbDataAdapter();
adapter.InsertCommand = new OleDbCommand("INSERT INTO Shippers (CompanyName, Phone) VALUES (@CompanyName , @Phone)", connection);
adapter.InsertCommand.Parameters.Add("CompanyName", OleDbType.VarChar, 40, "CompanyName");
adapter.InsertCommand.Parameters.Add("Phone", OleDbType.VarChar, 24, "Phone");

adapter.Update(data);
}    

Upvotes: 0

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48558

How about Merging your DataTables

DataTable dttemp = new DataTable();

dataAdapter.Fill(dtTemp);

originaldatatable.Merge(dtTemp);

Upvotes: 2

Related Questions