1

mysqlデータベースから読み取るコードがいくつかありますが、これを変更してユーザーがテーブルに存在するかどうかを確認するにはどうすればよいか疑問に思っていました。

ありがとう

    Private Sub GetDBData()
    Try
        'prepare connection query 
        strQuery = "SELECT users.Username, users.Password " & _
        "FROM users " & _
        "WHERE Username='User'"
        SQLCmd = New MySqlCommand(strQuery, dbCon)
        'open db and start query
        dbCon.Open()
        DR = SQLCmd.ExecuteReader
        While DR.Read
            MysqlData.Text = MysqlData.Text & DR.Item("Username") & Space(10) & DR.Item("Password") & vbCrLf
        End While
        'done so closing db
        DR.Close()
        dbCon.Close()

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub
4

6 に答える 6

2

簡単な方法は、次のようなクエリを作成することです。

SELECT COUNT(*) FROM users WHERE Username='user123';

あなたはそれを実行し、それが返す値を取り戻します.0の場合、ユーザーは存在しません. 1 の場合は存在し、1 より大きい場合は何か問題があります (同じユーザー名を持つ複数のユーザーがいます)。

于 2012-08-07T22:16:23.237 に答える
2

私のVBはかなり錆びていますが、その要点は次のとおりです。

Private Sub GetDBData()
Try
    'prepare connection query 
    strQuery = "SELECT users.Username, users.Password " & _
    "FROM users " & _
    "WHERE Username='User'"
    SQLCmd = New MySqlCommand(strQuery, dbCon)
    'open db and start query
    dbCon.Open()
    DR = SQLCmd.ExecuteReader

    If DR.HasRows Then

        While DR.Read
            MysqlData.Text = MysqlData.Text & DR.Item("Username") & Space(10) & DR.Item("Password") & vbCrLf
        End While
    Else
        'COMMENT: Your user didn't exist
    End If

    'done so closing db

    'COMMENT: move to a finally() section and check objects are not null before closing
    DR.Close()
    dbCon.Close()

Catch ex As Exception
    MessageBox.Show(ex.Message)
End Try

サブ終了

于 2012-08-07T22:27:29.027 に答える
2

commandwithを使用しparametersて、SQL インジェクションを回避します。比較を行ってユーザー名の存在を確認するだけの場合の戦略は、ブール値を返す関数を作成することです。以下は、ニーズに基づいたコード例です。

Private Function IsUserExist(userName as string) AS Boolean

        Dim returnValue as boolean = false

        strQuery = "SELECT COUNT(*)"
        strQuery &= "FROM users "
        strQuery &= "WHERE Username = @xUserName "

        Using xConn as new MySQLCnnection("connectionStringHere")
            Using xComm as New MySQLCommand()
                With xComm
                    .Connection = xConn
                    .CommandText = strQuery
                    .CommandType = CommandType.Text
                    .Parameters.AddWithValue("@xUserName", userName)
                End With
                Try
                    xConn.Open()
                    If CInt(xComm.ExecuteScalar()) > 0 Then
                        returnValue = true
                    End If
                Catch ex as MySQlException
                    MsgBox(ex.Message)
                    returnValue = false
                Finally
                    xConn.Close
                End Try
            End Using
        End Using

        return returnValue
End Sub
于 2012-08-07T22:43:34.200 に答える
2

修正しますか?それはあまりにも間違っています。ブロックの使用、例外の飲み込み、SQL インジェクション攻撃の可能性はありません。

のようなもの(私はVBをやっていませんが、基本的な考え方は健全です)

Private Function UserExists(argUser As string) As Bool
  strQuery = "SELECT Username FROM users WHERE Username=?User"
  Using SQLcmd = New MySqlCommand(strQuery, dbCon)
    SQLCmd.Parameters.Add("?User",argUser)  
    dbCon.Open()
    Using reader = SQLCmd.ExecuteReader()
      return reader.Read()
    End Using
  End Using
End Function

それが私であれば、現在どこにいても(使用中のブロックで)接続を取得するのではなく、接続をインスタンス化することになります。

于 2012-08-07T22:43:50.077 に答える
0
Protected Sub btnlogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnlogin.Click
    Dim myAdapter As New MySqlDataAdapter
    Dim myCommand As New MySqlCommand
    Dim myData As MySqlDataReader
    Dim conn As MySqlConnection = New MySqlConnection("Data Source=localhost;Database=name;User ID=root;Password=pwd;")

    Dim loginstring As String = "SELECT uname,password,type FROM logindetails WHERE uname = '" + txtuname.Text + "' AND password = '" + txtpwd.Text + "' "
    Try
        conn.Open()
    Catch myerror As MySqlException 'If it fails do this... (i.e. no internet connection, etc.)
        MsgBox("Error connecting to database. Check your internet connection.", MsgBoxStyle.Critical)
    End Try

    myCommand.Connection = conn
    myCommand.CommandText = loginstring
    myAdapter.SelectCommand = myCommand
    myData = myCommand.ExecuteReader

    If myData.HasRows = 0 Then
        MsgBox("Invalid Credentials", MsgBoxStyle.Critical)
    Else
        Response.Redirect("Adminhome.aspx")
        MsgBox("Logged in as " & txtuname.Text & ".", MsgBoxStyle.Information)
    End If
    conn.Close()
End Sub
于 2012-12-21T23:21:13.040 に答える
0

その非常に単純な...

while ループに入る前に、整数を宣言してゼロに設定します。データの取得を開始する前の while ループで、その整数を 1 に設定します。while の外側で catch の前に if ステートメントを配置して、整数がまだゼロの場合に通知します。これは、db にデータがないことを意味します。このようなもの...

Private Sub GetDBData()
    Try
        'prepare connection query 
        strQuery = "SELECT users.Username, users.Password " & _
        "FROM users " & _
        "WHERE Username='User'"
        SQLCmd = New MySqlCommand(strQuery, dbCon)
    'open db and start query
    dbCon.Open()
        DR = SQLCmd.ExecuteReader
        Dim x As Integer = 0
        While DR.Read
        x = 1
        MysqlData.Text = MysqlData.Text & DR.Item("Username") & Space(10) & DR.Item("Password") & vbCrLf
    End While
    'done so closing db

    DR.Close()
    dbCon.Close()

   if x = 0
        Messagebox.Show("Record not found")
   End If
Catch ex As Exception
    MessageBox.Show(ex.Message)
End Try

サブ終了

于 2019-12-04T15:46:59.190 に答える