Hello all,
ADO is very new to me, so I hope this question doesn't seem too simple. If anybody can recommend a good ADO book or tutorial that would be great.
I am using C# Express and SQL Express. I've been able to connect to my database, use a TableAdapter to get access to it, and fill a DataSet with a query from the TableAdapter. My question is, how can I get access to the the rows and columns in my DataSet I know how to "databind" them to a form control, but I'm trying to just get them into variables.
For example, if my table looks like this:
int myAlbumsID (primary key, isIdentity=yes)
varchar[50] albumName
and the data in it is:
1, Album1
2, Album 2
3, Album 3
How can I put all the albumName's in, say, an array of strings
Thank you for your time,
Rob B

Accessing data in a DataSet
aacbob
Hi Rob,
ADO.NET is a massive subject that combines database, XML, SQL and OOP technologies into one extremely useful API. Initally it can all be a bit daunting but its absolutely worth the time to learn and once you get the idea you can see a lot of uses for it.
There is an excellent book called Professional ADO.NET Programming thats avaliable on Wrox (I don't have the ISBN). It's an excellent book.
As to your question... the DataTable is kinda already an array, an multi-dimensional array of DataColumn types and DataRow types. You can loop over the DataTable exactly the same way as you do with an array one DataRow at a time, accessing whichever DataColumn you want.
For example:
myDataSet.Table(0).Rows(1) - gives you access to the full second DataRow, similar to myArray(1) in a one dimentional array of objects
myDataSet.Table(0).Rows(1).Item(1) - gives you access to the second DataRow's second column, similar to myArray(1,1)
So you can loop through each rows second column like this
For nRowCount = 0 to myDataSet.Table(0).Rows.Count - 1
myValue = myDataSet.Table(0).Rows(nRowCount).Item(1).ToString
Next
Hopefully this will help.
blurgal
In C# syntax would like this.
For Adding New Row.
DataRow dr = yourDataSet.yourDataTable.NewRow();
dr[0]=1;dr[1]="Album1";//OR dr["AlbumID"]=1;dr["AlbumName"]="Album1";
yourDataSet.yourDataTable.Rows.Add(dr);
yourTableAdapter.update(yourDataSet.yourDataTable);
For Accessing Row and columns:
object myObject = yourDataSet.yourDataTable.Rows[0][0];//First Column in first Row.
Likewise you can do rest.
cheers
RyanPitt
DataSet.Tables(0).rows(0).item(0).toString gives you the value of the 1st rowm 1st column.
Note that you can also use Item("ColumnName") instead of the index
moggel
Thank you all for the help! I had a feeling it wouldn't be too difficult. I'm looking forward to learning more about ADO.net.
- Rob