我的登录页面有问题。使用SQLite数据库

时间:2019-07-03 00:43:26

标签: vb.net

以下是我的登录代码:

[1]: https://codepen.io/kimsanka/pen/OeZLZy

enter image description here

1 个答案:

答案 0 :(得分:0)

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