PC Review


Reply
Thread Tools Rate Thread

add new rows of data to a datatable of a dataset

 
 
=?Utf-8?B?QW5kcmV3?=
Guest
Posts: n/a
 
      22nd Mar 2006
Hi all,

I want to keep adding a row of data each time to the datatable of a dataset.
I see on msdn that I could do this:
ie.
workRow = workTable.NewRow();
workTable.Rows.Add(new Object[] {1, "Smith"});
eg. I have:
DataRow row = ds.Tables["AA"].NewRow();
ds.Tables["AA"].Rows.Add(new Object[] {xxxxxxxxxxx});

I was wondering if I could add a row each time by running a sql statement.
How do I do this instead of using an Object[] array ?? My existing code is as
below:

int intPID=1;
for (int i = 1; i<= 20; i++)
{
strCommandA = "select * from Product where productID="+ intPID;
SqlDataAdapter myCommandA = new SqlDataAdapter(strCommandA, myConnection);
myConnection.Open();
xxxx
myConnection.Close();

intPID++;
}

TIA.

regards,
andrew

 
Reply With Quote
 
 
 
 
=?Utf-8?B?VkhENTA=?=
Guest
Posts: n/a
 
      22nd Mar 2006
Hi Andrew,
Why don't you use BETWEEN in your SELECT statement? Instead of doing 20
connections to the DB, now you do it in only 1 shot, then use
DataAdapter.Fill() method to fill the dataset or datatable.

Hope this helps.
VHD50.

"Andrew" wrote:

> Hi all,
>
> I want to keep adding a row of data each time to the datatable of a dataset.
> I see on msdn that I could do this:
> ie.
> workRow = workTable.NewRow();
> workTable.Rows.Add(new Object[] {1, "Smith"});
> eg. I have:
> DataRow row = ds.Tables["AA"].NewRow();
> ds.Tables["AA"].Rows.Add(new Object[] {xxxxxxxxxxx});
>
> I was wondering if I could add a row each time by running a sql statement.
> How do I do this instead of using an Object[] array ?? My existing code is as
> below:
>
> int intPID=1;
> for (int i = 1; i<= 20; i++)
> {
> strCommandA = "select * from Product where productID="+ intPID;
> SqlDataAdapter myCommandA = new SqlDataAdapter(strCommandA, myConnection);
> myConnection.Open();
> xxxx
> myConnection.Close();
>
> intPID++;
> }
>
> TIA.
>
> regards,
> andrew
>

 
Reply With Quote
 
=?Utf-8?B?QW5kcmV3?=
Guest
Posts: n/a
 
      23rd Mar 2006
Hi,

Thanks for the reply. Actually my table structure doesn't allow me to use
the BETWEEN statement. The sqlstatement that I have given is just an example
of me requiring a loop to add a new row to the datatable.

Is there anyone else here who can help me out ?
Thanks.

regards,
andrew



 
Reply With Quote
 
=?Utf-8?B?VkhENTA=?=
Guest
Posts: n/a
 
      23rd Mar 2006
Suppose you know the data (how many columns there are & which data goes into
which column...), you can manually build a datarow, then add it to the
datatable. Some thing like this:
DataRow row = ds.Tables["AA"].NewRow();
row.Item(0) = x
row.Item(1) = y
row.Item(2) = z
.....
ds.Tables["AA"].Rows.Add(row);

Hope this helps.
VHD50.




"Andrew" wrote:

> Hi,
>
> Thanks for the reply. Actually my table structure doesn't allow me to use
> the BETWEEN statement. The sqlstatement that I have given is just an example
> of me requiring a loop to add a new row to the datatable.
>
> Is there anyone else here who can help me out ?
> Thanks.
>
> regards,
> andrew
>
>
>

 
Reply With Quote
 
=?Utf-8?B?QW5kcmV3?=
Guest
Posts: n/a
 
      24th Mar 2006
Hi,

Thanks for replying. What I did eventually was to create an object called row:

DataRow row = ds.Tables["AA"].NewRow();
row.aa = row.retrieveStudentResults2(intX, intY, "aa");
row.bb = row.retrieveStudentResults2(intX, intY, "bb");
ds.Tables["AA"].Rows.Add(new Object[] {row.aa, row.bb,row. cc, ..... });

The issue I am worried about is that this means that I have to make multiple
connections to the db just to populate my ds. I wonder if there is a more
efficient way of doing this.

This seems to work though.

regards,
andrew

"VHD50" wrote:

> Suppose you know the data (how many columns there are & which data goes into
> which column...), you can manually build a datarow, then add it to the
> datatable. Some thing like this:
> DataRow row = ds.Tables["AA"].NewRow();
> row.Item(0) = x
> row.Item(1) = y
> row.Item(2) = z
> ....
> ds.Tables["AA"].Rows.Add(row);
>
> Hope this helps.
> VHD50.
>
>
>
>
> "Andrew" wrote:
>
> > Hi,
> >
> > Thanks for the reply. Actually my table structure doesn't allow me to use
> > the BETWEEN statement. The sqlstatement that I have given is just an example
> > of me requiring a loop to add a new row to the datatable.
> >
> > Is there anyone else here who can help me out ?
> > Thanks.
> >
> > regards,
> > andrew
> >

 
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
DataList, DataSet or DataTable rows limit Aahz Microsoft C# .NET 5 10th Jun 2006 07:15 AM
How convert Excel data into DataSet/DataTable? Ronald S. Cook Microsoft C# .NET 2 14th Feb 2006 09:30 PM
DataTable or DataSet for Updating Data? Mike Wilson Microsoft ADO .NET 4 7th Jan 2006 01:19 PM
How to reteive data amongst different datatable within a dataset =?Utf-8?B?TGkgUGFuZw==?= Microsoft ADO .NET 2 1st Sep 2005 10:03 PM
Deleting rows from a DataTable in a DataSet =?Utf-8?B?TWljaGFlbA==?= Microsoft ADO .NET 4 23rd Jan 2004 01:43 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:51 AM.