开发者

How to check if table is empty in VB.net [duplicate]

This question already has answers here: 开发者_Python百科 Closed 12 years ago.

Possible Duplicate:

Checking for an SQL result in VB.NET

I have login form which is redirect user for their levels but before to that if there is no any user on the data table I would like to redirect to create admin form. I have all the forms but I didn't manage to redirect them because I don't know how to create statement. Could you please help me about solution.

    Dim con As SqlCeConnection
    Dim command As SqlCeCommand
    con = New SqlCeConnection("Persist Security Info=False;Data Source=.\database.sdf;Password=********;File Mode=shared read")
    con.Open()
    command = New SqlCeCommand("select * from users where Name=? and Password=?", con)

    Dim param1, param2 As SqlCeParameter

    param1 = New SqlCeParameter("Name", uname.Text)

    param2 = New SqlCeParameter("Password", pwd.Text)

    command.Parameters.Add(param1)
    command.Parameters.Add(param2)

    Dim reader As SqlCeDataReader = command.ExecuteReader

    If (reader.Read = True) Then
        role = reader.GetString(1)

    Else
        MsgBox("Invalid Login")
    End If

I have this code is working. What to write for

Private Sub frmlogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub


I would recommend trying the DataReader's HasRows property to determine if one or more rows was returned to the DataReader object.

if (reader.HasRows)
{

    reader.Read();
    role = reader.GetString(1)

}
else
{
    // invalid login
}


Not sure I completely understand your question, but if the table is empty then reader.Read() would evaluate to False.

I think what you want is a SQL Statement checking the Count of the users table. Something like

command = New SqlCeCommand("SELECT COUNT(Name) as NameCount FROM Users", con)

Then you would evaluate the count by doing something like

Dim reader as SqlCeDataReader = command.ExecuteReader() 
While(reader.Read())
   if reader("NameCount") = 0 then
       'Redirect to Admin Form

   else
       'Run all your current logic here to find the user from the DB
   end if
End While
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜