如何将参数添加到Sqllite命令

时间:2012-03-22 03:27:57

标签: .net sqlite

我正在使用vs2008和SQLite.Net, 当我使用这些代码时:

String sel = "select * from admins where [name]='admin88' and [password]='123456'";
System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection();
conn.ConnectionString = Config.connStr;
conn.Open();

System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand();


cmd.CommandText = sel;
cmd.CommandType = CommandType.Text;

cmd.Connection = conn;

DataSet set = new DataSet();
System.Data.SQLite.SQLiteDataAdapter adp = new System.Data.SQLite.SQLiteDataAdapter() ;
adp.SelectCommand = cmd;
adp.Fill(set);
adp.Dispose();

Response.Write(set.Tables.count);

它有效。 但是当我使用参数时,返回null:

String sel = "select * from admins where [name]=@name and [password]=@pass";
cmd.Parameters.Clear();
cmd.Parameters.Add(new System.Data.SQLite.SQLiteParameter("@name", "admin88"));
cmd.Parameters.Add(new System.Data.SQLite.SQLiteParameter("@pass", "123456"));

1 个答案:

答案 0 :(得分:1)

参考:SQL As Understood By SQLite

  

:AAAA后跟标识符名称的冒号包含命名的点   参数名称:AAAA。命名参数也已编号。

您的查询应为:

String sel = "select * from admins where [name]= :name and [password]= :pass";

添加不带@符号的参数。请检查以下代码段。

SQLiteParameter[] myParams = new SQLiteParameter[]  
    { 
      new SQLiteParameter("DeptNo", 10), 
      new SQLiteParameter("DName", "COUNTING") 
    }; 
  SQLiteConnection sqConnection1 = new SQLiteConnection("DataSource=mydatabase.db"); 
  CreateCommand(sqConnection1,"UPDATE Dept SET DName = :DName WHERE DeptNo = :DeptNo",myParams); 

参考this