简单的MySQL登录 - 需要帮助

时间:2016-07-02 14:05:24

标签: mysql vb.net

我有一个登录系统,带有一个网站的远程mysql服务器。

我有下面的系统工作正常:

mydbcon = New MySqlConnection
        mydbcon.ConnectionString = "server=server.com; userid=nathand2_portico; password=password; database=nathan_database"
        Dim reader As MySqlDataReader

    Try
        mydbcon.Open()
        Dim Query As String
        Query = String.Format("SELECT * FROM info WHERE username = '{0}' AND password = '{1}'", Me.Username.Text.Trim(), Me.Password.Text.Trim())
        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
            Panel2.Visible = True
            Panel2.BringToFront()
            Timer1.Start()

但是,虽然以上工作正常 - 我想添加另一部分。在我的数据库中,我有一个名为'active'的字段,只希望它们能够在字段'active'等于1时登录。 / p>

但是,我不知道该怎么做。你能帮我吗?

2 个答案:

答案 0 :(得分:1)

只需更新您的查询

SELECT * FROM info WHERE username ='{0}'AND password ='{1}'and active = 1

答案 1 :(得分:0)

您可以将查询构建语句更改为如下所示。同样,'方法中的占位符周围不需要string.Format()

    Query = String.Format("SELECT * FROM info WHERE username = {0} AND password = {1} AND active = 1", Me.Username.Text.Trim(), Me.Password.Text.Trim())

此外,您只是想检查用户是否是现有用户,在这种情况下可以将您的查询更改为

   Query = String.Format("SELECT COUNT(*) FROM info WHERE username = {0} AND password = {1} AND active = 1", Me.Username.Text.Trim(), Me.Password.Text.Trim())

并使用ExecuteScalar()方法代替ExecuteReader()