我可以在sqlite查询中使用LIMIT条件的参数

时间:2014-07-12 03:46:20

标签: c# winforms sqlite sql-limit

关于我正在为C#应用程序工作的SQLite查询的快速提问:

我可以使用参数在查询中设置“LIMIT”值吗?例如我想这样做:

SQLiteCommand cmd = new SQLiteCommand("SELECT FROM ... WHERE ... LIMIT @items");
cmd.Parameters.Add(new SQLiteParameter("items", numberofItems));
这是一件事吗?或者有同等的?如果可以的话,我希望能够以编程方式设置LIMIT值。

我试着用谷歌搜索这个问题一段时间,但我没有想出任何东西,所以也许你们都可以提供帮助。非常感谢!

1 个答案:

答案 0 :(得分:3)

是的,这很有效。如果您不确定它是否有效,请不要害怕测试某些东西。你没有提到你遇到的问题。这是一个工作示例

SQLiteConnectionStringBuilder builder = new SQLiteConnectionStringBuilder();
builder.DataSource = "test.db";

SQLiteConnection connection = new SQLiteConnection(builder.ConnectionString);
using (connection.Open())
{
    SQLiteCommand command = new SQLiteCommand("select * from people limit @limitNum", connection);
    command.Parameters.Add(new SQLiteParameter("limitNum", 2));
    SQLiteDataReader reader = command.ExecuteReader();

    while (reader.Read())
    {
        Console.WriteLine(reader.GetValue(0));
    }
}