为什么每行更新一次?

时间:2017-02-21 16:15:23

标签: database vb.net

我试图建立一个具有重置密码功能的登录系统,每当我更新帐户时,它都会更新所有帐户而不是指定的帐户。我所做的是我验证表单1上的用户名并更新表单2中的帐户。

表格1

            Dim ctr As Integer
    ctr = ctr + 1
    If ctr >= 2 Then
        MessageBox.Show("Account already verified")
        Exit Sub
    End If
    Dim q2 As String = "SELECT * FROM tblusers WHERE username = @username "
    con = New SqlConnection
    con.ConnectionString = constring
    con.Open()
    Dim command As New SqlCommand(q2, con)
    command.Parameters.Add("@username", SqlDbType.NChar).Value = TextBox1.Text
    user = TextBox1.Text
    Dim ad As New SqlDataAdapter(command)
    ad = New SqlDataAdapter(command)
    Dim ds As New DataSet
    ds = New DataSet
    ad.Fill(ds)
    If ds.Tables(0).Rows.Count = 0 Then
        MessageBox.Show("Account Not Found")
        Exit Sub
    End If
    sq = ds.Tables(0).Rows(0).Item("SecurityQuestion")
    ans = ds.Tables(0).Rows(0).Item("Answer")
    con.Close()
    ctr = 0
    If tries < 5 Then
        Dim myValue As Object
        myValue = InputBox(sq, "Security Question", "", 500, 225)
        If myValue = "" Then
            MessageBox.Show("Please Enter An Answer")
            Exit Sub
        End If
        ans = ans.Replace(" ", "")
        If ans = myValue Then
            Me.Hide()
            Reset.Show()
            TextBox1.Text = ""
        Else
            tries = tries + 1
            MessageBox.Show("Incorrect!")
            Exit Sub
        End If
    Else
        MessageBox.Show("Maximum Limit Of Tries Reached. Try Again Later")
    End If

表格2

           Dim q As String = "UPDATE tblusers SET username =  @username,password = @password              
    Dim q2 As String = "SELECT * FROM tblusers WHERE username = @username"
    con = New SqlConnection
    con.ConnectionString = constring
    If (TextBox1.Text.Length > 0) Then
        Try
            con.Open()
            Dim command As New SqlCommand(q2, con)
            command.Parameters.Add("@username", SqlDbType.NChar).Value = TextBox1.Text
            Dim ad As New SqlDataAdapter(command)
            ad = New SqlDataAdapter(command)
            Dim table As New DataTable
            ad.Fill(table)
            If table.Rows.Count() = 1 Then
                MessageBox.Show("Username Is Unavailable")
                Exit Sub
            End If
            Dim command2 As New SqlCommand(q, con)
            command2.Parameters.AddWithValue("@username", TextBox1.Text)
            command2.Parameters.AddWithValue("@password", TextBox2.Text)
            Dim adap As New SqlDataAdapter
            adap = New SqlDataAdapter(command2)
            Dim rowsAffected As Integer = command2.ExecuteNonQuery()
            MessageBox.Show("Values updated")
            con.Close()
        Catch ex As SqlException
            MessageBox.Show(ex.Message)

        Finally
            con.Dispose()
        End Try
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        Me.Hide()
        Login.Show()

0 个答案:

没有答案