How to read data from dataset?

  • Thread starter Thread starter JenHu
  • Start date Start date
J

JenHu

I don't know how to put the datarow value into a string value, can you
please help?

For example: cmdBatchName SQL command will return 3 file_name
121104_14
121204_16
121204_17

I want to get these 3 values in my For loop.

THanks.
--------------------------------------------------------------------------
Dim EpayConnection As New SqlConnection
Dim cmdBatchName As New SqlCommand
Dim dtrBatchName As SqlDataReader
Dim strRetFileName As String
Dim dataAdapter As New SqlDataAdapter
Dim dataSet As New DataSet
Dim r As DataRow
EpayConnection.ConnectionString = strEpayDBConn
cmdBatchName.Connection = EpayConnection
cmdBatchName.CommandText = "SELECT File_Name FROM
EPay_Batch_Table Where File_Status=2"
cmdBatchName.CommandType = CommandType.Text
dataAdapter.SelectCommand = cmdBatchName
dataAdapter.Fill(dataSet, "dtEPayFiles")
Dim dt As New DataTable
EpayConnection.Open()
For Each r In dataSet.Tables("dtEpayFiles").Rows
strRetFileName = ????
Next

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
strRetFileName = r("File_Name")

should do it - you may need a tostring - not used to the VB syntax.
 
You have a few lines of code you don't need.... that EpayConnection.Open()
is totally unnecessary and unless you are closing it somewhere is only going
to cause you some problems.

You can two ways, nominal lookups or ordinal ones. Nominal ones are slower
but clear, ordinal ones are faster - you can use an enum (Bill Vaughn's
trick) to get the best of both worlds

For Each r as DataRow in dataSet.Tables("dtEpayFiles").Rows
strRetFileName = r[FileNameIndex].ToString() //or
f["File_Name"].ToString()

Make sure you have Option Strict ON too! trust me on this
Next
 
Bill,

Did you know that this one is the fastest.
We did some tests in the VBNet.language group with this (Jay and me).

http://msdn.microsoft.com/library/d...tml/frlrfsystemdatadatarowclassitemtopic2.asp

I thought it was twice as fast. However, even than not much when you see how
few time this kind of operations take.

It seems to be in David Sceppa's book.

Cor

W.G. Ryan eMVP said:
You have a few lines of code you don't need.... that EpayConnection.Open()
is totally unnecessary and unless you are closing it somewhere is only
going
to cause you some problems.

You can two ways, nominal lookups or ordinal ones. Nominal ones are
slower
but clear, ordinal ones are faster - you can use an enum (Bill Vaughn's
trick) to get the best of both worlds

For Each r as DataRow in dataSet.Tables("dtEpayFiles").Rows
strRetFileName = r[FileNameIndex].ToString() //or
f["File_Name"].ToString()

Make sure you have Option Strict ON too! trust me on this
Next

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
JenHu said:
I don't know how to put the datarow value into a string value, can you
please help?

For example: cmdBatchName SQL command will return 3 file_name
121104_14
121204_16
121204_17

I want to get these 3 values in my For loop.

THanks.
--------------------------------------------------------------------------
Dim EpayConnection As New SqlConnection
Dim cmdBatchName As New SqlCommand
Dim dtrBatchName As SqlDataReader
Dim strRetFileName As String
Dim dataAdapter As New SqlDataAdapter
Dim dataSet As New DataSet
Dim r As DataRow
EpayConnection.ConnectionString = strEpayDBConn
cmdBatchName.Connection = EpayConnection
cmdBatchName.CommandText = "SELECT File_Name FROM
EPay_Batch_Table Where File_Status=2"
cmdBatchName.CommandType = CommandType.Text
dataAdapter.SelectCommand = cmdBatchName
dataAdapter.Fill(dataSet, "dtEPayFiles")
Dim dt As New DataTable
EpayConnection.Open()
For Each r In dataSet.Tables("dtEpayFiles").Rows
strRetFileName = ????
Next

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
Back
Top