我正在尝试在VB.NET中创建一个SQL登录表单,但它并没有真正起作用,表单正在加载,但是当我单击登录时,它只会崩溃并出现异常。有人能告诉我如何解决这个问题吗?
代码:
Imports System.Data.SqlClient
Public Class Form1
Private Sub BTN_LOGIN_Click(sender As Object, e As EventArgs) Handles BTN_LOGIN.Click
Dim connection As New SqlConnection("Server= (localdb)\MSSQLLocalDB; Database = TestDB; Integrated Security = true")
Dim command As New SqlCommand("select * from Table where Username = @username and Password = @password", connection)
Dim reg As New SqlCommand("INSERT INTO Table (username, password) VALUES (@username, @password)")
command.Parameters.Add("@username", SqlDbType.VarChar).Value = TextBoxUsername.Text
command.Parameters.Add("@password", SqlDbType.VarChar).Value = TextBoxPassword.Text
Dim adapter As New SqlDataAdapter(command)
Dim data As New DataTable()
adapter.Fill(data)
adapter.Dispose()
If data.Rows.Count() <= 0 Then
MessageBox.Show("Username Or Password Are Invalid")
Else
MessageBox.Show("Login Successfully")
Dim frm As New Form2()
Me.Hide()
frm.Show()
End If
End Sub
Private Sub BTN_CANCEL_Click(sender As Object, e As EventArgs) Handles BTN_CANCEL.Click
Me.Close()
End Sub
End Class
表也很好。只是不要谈论表
答案 0 :(得分:3)
异常说明:“关键字表附近的语法不正确”。表格的名称是Table
吗?这是一个保留关键字。如果是名称,您应该考虑更改它。但是如果你必须使用这个名称,你需要将它嵌入方形支持:
Dim command As New SqlCommand("SELECT * FROM [Table] where Username = @username and Password = @password", connection)