Reputation: 20224
I want to use a dataadapter with a datatable to insert thousands of record into a 30-column sql table.
SqlDataAdapter adapter = new SqlDataAdapter();
DataTable table = new DataTable();
adapter.InsertCommand = new SqlCommand("INSERT INTO ...");
adapter.UpdateBatchSize = 1000;
DataRow r = null;
foreach(var entry in list)
{
r = table.NewRow();
r["lastchange"] = entry.TryGet("LastChangeTime");
// <- throws System.ArgumentException: Column does not belong to table
...
}
Is there any way to not manually define the schema of the datatable, but to read it from the table the insertions should take place in?
Upvotes: 0
Views: 2523
Reputation: 3860
You can create/define dataset in your project and can use it in your subsequent operation wherever you need.
Have a look at below link showing how to add dataset to your project and add tables into it and also how to use data adapters with this dataset.
https://msdn.microsoft.com/en-us/library/04y282hb.aspx
https://msdn.microsoft.com/en-us/library/ms171919.aspx
I hope this will help you. :)
Upvotes: -1
Reputation: 22811
Define SelectCommand
and apply Fill
method to get data first. If you need only table schema just make the query which returns no rows.
SqlDataAdapter adapter = new SqlDataAdapter();
DataTable table = new DataTable();
adapter.SelectCommand = new SqlCommand("SELECT * FROM myTable WHERE 1=2");
adapter.Fill(table);
Upvotes: 1