2014-04-08 88 views
0
Dim con As New SqlConnection 
con.ConnectionString = "Data Source=(local);Integrated Security=True" 
Dim cmd As New SqlCommand("SELECT * FROM login where Admin = @Admin AND Password = @Password ", con) 

'Set up parameters for the sqlcommand 
cmd.Parameters.AddWithValue("@Admin", comb.Text) 
cmd.Parameters.AddWithValue("@Password", Txtpass.Text) 

'If the username or password is empty then throw an error 
If comb.Text = String.Empty Then 
     MessageBox.Show("Please choose the username.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error) 
ElseIf Txtpass.Text = String.Empty Then 
     MessageBox.Show("Please enter the password.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error) 
Else 
     Try 
      'Open the connection and declare an sqlreader 
      con.Open() 
      Dim reader As SqlDataReader = cmd.ExecuteReader 

      'If our reader has one or more rows then read those rows and compare the text 

      If reader.HasRows = True Then 
       reader.Read() 
       Dim ReadUserName As String 
       Dim ReadUserID As String 
       ReadUserID = reader(3) 'This is the first field returned, most likely UserID 
       ReadUserName = reader(1) 'This is the second field returned 

       'If the username and password match then it's a success 
       If comb.Text = reader("Admin").ToString And Txtpass.Text = reader.Item("Password").ToString Then 

        MessageBox.Show("Login successful" & ReadUserName) 

       Else 
        'If they don't match than throw an error 
        MessageBox.Show("Login Failed." & Environment.NewLine & _ 
            "UserName and Password are casesensitive.", _ 
            Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error) 
       End If 
      End If 
     Catch ex As Exception 
      MessageBox.Show(ex.ToString, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error) 
     Finally 
      con.Close() 
     End Try 
    End If 
+0

不確定要理解表的邏輯。什麼是管理員字段?您是否期望具有特定密碼的用戶不止一次出現? – Steve

回答

1

告訴你的代碼讀取一行

If reader.HasRows = True Then 
    reader.Read() 

如果你想從你的SqlDataReader閱讀所有行,則需要在C#(這裏使用一個循環):

while (reader.Read()) 
{ 
    ..... (read the individual values for the current row) ...... 
} 

.Read()調用將返回true,一隻要已經從SqlDataReader中讀取了一行 - 現在從行中獲取數據,存儲或處理數據 - 無論您需要做什麼 - 然後再次調用reader.Read()以檢查是否可以讀取另一行。如果是這樣:重複處理。

如果reader.Read()返回false,那麼所有的行都被讀取完成。

1

如果命令只運行一次代碼並讀取一行。所以使用while循環來讀取所有行

 If reader.HasRows = True Then 
      While reader.Read() 
      Dim ReadUserName As String 
      Dim ReadUserID As String 
      ReadUserID = reader(3) 'This is the first field returned, most likely UserID 
      ReadUserName = reader(1) 'This is the second field returned 

      'If the username and password match then it's a success 
      If comb.Text = reader("Admin").ToString And Txtpass.Text = reader.Item("Password").ToString Then 

       MessageBox.Show("Login successful" & ReadUserName) 

      Else 
       'If they don't match than throw an error 
       MessageBox.Show("Login Failed." & Environment.NewLine & _ 
           "UserName and Password are casesensitive.", _ 
           Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error) 
      End If 

     End While ' I don't know much of vb.net' 
    End If 
+0

@Steve固定,對不起 –

相關問題