数据库中的字符串设置为公共字符串

时间:2013-04-19 15:32:57

标签: mysql vb.net visual-studio-2010

好的,从上一个问题的答案来看,推理仍然适用于此,但这次是一个不同的问题。我为我正在创建的启动器获得了一个登录系统(Loginvb.vb),并且想知道两件事:

  1. 是否有更好的方法对数据库进行登录检查(如 更安全)(登录样式将具有基于Web的注册 通过PHP脚本设置)?
  2. 有没有办法在数据库中取一个特定的列(标记为访问)并放入它 作为一个公共字符串,所以我可以检查它是否等于1 2或3 标记为Main.vb的不同表格
  3. 这是当前的登录检查:

    Public Sub login_Click(sender As Object, e As EventArgs) Handles login.Click
        If txtuserName.Text = "" Or txtpassWord.Text = "" Then
            MsgBox("You cannot progress until you login....(moron =p)")
        Else
            'Connects To the Database 
            Dim connect As MySqlConnection
            connect = New MySqlConnection()
            connect.ConnectionString = "server=127.0.0.1;user id=sc;Password=derp;database=sclaunch" 'not the actual login ;)
            Try
                connect.Open()
            Catch myerror As MySqlException
                MsgBox("Error Connecting to Database. Please Try again !")
            End Try
            'SQL Query To Get The Details
            Dim myAdapter As New MySqlDataAdapter
            Dim sqlquerry = "Select * From login where username = '" + txtuserName.Text + "' And password= '" + txtpassWord.Text + "'"
            Dim myCommand As New MySqlCommand()
            'My fail attempt at what I am trying to do :(
            Dim sql22 As MySqlConnection
            sql22 = New MySqlConnection()
            sql22.ConnectionString = "Select * From login where access ="
            'End of fail attempt
            myCommand.Connection = connect
            myCommand.CommandText = sqlquerry
            'Starting The Query
            myAdapter.SelectCommand = myCommand
            Dim mydata As MySqlDataReader
            mydata = myCommand.ExecuteReader
            'To check the Username and password and to validate the login 
            If mydata.HasRows = 0 Then
                MsgBox("Invalid Login")
            Else
                'fail testing xD
                Label3.Text = sql22
                MsgBox("You are now Loged In!")
    
            End If
        End If
    End Sub
    

    仍然基本上学习越来越多,因为我编码所有这一切都喜欢试验和错误以及你遇到困难的时刻= /(对不起管理员或其他任何修复标签问题仍然是网站xD新的)

1 个答案:

答案 0 :(得分:0)

假设包含凭据的同一个表login还包含您要检索的access列,那么我已经更改了很多代码

    Dim sqlquerry = "Select * From login where username = @name AND password=@pwd"
    Dim myCommand As New MySqlCommand(sqlquery, connect)
    myCommand.Parameters.AddWithValue("@name", txtuserName.Text)
    myCommand.Parameters.AddWithValue("@pwd", txtpassWord.Text)
    Dim mydata = myCommand.ExecuteReader

    If mydata.HasRows = False Then
        MsgBox("Invalid Login")
    Else
        ' the same record that contains the credentials contains the access field'
        mydata.Read()
        Label3.Text = mydata("access").ToString()
        MsgBox("You are now Loged In!")
    End If

我改变了什么:

  • 删除字符串连接并添加适当的参数
  • 删除了myAdapter以及每次引用(不需要,你没有 填写DataTable / DataSet)
  • 删除了sql22及其每个引用。这是一个连接和你 尝试像命令一样使用
  • 修正了对HasRows的检查(返回布尔值而非整数。是吗? 使用Option Strict Off?)