要在C#中通过变量条件筛选查询SQLite数据库数据表,可以使用SQLiteCommand对象来执行查询语句,并将变量的值作为参数传递给查询语句。

以下是一个示例代码:

using System;
using System.Data.SQLite;

class Program
{
    static void Main()
    {
        string connectionString = "Data Source=database.db;Version=3;";
        using (SQLiteConnection connection = new SQLiteConnection(connectionString))
        {
            connection.Open();

            string name = "John";
            int age = 25;

            string query = "SELECT * FROM users WHERE name = @name AND age = @age";
            using (SQLiteCommand command = new SQLiteCommand(query, connection))
            {
                command.Parameters.AddWithValue("@name", name);
                command.Parameters.AddWithValue("@age", age);

                using (SQLiteDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        string result = String.Format("ID: {0}, Name: {1}, Age: {2}",
                            reader["id"], reader["name"], reader["age"]);
                        Console.WriteLine(result);
                    }
                }
            }
        }
    }
}

在上面的代码中,我们首先创建了一个SQLiteConnection对象并打开了数据库连接。然后,我们定义了一个查询语句,其中的@name和@age是参数占位符。接下来,我们创建了一个SQLiteCommand对象,并使用AddWithValue方法为参数赋值。最后,我们使用ExecuteReader方法执行查询,并使用SQLiteDataReader对象读取返回的结果。

在实际使用中,可以根据需要修改查询语句和参数的名称、类型和值

C#通过变量条件筛选查询SQLite数据库数据表

原文地址: https://www.cveoy.top/t/topic/hYI6 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录