2011-04-24 64 views
1

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

我正在使用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(); 

回答

2

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

使用不同的方法 - 这将返回的结果,例如:

sqlCommand.ExecuteReader() 

您需要的这个返回的值赋给DbDataReader类型的变量(或者更确切地说,从它继承了SqlDataReader )。

这会给你一个SqlDataReader,您可以迭代以检索值。

+0

先生,您能否向我提供DataReader示例代码 – 2011-04-24 05:57:48

+0

@Failed_Noob - 先生,您如何遵循我提供的链接,阅读文档(有示例),并自己学习? – Oded 2011-04-24 05:58:48

+0

对不起,没有看到链接 – 2011-04-24 06:04:33