如何将变量分配给查询结果

时间:2011-04-24 05:48:17

标签: vb.net sqlite

如何将查询结果分配给变量。

我正在使用SQLlite。以下是从用户表中检索密码的代码。我需要比较给定的密码和给定的密码。

    Dim i As String
    Dim p As String
    i = txtUserID.Text
    p = txtPassword.Text

    Try
        Dim sqlConnection As New SQLite.SQLiteConnection()
        Dim sqlCommand As New SQLiteCommand("", sqlConnection)
        Dim sqlPath As String = "Data Source=" & Application.StartupPath & "\Database\SimpleDB.db3"
        Dim sqlQuery As String = "SELECT Password FROM User WHERE UserID LIKE '" & i & "'"
        sqlConnection.ConnectionString = sqlPath
        sqlConnection.Open()
        sqlCommand.CommandText = sqlQuery
        sqlCommand.ExecuteNonQuery()
        sqlConnection.Close()
    Catch ex As Exception
        MsgBox("Invalid ID or Password. Please try again.")
    End Try

修改1

在下面的代码中,为什么我需要使用GetInt16(0)。为什么16和参数中为什么为0。 GetInt32(),GetInt16()和GetString()

之间有什么区别
DbDataReader reader = command.ExecuteReader();

while (reader.Read())
{
    int id=  reader.GetInt16(0);
}
reader.Close();

1 个答案:

答案 0 :(得分:2)

首先,您正在使用sqlCommand.ExecuteNonQuery() - 这只会返回受影响的行数。

使用其他方法 - 返回结果的方法,例如:

sqlCommand.ExecuteReader()

您需要将此返回值分配给DbDataReader类型的变量(或者更确切地说是从其继承的SqlDataReader)。

这将为您提供SqlDataReader,您可以迭代以检索值。

相关问题