2012-03-22 52 views
1

我我,当我使用这些代码使用VS2008和SQLLite.Net, :如何参数添加到Sqllitecommand

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][email protected] and [password][email protected]"; 
cmd.Parameters.Clear(); 
cmd.Parameters.Add(new System.Data.SQLite.SQLiteParameter("@name", "admin88")); 
cmd.Parameters.Add(new System.Data.SQLite.SQLiteParameter("@pass", "123456")); 

回答

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

+0

感谢Niranjan Kala.that很有帮助! – 2012-03-22 05:40:24