|
-
Dec 3rd, 2003, 01:10 AM
#1
Thread Starter
Fanatic Member
Find a record in a datareader
Is it possible for me to find a record in a datareader .. I use below code .. (I don't want to use "Where clause in my SQL statement")
Dim mKode as string
Dim mSQLCheckData As String = "Select * from MyTable01 Order by Kode"
Dim DataReader As SqlDataReader
Dim SQLCommand As New SqlCommand(mSQLCheckData, SqlConnection1)
SqlConnection1.Open()
DataReader = SQLCommand.ExecuteReader()
--
--
--
SqlConnection1.Close()
please help
TIA
Winanjaya
-
Dec 3rd, 2003, 01:13 AM
#2
Why would you not want to use a WHERE clause? If you don't then it is just being unefficent because you have to keep returning rows until you find the one you want otherwise the only one returned would be the one you want. This adds network traffic also.
-
Dec 3rd, 2003, 07:36 AM
#3
Fanatic Member
Also, Select * is usually frowned upon, instead you might want to just return the columns you need (unless of course you need all of them )
-
Dec 3rd, 2003, 08:20 PM
#4
Thread Starter
Fanatic Member
Hi All,
As far as I know, in VB6 application, we can use find method in the recordset to find a record .. so is that mean that it is unavailble in
VB.NET ?
example:
mSQL= "Select * from Student Order by StudentCode"
rs.open msql,myconn,.. blah blah
rs.find "studentcode = ' "& mycode & " ' "
please advise .. thanks a lot in advance
regards
Winanjaya
-
Dec 3rd, 2003, 09:27 PM
#5
PowerPoster
USE A WHERE CLAUSE!!
OR
Use a dataset instead of datareader. Then you can create a dataview that will filter the objects.
-
Dec 4th, 2003, 04:26 PM
#6
Hyperactive Member
if you really just want to load the dataset and then interrogate it, then try this:
Dim tb2 As DataTable = ds2.Tables("Mytable")
Dim found As Boolean = False
Dim myRows() As DataRow
Dim selec As String = "Mykey = " & CStr(MyKey)
tb2 = ds2.Tables("Mytable")
myRows = tb2.Select(selec)
found = False
Dim i As Integer
For i = 0 To myRows.GetUpperBound(0)
found = True
' Exit For
Next i
if found then
dim dr2 as datarow = myRows(0)
.........
...........
end if
not great code but should give you an idea.
The underlying theme is to fill a dataset with records, then declare and use a datatable and its selec function to find a particular record that you want.
There are a few more examples in the msdn help as well
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
|