vb.net - 我的登录页面有问题。使用 sqlite 数据库
问题描述
以下是我的登录代码:
Private Sub btnlogin_Click(sender As Object, e As EventArgs) Handles btnlogin.Click
cn = New SQLiteConnection
Try
With cm
.Connection = cn
.CommandType = CommandType.Text
.CommandText = "SELECT * FROM UserLogin WHERE USERNAME = @USERNAME And PASSWORD= @PASSWORD"
.Parameters.AddWithValue("@USERNAME", txtUser.Text)
.Parameters.AddWithValue("@PASSWORD", txtPass.Text)
Dim reader = cm.ExecuteReader()
While reader.Read
Home.btnstudent.Enabled = True
Home.btnlis.Enabled = True
Home.btnsubject.Enabled = True
Home.btntrans.Enabled = True
Home.btnmStudent.Enabled = True
Home.btnuser.Enabled = True
MessageBox.Show("You are welcome")
UserValid = True
End While
If UserValid = False Then
MessageBox.Show("sorry, Access denied", "Incorrect Password!")
End If
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
解决方案
不要我们.AddWithValue。请参阅http://www.dbdelta.com/addwithvalue-is-evil/ 和 https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/ 另一个: https ://dba.stackexchange.com/questions/195937/addwithvalue-performance-and-plan-cache-implications
您所需要的只是计数以查看这是否是有效用户。不要不必要地检索数据。
Private Sub btnlogin_Click(sender As Object, e As EventArgs) Handles btnlogin.Click
Dim count As Integer
Using cn = New SQLiteConnection("Your connection string")
Using cm As New SQLiteCommand("SELECT Count(*) FROM UserLogin WHERE USERNAME = @USERNAME And PASSWORD= @PASSWORD", cn)
cm.Parameters.Add("@USERNAME", DbType.String).Value = txtUser.Text
cm.Parameters.Add("@PASSWORD", DbType.String).Value = txtPass.Text
Try
cn.Open()
count = CInt(cm.ExecuteScalar)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Using 'Closes and disposes command
End Using 'Closes and disposes connection
'Do all this AFTER the connection and command are closed and disposed
'Showing a message box while your connection is open can keep the connection open
'until your user gets back from lunch and clicks OK
If count = 1 Then
Home.btnstudent.Enabled = True
Home.btnlis.Enabled = True
Home.btnsubject.Enabled = True
Home.btntrans.Enabled = True
Home.btnmStudent.Enabled = True
Home.btnuser.Enabled = True
MessageBox.Show("You are welcome")
UserValid = True
Else
'Don't tell the user exactly what was wrong with their login.
MessageBox.Show("sorry, Access denied", "Invalid Login")
End If
End Sub
推荐阅读
- oracle - 光标到查询语句
- python-3.x - 从文件中读取会引发 Python 中的 IndexError
- python-3.x - Pandas:保存在两个数据框中具有相同列名的独立文件
- c++ - std::list 中的迭代器感知对象
- java - 重置地图(删除并在下一次放置时重置)密钥到期时的值
- java - 即使成功绘制,从 Firebase 数据库中提取的数据也无法存储在变量中
- python - OSX Catalina - GCC/PIP 编译问题
- ruby-on-rails - Rails 电子邮件 SMTP 验证器 gem 错误率高?
- python - Asyncio - 如何检测另一个任务中的结束任务
- angular - 角度将延迟的服务器响应视为空响应