1,首先安装System.Data.SQLite库。

在这里插入图片描述
方法不只有这一个

2,创建一个名为DatabaseManager的类

在DatabaseManager类中引入以下命名空间:

using System.Data;
using System.Data.SQLite;

在DatabaseManager类中添加一个私有字段,用于存储数据库连接字符串:

private string connectionString = "Data Source=your_database_path;Version=3;";

在DatabaseManager类中添加一个方法,用于打开数据库连接

private SQLiteConnection OpenConnection()
{
    SQLiteConnection connection = new SQLiteConnection(connectionString);
    connection.Open();
    return connection;
}

在DatabaseManager类中添加增删改查方法:

public void AddData(string tableName, Dictionary<string, object> data)
{
    using (SQLiteConnection connection = OpenConnection())
    {
        string columns = string.Join(", ", data.Keys);
        string values = string.Join(", ", data.Values.Select(v => $"'{v}'"));
        string query = $"INSERT INTO {tableName} ({columns}) VALUES ({values});";
        using (SQLiteCommand command = new SQLiteCommand(query, connection))
        {
            command.ExecuteNonQuery();
        }
    }
}

public void UpdateData(string tableName, Dictionary<string, object> data, string condition)
{
    using (SQLiteConnection connection = OpenConnection())
    {
        string setClause = string.Join(", ", data.Select(kvp => $"{kvp.Key} = '{kvp.Value}'"));
        string query = $"UPDATE {tableName} SET {setClause} WHERE {condition};";
        using (SQLiteCommand command = new SQLiteCommand(query, connection))
        {
            command.ExecuteNonQuery();
        }
    }
}

public void DeleteData(string tableName, string condition)
{
    using (SQLiteConnection connection = OpenConnection())
    {
        string query = $"DELETE FROM {tableName} WHERE {condition};";
        using (SQLiteCommand command = new SQLiteCommand(query, connection))
        {
            command.ExecuteNonQuery();
        }
    }
}

public DataTable SelectData(string tableName, string condition)
{
    using (SQLiteConnection connection = OpenConnection())
    {
        string query = $"SELECT * FROM {tableName} WHERE {condition};";
        using (SQLiteCommand command = new SQLiteCommand(query, connection))
        {
            SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);
            DataTable dataTable = new DataTable();
            adapter.Fill(dataTable);
            return dataTable;
        }
    }
}

在其他窗口中,创建DatabaseManager类的实例,并调用相应的增删改查方法。例如:

DatabaseManager dbManager = new DatabaseManager();
dbManager.AddData("your_table_name", new Dictionary<string, object> { { "column1", value1 }, { "column2", value2 } });

完整代码(加注释):

using System.Data; // 引入System.Data命名空间,用于处理数据表和数据行
using System.Data.SQLite; // 引入System.Data.SQLite命名空间,用于处理SQLite数据库

public class DatabaseManager
{
    private string connectionString = "Data Source=your_database_path;Version=3;"; // 定义数据库连接字符串

    // 打开数据库连接的方法
    private SQLiteConnection OpenConnection()
    {
        SQLiteConnection connection = new SQLiteConnection(connectionString); // 创建SQLiteConnection对象
        connection.Open(); // 打开数据库连接
        return connection; // 返回数据库连接对象
    }

    // 添加数据的方法
    public void AddData(string tableName, Dictionary<string, object> data)
    {
        using (SQLiteConnection connection = OpenConnection()) // 使用using语句确保连接被正确关闭
        {
            string columns = string.Join(", ", data.Keys); // 将字典中的键(列名)拼接成字符串
            string values = string.Join(", ", data.Values.Select(v => $"'{v}'")); // 将字典中的值拼接成字符串,用单引号包围
            string query = $"INSERT INTO {tableName} ({columns}) VALUES ({values});"; // 构造插入数据的SQL语句
            using (SQLiteCommand command = new SQLiteCommand(query, connection)) // 创建SQLiteCommand对象
            {
                command.ExecuteNonQuery(); // 执行SQL语句,不返回结果集
            }
        }
    }

    // 更新数据的方法
    public void UpdateData(string tableName, Dictionary<string, object> data, string condition)
    {
        using (SQLiteConnection connection = OpenConnection()) // 使用using语句确保连接被正确关闭
        {
            string setClause = string.Join(", ", data.Select(kvp => $"{kvp.Key} = '{kvp.Value}'")); // 将字典中的键值对拼接成SET子句
            string query = $"UPDATE {tableName} SET {setClause} WHERE {condition};"; // 构造更新数据的SQL语句
            using (SQLiteCommand command = new SQLiteCommand(query, connection)) // 创建SQLiteCommand对象
            {
                command.ExecuteNonQuery(); // 执行SQL语句,不返回结果集
            }
        }
    }

    // 删除数据的方法
    public void DeleteData(string tableName, string condition)
    {
        using (SQLiteConnection connection = OpenConnection()) // 使用using语句确保连接被正确关闭
        {
            string query = $"DELETE FROM {tableName} WHERE {condition};"; // 构造删除数据的SQL语句
            using (SQLiteCommand command = new SQLiteCommand(query, connection)) // 创建SQLiteCommand对象
            {
                command.ExecuteNonQuery(); // 执行SQL语句,不返回结果集
            }
        }
    }

    // 查询数据的方法
    public DataTable SelectData(string tableName, string condition)
    {
        using (SQLiteConnection connection = OpenConnection()) // 使用using语句确保连接被正确关闭
        {
            string query = $"SELECT * FROM {tableName} WHERE {condition};"; // 构造查询数据的SQL语句
            using (SQLiteCommand command = new SQLiteCommand(query, connection)) // 创建SQLiteCommand对象
            {
                SQLiteDataAdapter adapter = new SQLiteDataAdapter(command); // 创建SQLiteDataAdapter对象,用于填充数据表
                DataTable dataTable = new DataTable(); // 创建数据表对象
                adapter.Fill(dataTable); // 填充数据表
                return dataTable; // 返回数据表对象
            }
        }
    }
}

注意:

请将your_database_path替换为你的SQLite数据库文件路径,将your_table_name替换为你要操作的表名,将column1、column2等替换为实际的列名,将value1、value2等替换为实际的值。

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐