在Visual Basic中创建更改密码窗体。如何更新SQL Server数据库?

时间:2017-06-10 04:26:13

标签: sql-server visual-studio-2015

我正在创建一个包含编辑信息的登录注册表。我坚持使用更改密码表单。

这就是我得到的 - 完全丢失它,没有错误但是在测试时崩溃突出了我的读者命令

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    Dim sql As String = "Select Username, Password From tblLog Where Username = @Username"
    Dim cmd As SqlClient.SqlCommand
    cmd = New SqlClient.SqlCommand(sql, con)
    cmd.Parameters.Add("@Username", SqlDbType.VarChar).Value = My.Forms.formEdit.tbUser.Text

    If con.State = ConnectionState.Closed Then
        con.Open()
    End If

    Dim rd As SqlClient.SqlDataReader = cmd.ExecuteReader

    Try
        If rd.Read = False Then
            MessageBox.Show("Incorrect Password")
            tbOldPass.Clear()
            tbNewPass.Clear()
            tbConPass.Clear()
        ElseIf tbConPass.Text <> tbNewPass.Text Then
            MessageBox.Show("Password does not match")
            tbOldPass.Clear()
            tbNewPass.Clear()
            tbConPass.Clear()
        Else
            Dim sqry As String = "Update tblLog Set Password = @Password" &
                "Where Username = @Username And Password = @OldPassword"
            rd.Close()
            Dim scmd As New SqlClient.SqlCommand(sqry, con)

            scmd.Parameters.AddWithValue("@Password", tbNewPass.Text)
            scmd.Parameters.AddWithValue("@Username", My.Forms.formEdit.tbUser.Text)
            scmd.Parameters.AddWithValue("@OldPassword", tbOldPass.Text)
            scmd.ExecuteNonQuery()
            MessageBox.Show("Information Updated")
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
    con.Close()
End Sub

已编辑:稍微更改了代码,它不再崩溃,但我的数据库没有更新

        Dim sql As String = "Select Username, Password From tblLog Where Username = @Username And Password = @Password"
    Dim cmd As New SqlClient.SqlCommand
    cmd = New SqlClient.SqlCommand(sql, con)
    cmd.Parameters.Add("@Password", SqlDbType.VarChar).Value = My.Forms.formEdit.tbPass.Text
    cmd.Parameters.Add("@Username", SqlDbType.VarChar).Value = My.Forms.formEdit.tbUser.Text
    If con.State = ConnectionState.Closed Then
        con.Open()
    End If
    cmd.ExecuteNonQuery()
    con.Close()

    Try
        If tbOldPass.Text <> My.Forms.formEdit.tbPass.Text = False Then
            MessageBox.Show("Incorrect Password")
            tbOldPass.Clear()
            tbNewPass.Clear()
            tbConPass.Clear()
        ElseIf tbConPass.Text <> tbNewPass.Text Then
            MessageBox.Show("Password does not match")
            tbOldPass.Clear()
            tbNewPass.Clear()
            tbConPass.Clear()
        Else
            Dim sqry As String = "Update tblLog Set [Password] = @Pss Where Username = @Use And Password = @OldPassword"
            Dim scmd As New SqlClient.SqlCommand(sqry, con)

            scmd.Parameters.AddWithValue("@Pss", tbNewPass.Text)
            scmd.Parameters.AddWithValue("@Use", My.Forms.formEdit.tbUser.Text)
            scmd.Parameters.AddWithValue("@OldPassword", My.Forms.formEdit.tbPass.Text)

            If con.State = ConnectionState.Closed Then
                con.Open()
            End If
            scmd.ExecuteNonQuery()
            con.Close()
            MessageBox.Show("Information Updated")
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

2 个答案:

答案 0 :(得分:0)

尝试在查询之前添加空格:

Dim sqry As String = "Update tblLog Set Password = @Password" &
                "  Where Username = @Username And Password = @OldPassword"

答案 1 :(得分:0)

稍微调整一下我的代码后,我意识到我正在关闭我的参数值的表格。所以我试着隐藏表格而不是关闭它,然后它起作用了。对不起,麻烦。

编辑: 我应该删除这个问题还是留下?我无法再将自己的答案选为正确的2天