1

我正在尝试学习使用带有 MYSQL 的 VB 程序进行注册。

我是新手,最近知道如何连接到 mysql 并注册。

现在的问题是......我修改了太多代码,我不能再注册了。

我想做以下事情:

1.只能注册一个唯一的用户名(输入相同的用户名显示错误)-------2. 用户名长度>5,密码长度>6-----3。显示注册完成

我只是……迷路了……需要很多帮助。谢谢你。

Private Sub btnReg_Click(sender As Object, e As EventArgs) Handles btnReg.Click
    Dim myAdapter As New MySqlDataAdapter
    Dim myCommand As New MySqlCommand
    Dim myData As MySqlDataReader
    Dim add As String
    Dim connect As MySqlConnection
    connect = New MySqlConnection()

    connect.ConnectionString = "server=localhost;user id=jackie588727;password=jackie1218;database=root"



    Dim usercheck As String = "SELECT Username FROM User WHERE Username = '" + txtUser.Text + "'"
    Try
        connect.Open()
    Catch myerror As MySqlException
        MsgBox("Error connecting to database. Check your internet connection.", MsgBoxStyle.Critical)
    End Try



    myCommand.Connection = connect
    myCommand.CommandText = usercheck
    myAdapter.SelectCommand = myCommand
    myData = myCommand.ExecuteReader


    If myData.HasRows = 0 Then
        add = "INSERT INTO user (Username, Password) VALUES('" + txtUser.Text + "','" + txtPass.Text + "')"
    Else
        MsgBox(txtUser.Text & " is being register, try again.", MsgBoxStyle.Information)
    End If
    connect.Close()


End Sub
4

2 回答 2

1

换行If myData.HasRows = 0 Then

如下

If not myData.HasRows Then

HasRows 属性将返回 true 或 false,如果为 true 则表示用户已经注册,您可以显示消息框,否则您可以插入。

于 2013-07-30T04:05:05.263 回答
0

我认为你应该试试这个:

    conn = New MySqlConnection("server=localhost;user id=jackie588727;password=jackie1218;database=root")
    Dim username As Boolean = True
    conn.Open()
    Dim sqlquery As String = "SELECT * FROM TABLENAMEHERE WHERE Username = '" & TextBox1.Text & "';"
    Dim data As MySqlDataReader
    Dim adapter As New MySqlDataAdapter
    Dim command As New MySqlCommand
    command.CommandText = sqlquery
    command.Connection = conn
    adapter.SelectCommand = command
    data = command.ExecuteReader
    While data.Read()
        If data.HasRows() = True Then
            MsgBox("Username Already in use!")
            username = False
            data.Close()
        Else
            data.Close()
            username = True
        End If
    End While
    data.Close()
    If username = True Then
        Dim sqlquery2 As String = "INSERT INTO NewTable (Username, Password, Wcoins)VALUES ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & "10" & "')"
        Dim data2 As MySqlDataReader
        Dim adapter2 As New MySqlDataAdapter
        Dim command2 As New MySqlCommand
        command2.CommandText = sqlquery2
        command2.Connection = conn
        adapter2.SelectCommand = command2
        data2 = command2.ExecuteReader
        MsgBox("You've Registered!")
        MsgBox("Welcome " + TextBox1.Text)
        My.Settings.Name = TextBox1.Text
        My.Settings.Save()
        Member.Show()
        Me.Close()
    End If

如果有问题请纠正我

于 2013-09-14T11:11:20.707 回答