|
-
Feb 4th, 2009, 12:20 PM
#1
Thread Starter
Lively Member
[RESOLVED] Dataset - datatable
Hi, I have a dataset and inside it a dataTable. How can I retrieve the data of the datatable?
My code is this:
objDataAdapter.SelectCommand = New SqlCommand()
objDataAdapter.SelectCommand.Connection = objConnection
objDataAdapter.SelectCommand.CommandText = _
"SELECT distinct Title FROM Outlookbar "
objDataAdapter.SelectCommand.CommandType = CommandType.Text
objConnection.Open()
' Fill the DataSet object with data...
objDataAdapter.Fill(objDataSet, "Outlookbar")
............
I think that untill here I have created a Dataset with a dataTable that has the name Outlookbar. In my example the sql query must return 4 rows(4 data from the table of the database).
So, how can I store its one of these rows (of the dataTable) in a variable for later use?
-
Feb 4th, 2009, 08:21 PM
#2
Fanatic Member
Re: Dataset - datatable
Look at objDataSet.Tables(0).Rows - you can loop through the 4 rows and read values as needed.
-
Feb 4th, 2009, 10:28 PM
#3
Re: Dataset - datatable
What VBCrazyCoder said is absolutely correct but, given that you've created the DataTable by name, I would tend to access it by name too, i.e. objDataSet.Tables("Outlookbar"). Neither is technically more correct than the other but using a name to access a named object feels more intuitive to me, plus it reinforces the realtionship between the table you filled and the table you're accessing, which may not otherwise be immediately obvious. Using the name is also easier when you have multiple tables, so you don't have to care which one is first.
-
Feb 5th, 2009, 01:40 AM
#4
Thread Starter
Lively Member
Re: Dataset - datatable
Thank you for answering. I managed to find it :
lblArchives.Text = objDataSet.Tables("Outlookbar").Rows(0).Item("Title")
AS you can see I am using the instance of the DataSet (objDataSet),then the Tables("Outlookbar") which is the collection name of the DataTable and the row and the Item to get the right row and the right column from the DataTable.
Thanks for answering to me
-
Feb 6th, 2009, 06:11 PM
#5
Fanatic Member
Re: [RESOLVED] Dataset - datatable
I guess the only thing to keep in mind would be that string comparisons are less efficient than ordinal based lookups - so if you had a large amount of data you would probably want to use ordinals (like for accessing rows, etc.) - you can always use an enum or something to get the name / value pairs to still be able to go by a name but actually use the value - if that makes sense.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|