add new rows of data to a datatable of a dataset

G

Guest

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
 
G

Guest

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.
 
G

Guest

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
 
G

Guest

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.
 
G

Guest

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 said:
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 said:
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
 
Joined
Dec 26, 2008
Messages
3
Reaction score
0
DataRow dr;
DataTable dt;
DataColumn dc;
dt.Columns.Add(dc);
dr = new DataRow();
dt.Rows.Add(dr);
DataSet ds = ds.Tables.Add(ds);

regards,
ANKIT CHAMPANERIYA
MUMBAI
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top