0

关于我正在为 C# 应用程序处理的 SQLite 查询的快速问题:

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

SQLiteCommand cmd = new SQLiteCommand("SELECT FROM ... WHERE ... LIMIT @items");
cmd.Parameters.Add(new SQLiteParameter("items", numberofItems));

这是一回事吗?或者有没有等价物?如果可以的话,我希望能够以编程方式设置 LIMIT 值。

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

4

1 回答 1

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));
    }
}
于 2014-07-12T04:11:39.333 回答