检查用户名是否已存在于Access数据库中

时间:2014-10-07 20:18:26

标签: database vb.net visual-studio-2010 ms-access username

由于我之前的问题here的帮助,我在我正在建立的应用程序上取得了一些可靠的进展。我正在研究“创建用户”功能。我似乎无法弄清楚的是如何检查用户名是否已经在数据库中。请查看代码,让我知道我做错了什么。显然这只是我需要帮助的部分。

ElseIf UserNameTextBox.Text = "SELECT * FROM Users WHERE Username = '" Then

        MsgBox("Username Already Taken!")
        Dim NEWUSER As New NewUser
        NEWUSER.Show()
        Me.Hide()

3 个答案:

答案 0 :(得分:1)

由于这是一个"如何进行问题",请考虑以下

  • 用户在文本框中输入用户名,然后点击"创建用户"按钮
  • 程序从文本框中获取值,并在数据库上运行查询,检查用户是否已在db中
  • 如果是,您在屏幕上显示错误,或者根据用户输入的用户名建议新的用户名,您验证为新的可能正确的值
  • 如果不是,您创建用户,让他知道他的帐户已创建

在您这样做之前需要您的基本知识:

  • 我如何查询数据库,并使用返回值
  • 如何将用户名添加到该查询以使其安全地进行SQL注入
  • 编程逻辑的基本知识也可能有所帮助(但是,你似乎已经做到了这一点......)

所以最后,归结为:

Dim strUsername As String = usernametextbox.text
Dim boolUsernameExists as Boolean = False

If String.IsNullOrEmpty(strUsername) Then
    ' do not allow empty usernames
    Return
End If

Using dbConnection as OleDbConnection = new OleDbConnection("your-connection-string")
    dbConnection.Open()
    Using dbCommand as OleDbCommand = new OleDbCommand("select count(username) from users where username like ?", dbConnection)
        dbCommand.Parameters.AddWithValue("@p1", strUsername)
        Dim result As Integer = DirectCast(dbCommand.ExecuteScalar(), Integer)
        If result > 0 Then
            boolUsernameExists = true
        End If
    End Using
    dbConnection.Close()
End Using

If boolUsernameExists Then
    ' throw error, or do some other action
    ...
    ' and return
    Return
End If
' If you end up here, your user doesn't exist, and it safe to create the new username

我想指出,这是在没有编辑的情况下编写的,但可能会有一些错误,正如您所指出的那样,这纯粹是一个如何进行的问题

答案 1 :(得分:0)

cn.Open()

    Dim dr As OleDbDataReader
    Dim cmd1 As New OleDbCommand("Select username from login where username= '" & TextBox2.Text & "'", cn)
    dr = cmd1.ExecuteReader
    If dr.Read = True Then
        MsgBox("Please insert another username")
    Else
        If TextBox3.Text = TextBox4.Text Then
            '   cn.Open()
            Dim cmd As New OleDbCommand("insert into login values('" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "')", cn)
            cmd.ExecuteNonQuery()
            MsgBox("sign up Successfully")
            '  cn.Close()
            TextBox2.Clear()
            TextBox3.Clear()
            TextBox4.Clear()
            Form1.Show()
            Me.Hide()
        Else
            MsgBox("Please insert same password & Confirm Password")
        End If

    End If

    cn.Close()

答案 2 :(得分:0)

Imports System.Data.SqlClient
Imports System.Data


Public Class Forgetpassword
    Dim cn As New SqlConnection("Data Source=DESKTOP-UIT47KQ;Initial Catalog=vishakha;Integrated Security=True")
    Dim result As String


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        cn.Open()
        Dim cmd As New SqlCommand("Select answer,passw from login where squestion = '" & Label4.Text & "'", cn)
        Dim dr As SqlDataReader
        dr = cmd.ExecuteReader
        If dr.Read = True Then
            If TextBox1.Text = dr.Item("answer").ToString Then
                Label1.Text = dr.Item("passw").ToString
            Else
                MsgBox("Please Enter Right Answer")
            End If
        Else

        End If
        cn.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        cn.Open()
        Dim cmd As New SqlCommand("select squestion from login where uid1='" & txtusername.Text & "'", cn)
        Dim dr As SqlDataReader
        dr = cmd.ExecuteReader
        If dr.Read = True Then
            Label4.Text = dr.Item("squestion").ToString

        Else
            MsgBox("Please Enter Right Username")
        End If`enter code here`
        cn.Close()
    End Sub



    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Form1.Show()
        Me.Hide()

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click


    End Sub
End Class