record count

  • Thread starter Thread starter salih ataoz
  • Start date Start date
S

salih ataoz

in visual basic 6 rst.recordcount shows us how many
records are there in rst
but in studio .net
i dont konw ho to learn recordcount
the code is like this


cnn.Open()
Dim cmd As OleDbCommand = New OleDbCommand()
cmd.Connection = cnn
cmd.CommandType = CommandType.Text
Dim sql As String
sql = "select * from users where username='" &_
TextBox1.Text & "' and password='" & TextBox2.Text & "';"

cmd.CommandText = sql
Dim result1 As OleDbDataReader
result1 = cmd.ExecuteReader
................



here i must learn the recordcount
 
Correct me if I'm wrong, but I don't think that you can get a record count
with a datareader. If you use a dataadapter and a datatable, you can get it
like this:

Dim sql As String
sql = "select * from users where username='" & _
TextBox1.Text & "' and password='" & TextBox2.Text & "';"
Dim dt As New DataTable
Dim o As New OleDb.OleDbDataAdapter(sql, cnn)
o.Fill(dt)
MessageBox.Show(dt.Rows.Count)
 
Two things... The reader doesn't know in advance how many records it has.
In VS 2003 it does have a .HasRows boolean that will return True if there
are records, but that's it. If you want to know the count, you are going to
have to walk forward through the reader and increment a counter.

Dim i as Int

While dr.Read
i+=1
End While
Debug.Writeline(i.ToString)

Now, as a matter of efficiency, avoid Dynamic SQL at all costs...
sql = "select * from users where username='" &_
TextBox1.Text & "' and password='" & TextBox2.Text & "'

replace it with "SELECT * FROM users WHERE username = @User and Password =
@Password"

Now, add the parameters to the commands Parameters collection

With cmd.Parameters
.clear
.Add("@User", textBox1.text)
.Add("@Password" , textBox2.text)

End WIth

Actually, you are using OLEDB so you should use ? instead (I thnk). You can
also specify the types and direction of the parameters.

It's faster, cleaner, and you don't run the risk of Injection attacks and
oversight where people have "'" in their name.
HTH,

Bill
 
Back
Top