首页 > 解决方案 > 我的登录页面有问题。使用 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

在此处输入图像描述

标签: vb.net

解决方案


不要我们.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

推荐阅读