How to add a row to an SQL Dataset

J

JT

I have an SQL database with one table in it. I want to load the table into
a DataSet, add new records to the DataSet, then commit the changes back to
the database.

Using C#.NET, the following loads the DataSet and fills it:

string SelectString = "SELECT * FROM dbo.Trade";
SqlCommand RecordsSelection = new SqlCommand(SelectString,
ThisConnection);

RecordsSelection.CommandTimeout = 10;

SqlDataAdapter TradeAdapter = new SqlDataAdapter();
TradeAdapter.SelectCommand = RecordsSelection;
DataSet TradeRecords = new DataSet();
TradeAdapter.Fill(TradeRecords, "dbo.Trade");

Now I want to access the single table and add a new row. I can iterate
through the tables collection of the dataset, but for each table, there is
no name property.

So my question is, how can I access a table within this dataset so I can add
a new row?

I know this question is elementary, but I would appreciate any help as
research has not answered the question.

Thanks.
 
M

Marc Scheuner

Now I want to access the single table and add a new row. I can iterate
through the tables collection of the dataset, but for each table, there is
no name property.

There's a .TableName though.....

Once you have a handle to your DataTable (by means of

DataTable tblTrades = TradeRecords.Tables[0]

you can then call the .NewRow() method on the DataTable to get back a
new instance of a DataRow - now fill it, add it to the .Rows
collection, and call the .Update method on the TradeAdapter, and you
should be done.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top