PC Review


Reply
Thread Tools Rate Thread

How to add a row to an SQL Dataset

 
 
JT
Guest
Posts: n/a
 
      22nd Feb 2007
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.


 
Reply With Quote
 
 
 
 
David
Guest
Posts: n/a
 
      22nd Feb 2007
Erm...

I think TradeRecords.Tables[0] will give you access to the first table in
the dataset.

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available


"JT" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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.
>
>



 
Reply With Quote
 
Marc Scheuner
Guest
Posts: n/a
 
      23rd Feb 2007
>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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to cast object of type 'System.Data.DataSet' to Typed DataSet Optimus Microsoft VB .NET 1 31st Jan 2006 06:26 AM
Ccopying a datatable content from an untyped dataset into a table which is inside a typed dataset Nedu N Microsoft ADO .NET 7 9th Dec 2003 02:50 PM
copying a datatable content from an untyped dataset into a table which is inside a typed dataset Nedu N Microsoft ADO .NET 2 31st Oct 2003 01:05 PM
copying a datatable content from an untyped dataset into a table which is inside a typed dataset Nedu N Microsoft ASP .NET 2 31st Oct 2003 01:05 PM
Ccopying a datatable content from an untyped dataset into a table which is inside a typed dataset Nedu N Microsoft ASP .NET 2 31st Oct 2003 02:42 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:36 AM.