vbhaha
asked on
Search a value in a datareader
What is the best way to search a value (string type) in a datareader (all columns) ?
ASKER
Because I wanted to use datareader as any temp array for searching different values.
If I cannot use datareader, would you tell me what can I use and give me some example please !
Thanks
If I cannot use datareader, would you tell me what can I use and give me some example please !
Thanks
Sure, please post the code u are using to read data into DataReader. Then it will be easier for me to give you an exact answer.
ASKER
Here you're:
Dim cmd As New SqlClient.SqlCommand
Dim retValue as string
Try
cmd.Connection = APCnn
cmd.CommandText = "select * from " + MappingTableName + " where sys_id='" & SysID & "'"
drTableMapping = cmd.ExecuteReader()
If Not drTableMapping.Read Then
Throw New Exception("No records in mapping table")
End If
Do While dr.Read()
if dr(1) = "value" then
retValue = dr(2)
endif
Loop
Catch ex As Exception
Throw New Exception(ex.Message, ex)
End Try
return retValue
Thnaks a lot.
Dim cmd As New SqlClient.SqlCommand
Dim retValue as string
Try
cmd.Connection = APCnn
cmd.CommandText = "select * from " + MappingTableName + " where sys_id='" & SysID & "'"
drTableMapping = cmd.ExecuteReader()
If Not drTableMapping.Read Then
Throw New Exception("No records in mapping table")
End If
Do While dr.Read()
if dr(1) = "value" then
retValue = dr(2)
endif
Loop
Catch ex As Exception
Throw New Exception(ex.Message, ex)
End Try
return retValue
Thnaks a lot.
There are lot of mistakes, but I will come to those later. Well, let me know what you are trying to do.
>> if dr(1) = "value" then <<
So what you are trying to do is, if you find "value" in DR(1) you want to return DR(2). What if you find the first match, do you still want to go looking for the next match ? What will happen to retValue if you do not have any match ?
-Baan
>> if dr(1) = "value" then <<
So what you are trying to do is, if you find "value" in DR(1) you want to return DR(2). What if you find the first match, do you still want to go looking for the next match ? What will happen to retValue if you do not have any match ?
-Baan
One more question ... Why did you give me "C" in the last question you accepted ? I gave you the right answer I guess.
ASKER
Yes you're right. I should add 'exit' after the first match.
Actually, this function is used for table name mapping, it input the key value and return the physical table name.
It should be something like that:
Public Function GetTableName(ByVal inKey As String) As String
Dim retValue As String = ""
Try
Do While dr.Read()
if dr(1) = "value" then
retValue = dr(2)
exit do
end if
Loop Catch ex As Exception
Throw New Exception("The table mapping key '" + inKey.Trim + "' doesn't exist !")
End Try
Return retValue
End Function
Thanks.
Actually, this function is used for table name mapping, it input the key value and return the physical table name.
It should be something like that:
Public Function GetTableName(ByVal inKey As String) As String
Dim retValue As String = ""
Try
Do While dr.Read()
if dr(1) = "value" then
retValue = dr(2)
exit do
end if
Loop Catch ex As Exception
Throw New Exception("The table mapping key '" + inKey.Trim + "' doesn't exist !")
End Try
Return retValue
End Function
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
-Baan