【发布时间】:2017-05-07 04:13:00
【问题描述】:
我无法弄清楚为什么这个函数会导致错误,即使它看起来是正确的。
这并没有错。有什么解决办法吗?
连接字符串或 SQL 相关函数没有问题。我的功能有问题。我使用 SQL Server。
ESEMKABank > Employee:
EmployeeId int IDENTITY(1, 1)
Username nvarchar(64)
Password nvarchar(64)
FullName nvarchar(64)
RoleId int -- <-- FOREIGN KEY
对于 VB.NET:
Imports System.Data.SqlClient
Public Class Form_Login
'Dimensions
Dim theConnection As New SqlConnection
Dim theCommand As New SqlCommand
Dim theDataReader As SqlDataReader
Dim theDataAdapter As New SqlDataAdapter
'Initialization
Private Sub Form_Login_Load(sender As Object, e As EventArgs) Handles Me.Load
theConnection.ConnectionString = "SERVER = MORGAN\SQLEXPRESS; DATABASE = ESEMKABank; INTEGRATED SECURITY = TRUE;"
'Parameterize
theCommand.Parameters.Add("@Username", SqlDbType.VarChar).Value = Me.TextBox_Username.Text
theCommand.Parameters.Add("@Password", SqlDbType.VarChar).Value = Me.TextBox_Password.Text
End Sub
'Login
Private Sub Button_Login_Click(sender As Object, e As EventArgs) Handles Button_Login.Click
If Me.TextBox_Username.Text = "" Or Me.TextBox_Password.Text = "" Then
Me.PictureBox_Wrong.Visible = True
Else
Using theDataReader
'Declaration
theCommand.Connection = theConnection
theCommand.CommandText = "SELECT Username, Password FROM Employee WHERE Username = @Username AND Password = @Password"
theConnection.Open()
theDataReader = theCommand.ExecuteReader()
If theDataReader.Read() Then
'Dispose!
theCommand.Dispose()
theConnection.Close()
SqlConnection.ClearPool(theConnection)
Me.PictureBox_Wrong.Visible = False
Else
'Dispose!
theCommand.Dispose()
theConnection.Close()
SqlConnection.ClearPool(theConnection)
Me.PictureBox_Wrong.Visible = True
End If
End Using
End If
End Sub
End Class
【问题讨论】:
-
您可以尝试将查询替换为“SELECT 1”吗?只是作为一个测试
-
好吧让我试试
-
结果是 = 错误。
标签: sql-server vb.net visual-studio