database
  • vb.net
  • visual-studio-2010
  • ms-access
  • username
  • 2014-10-07 155 views -4 likes 
    -4

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

    ElseIf UserNameTextBox.Text = "SELECT * FROM Users WHERE Username = '" Then 
    
         MsgBox("Username Already Taken!") 
         Dim NEWUSER As New NewUser 
         NEWUSER.Show() 
         Me.Hide() 
    
    +0

    问你自己:“我在比较什么?”那个where子句不完整 – Barranka 2014-10-07 21:05:03

    +0

    我只是重新学习visual studio。即时通讯相当失去从那里去.. – 2014-10-07 21:43:21

    +0

    所以如果你的usernameTextbox等于你的SQL字符串,你会抛出这个错误?你确定你复制/粘贴了正确的代码吗? – Icepickle 2014-10-07 21:46:07

    回答

    1

    由于这是相当一个“怎么办,我-进行-问题”,考虑他在你的文本框的用户名以下

    • 的用户类型,并点击“创建用户”按钮
    • 该程序从文本框中获取值,并在数据库上运行查询,以检查用户是否已经在分区中,如果情况是,在屏幕上显示错误或者建议新用户名,基于用户输入的用户名,您验证的用户名作为一种新的可能正确的值的情况下,
    • 没有,你创建的用户,并让他知道,他的帐户创建得到了

    你这样做之前,我们需要您的基本知识:

    • 我如何查询数据库,并使用返回值
    • 如何添加用户名到该查询,使之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 
    

    我想指出的是,这是没有的编辑写的,可能有一些错误,不过,正如你指出的那样,纯粹是如何对进行-问题

    +0

    这段代码让我头疼。 >没有为数据读取器和整数定义。 – 2014-10-13 19:46:55

    +0

    你可以更清楚一些什么不工作?正如我在我的文章中指定的,我只是键入它虽然它可能是,我没有验证我的vb.net语法 – Icepickle 2014-10-13 22:32:35

    +0

    昏暗的结果由于Integer = DirectCast(dbCommand.ExecuteReader(),Integer)..它给予一个issure关于整数。 – 2014-10-14 03:47:58

    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() 
    
    +0

    其简单的逻辑来检查如何检查用户名是否存在和注册形式................. from ... connection open连接关闭。 – 2017-12-08 06:22:55

    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 
    
    +0

    给工作代码很好,但请包括它的作用的描述。 – Marc 2018-01-19 09:59:14

    相关问题