0

我在 C# 表单应用程序中使用 Sqlite 作为我选择的数据库,并带有http://sqlite.phxsoftware.com/ System.Data.SQLite 提供程序。我正在尝试实现搜索功能,但效果不佳......或者我错过了一些东西。

我正在使用的简化 sql 如下所示:

SELECT *
FROM Table
WHERE column LIKE @boundParameter ESCAPE '!'

当我运行此程序时,在带有参数的任何排列中(使用 ? 或 ?001 或 :boundParameter 或 @boundParameter),它会给我一个 FormatException:“输入字符串的格式不正确。” 我找不到任何说我不能在 LIKE 中使用参数的东西。有人知道吗?我需要以其他方式吗?

4

4 回答 4

2

我建议尝试这样的事情:

"SELECT * FROM [Table] WHERE [column] LIKE @boundParameter ESCAPE @escape";

接着:

command.Parameters.AddWithValue("@boundParameter", parameter));
command.Parameters.AddWithValue("@escape", "!");

Parameters.AddWithValue 是添加绑定参数的 SQLite 方式,而不必每次都声明一个新参数。

@Noah(对不起,还不能评论)

斯蒂芬詹宁斯是对的,你不必引用你所绑定的价值。

于 2009-04-16T07:11:21.200 回答
0

"Input string was not in a correct format" is not an error message returned by any version of SQLite

It must be being returned by the wrapper. SO ... I am going to guess that you are using the ADO.NET 2.0 Provider from sqlite.phxsoftware.com

You must remember to quote the value you are binding to the parameter.

For example, if you use

command.Parameters.Add(new SQLiteParameter("@boundParameter", _pattern));

then _pattern = "'test'" and not "test"

于 2009-04-16T05:21:46.003 回答
0

如何连接和添加参数?

我没有太多使用 SQLite,但以下应该可以工作;

SQLiteCommand command = _yourConnection.CreateCommand();
command.CommandType = CommandType.Text;
command.CommandText = "SELECT * FROM Table WHERE column LIKE @boundParameter";
command.Parameters.Add(new SQLiteParameter("@boundParameter", _yourSearchCriteria));
...
于 2009-03-21T23:19:25.050 回答
0

该程序代码执行一个查询,其中包括一个步骤中的参数替换和模式拟合。在这里,字符串变量 myNamePattern 是我们要为其查找客户的字符串,因此所有返回的客户都将包含变量 myNameattern 字符串。我有同样的问题,但我解决了!这是将字符串模式(也是参数)替换为 SQLiteCommand.CommandText 的完美方式:

SQLiteCommand command = conn.CreateCommand(); command.CommandText = "select * from Customer where name like @myStringParameter"; command.Parameters.Add("myStringParameter", System.Data.DbType.String).Value = "%"+ myNamePattern+ "%";

于 2009-05-12T23:36:51.073 回答