SQL,VB.Net |我可以使用什么查询获取此值?

时间:2015-11-07 19:59:04

标签: mysql vb.net

enter image description here

在上面我想让它给我与之相关的“用户名”的“ID”值。

这是我试过的命令

Dim Query2 = String.Format("SELECT ID FROM account WHERE character = " & UsernameTextBox.Text & " ")
            Command = New MySqlCommand(Query2, mydbcon)
            Dim idtest = reader.Read.ToString
            MsgBox(idtest)

然而它回归“假”我做错了什么?

以下是我正在处理的整个代码:

Try
        mydbcon.Open()
        Dim Query As String
        Query = String.Format("SELECT * FROM account WHERE username = '{0}' AND password = '{1}'", Me.UsernameTextBox.Text.Trim(), Me.PasswordTextBox.Text.Trim())
        Dim Command = New MySqlCommand(Query, mydbcon)
        reader = Command.ExecuteReader

        Dim count As Integer
        count = 0
        While reader.Read
            count = count + 1
        End While

        If count = 1 Then
            MessageBox.Show("Username and password are correct")
            Query = String.Format("SELECT username AS ID FROM account WHERE character = " & UsernameTextBox.Text & " ")
            Command = New MySqlCommand(Query, mydbcon)

            My.Settings.LoginID = reader.Read
            MsgBox(My.Settings.LoginID)
        ElseIf count > 1 Then
            MessageBox.Show("Username and password are duplicate")
        Else
            MessageBox.Show("Username and password are wrong")
        End If
        mydbcon.Close()
    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        mydbcon.Dispose()
    End Try

1 个答案:

答案 0 :(得分:0)

使用ID作为别名(所以select username AS anyNameYouWantToAssignToIt):

Dim Query2 = String.Format("SELECT username AS ID FROM account WHERE character = " & UsernameTextBox.Text & " ")