如何向Sqllite命令添加参数 [英] How to add parameters to a Sqllitecommand

查看:32
本文介绍了如何向Sqllite命令添加参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i'm using vs2008 and SQLLite.Net, when i using these code:

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);

它有效.但是当我使用参数时,返回空值:

it works. but when i using parameters ,return 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"));

推荐答案

Ref: SQLite 理解的 SQL

:AAAA 冒号后跟标识符名称为命名的名称为:AAAA 的参数.命名参数也有编号.

:AAAA A colon followed by an identifier name holds a spot for a named parameter with the name :AAAA. Named parameters are also numbered.

您的查询应为:

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

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

Add parameters without @ symbol. Check the following code snippet.

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.

这篇关于如何向Sqllite命令添加参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆