我登錄頁面我的代碼VB.NET代碼:VB.NET SQL Server的SELECT COUNT(*) - 的ExecuteNonQuery:Connection屬性尚未初始化
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
Try
If UsernameTextBox.Text = "" Then
MsgBox("Insert your username.")
UsernameTextBox.Focus()
Return
ElseIf PasswordTextBox.Text = "" Then
MsgBox("Insert your Passwprd.")
PasswordTextBox.Focus()
Return
Else
Dim count As Integer
Using con As New OleDbConnection("Provider=SQLOLEDB;Data Source=JUNIOR-PC\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=ShopHereNow")
con.Open()
Dim command = New OleDbCommand("Select count(*) from Employees where FirstName = '" & UsernameTextBox.Text & "' and [Password] = '" & PasswordTextBox.Text & "'")
count = command.ExecuteNonQuery()
con.Close()
If count > 0 Then
MsgBox("Welcome " & UsernameLabel.Text & "!")
Me.Hide()
Home.Show()
Else
MessageBox.Show("Invalid combination. Try again...")
Cancel.PerformClick()
Return
End If
End Using
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "ERROR5", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
在執行此代碼我得到錯誤閱讀:ExecuteNonQuery: Connection property has not been initialized
請幫助,我可以在哪裏編碼錯誤?
在調用查詢之前,您需要將命令鏈接到您的連接,例如'command.Connection = con'。 – TZHX