这是我尝试从登录表单中打开的代码 这是说当阅读器关闭时无效尝试调用读取
Imports System.Data.SqlClient
Public Class Form40
Private Sub Form40_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = users
'Dim cmd As New SqlCommand
Dim user_Name As String
Dim main_menu1, main_menu2 As String
If (con.State = ConnectionState.Closed) Then
Dim cmd As New SqlCommand(" SELECT User_name, main_menu1,main_menu2 FROM users WHERE (User_name = '" & TextBox1.Text & "' )")
con.Open()
' cmd.Connection = con
' cmd.CommandText = " SELECT User_name, main_menu1,main_menu2 FROM users WHERE (User_name = '" & TextBox1.Text & "' )"
Dim ddr As SqlDataReader = cmd.ExecuteReader()
If ddr.HasRows Then
While ddr.Read()
main_menu1 = ddr("main_menu1").ToString()
main_menu2 = ddr("main_menu2").ToString()
user_Name = ddr("User_Name").ToString()
If user_Name = TextBox1.Text And main_menu1 = "True" Then
TextBox2.Visible = False
End If
End While
End If
End If
con.Close()
End Sub
End Class
这是表单登录的代码
Imports System.Data
Imports System.Data.SqlClient
Public Class LoginForm2
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
ConnectToSQL()
End Sub
Private Sub ConnectToSQL()
Dim Pass_word As String
Dim Pass_word2 As String
Dim user_Name As String
Try
Dim cmd As New SqlCommand(" SELECT User_name, Pass_word FROM users WHERE (User_name = '" & UsernameTextBox.Text & "' ) AND (Pass_word = '" & PasswordTextBox.Text & "')", con)
con.Open()
Dim lrd As SqlDataReader = cmd.ExecuteReader()
If lrd.HasRows Then
While lrd.Read()
'Do something here
Pass_word = lrd("Pass_word").ToString()
user_Name = lrd("User_Name").ToString()
Pass_word2 = PasswordTextBox.Text()
If Pass_word = Pass_word2 And user_Name = UsernameTextBox.Text Then
MessageBox.Show("Logged in successfully as " & user_Name, "", MessageBoxButtons.OK, MessageBoxIcon.Information)
users = user_Name
Me.Hide()
Form40.Show()
'Clear all fields
PasswordTextBox.Text = ""
UsernameTextBox.Text = ""
End If
End While
Else
MessageBox.Show("Username and Password do not match..", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
'Clear all fields
PasswordTextBox.Text = ""
UsernameTextBox.Text = ""
End If
lrd.Close()
Catch ex As Exception
MessageBox.Show("Error while connecting to SQL Server." & ex.Message)
Finally
con.Close()
End Try
End Sub
Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
Me.Close()
End Sub
End Class