VB.NET DataTable行

时间:2018-11-28 17:01:42

标签: sql-server database vb.net login

我正在尝试创建一个登录表单。

我已经在服务器上创建了一个数据库,并创建了用户名和密码行。 然后,我以root用户作为密码创建了一个root用户。

但是我在检查用户名和密码是否正确时遇到问题, 我不知道如何给他两行。

Dim conn = New SqlConnection("Data Source=SRV-SQL;Initial Catalog=prova;User ID=user;Password=user")
Dim sda = New SqlDataAdapter("select count(*) from tblLogin where username ='" + txtUsername.Text + "' and password='" + txtUserPwd.Text + "'", conn)
Dim dt = New DataTable()
sda.Fill(dt)
If (dt.Rows().ToString() = "1") Then
    MsgBox("Logged-in successfully")
Else
    MessageBox.Show("The username or the password is wrong!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If

表格:

table

3 个答案:

答案 0 :(得分:0)

在线发表评论和解释。

Private Sub VerifyLogin()
        'For the Return Value of the command
        Dim RetVal As Integer
        ' A Using...End Using will ensure that you connectionis closed and disposed event
        'it there is an error.
        Using conn = New SqlConnection("Data Source=SRV-SQL;Initial Catalog=prova;User ID=user;Password=user")
            'You don't need a DataAdapter, just a command
            'USE PARAMETERS. Yes, I am yelling :-) Even if you are the only user
            'it will save you headaches with syntax.
            Using cmd = New SqlCommand("select count(*) from tblLogin where username = @UserName and password= @Password;", conn)
                cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = txtUsername.Text
                cmd.Parameters.Add("@Password", SqlDbType.VarChar).Value = txtUserPwd.Text
                'You are only returning one row
                'ExecuteScalar returns the value in the first column of the 
                'first row of the the data
                conn.Open()
                RetVal = CInt(cmd.ExecuteScalar)
            End Using
        End Using
        'No need to convert to a string just compare the Integer
        If RetVal = 1 Then
            MsgBox("Logged-in successfully")
        Else
            MessageBox.Show("The username or the password is wrong!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
End Sub

答案 1 :(得分:0)

Private Function CalculateHash(password As String, salt As String) As String
    'TODO:
    ' Suggest pulling the BCrypt from the NuGet gallery for this:
    ' https://www.nuget.org/packages/BCrypt-Official/
    ' Just remember that bcyrpt lib encodes salt as part of the password hash, so the function signatures and db table will be different.
End Function

Public Function CheckCredentials(UserName As String, Password As String) As Boolean
    Using conn As New SqlConnection("Data Source=SRV-SQL;Initial Catalog=prova;User ID=user;Password=user"), _
          ' Need to add a "Salt" column to your table, create a new random salt for each user when you create the user
          cmd As New SqlCommand("SELECT Salt, PwdHash FROM tblLogin WHERE username = @Username", conn)

        'Parameterized queries or NOTHING. String concatention is NOT OKAY here
        cmd.Parameters.Add("@UserName", SqlDbType.NVarChar, 50).Value = UserName

        conn.Open()   
        Using rdr As SqlDataReader = cmd.ExecuteReader()
            If Not rdr.Read() Then Return False

            Dim Salt As String = rdr("Salt")
            Dim PwdHash As String = rdr("PwdHash")

            'Compare HASHES, not Passwords
            Return PwdHash = CalculateHash(Password, Salt As String)
        End Using
    End Using
End Function

If CheckCredentials(txtUsername.Text, txtUserPwd.Text) Then
    MsgBox("Logged-in successfully")
Else
    MessageBox.Show("The username or the password is wrong!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If

答案 2 :(得分:0)

改为使用DataReader,使用此代码,只需在登录按钮或其他内容中调用CheckLogin。

import javafx.geometry.Pos
import javafx.scene.control.{Label, ProgressBar}
import javafx.scene.layout.StackPane

class JavaFXGhostProgressBar extends StackPane {

    this.setAlignment(Pos.CENTER)

    val bar = new ProgressBar()
    bar.setMaxWidth(Double.MaxValue)
    val text = new Label()
    text.textProperty().setValue("PERFORMANCE TESTING")
    text.idProperty().setValue("OutlineProgressBarText")

    this.getChildren.addAll(bar, text)
}