Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 参数化查询不';不工作,没有错误_C#_Sqlite - Fatal编程技术网

C# 参数化查询不';不工作,没有错误

C# 参数化查询不';不工作,没有错误,c#,sqlite,C#,Sqlite,为什么下面的查询不返回任何结果?它没有错误 SQLiteCommand sqliteCommand = new SQLiteCommand("SELECT * FROM " + table + " WHERE @COL LIKE @searchKey", DataBaseConnnection); sqliteCommand.Parameters.Add("@searchKey", DbType.String).Value = SearchKeyWord; sqliteCommand.Param

为什么下面的查询不返回任何结果?它没有错误

SQLiteCommand sqliteCommand = new SQLiteCommand("SELECT * FROM " + table + " WHERE @COL LIKE @searchKey", DataBaseConnnection);
sqliteCommand.Parameters.Add("@searchKey", DbType.String).Value = SearchKeyWord;
sqliteCommand.Parameters.Add("@COL", DbType.String).Value = COLString;

如果您试图将列名作为参数传入,则不能这样做。该查询将
@COL
参数的文本值与
@searchKey
模式进行比较。这是完全合法的语法,但不是你想要的工作生活

我想你想要的是:

SQLiteCommand sqliteCommand = new SQLiteCommand("SELECT * FROM " + table 
                                              + " WHERE " + COLString 
                                              + " LIKE @searchKey", DataBaseConnnection);
sqliteCommand.Parameters.Add("@searchKey", DbType.String).Value = SearchKeyWord;

然而,只有当您完全控制可以传入的字符串时,才应该这样做,否则您会受到SQL注入攻击

这就是我试图将其作为参数传递的原因,我想我应该先过滤掉字符串。一个简单的问题,是否可以选择任何列?*似乎不起作用。据我所知并非如此——我不是SQLite专家,但我所知道的其他系统不允许通配符列选择。