c# sqlite导出导入数据表 作为sql文件

csharp 复制代码
// 创建一个 SQLiteConnection 对象
using (var connection = new SQLiteConnection("Data Source=mydatabase.sqlite"))
{
    // 打开数据库连接
    connection.Open();

    // 查询所有表名
    var tableNames = new List<string>();
    var tablesCommand = new SQLiteCommand("SELECT name FROM sqlite_master WHERE type='table'", connection);
    using (var tablesReader = tablesCommand.ExecuteReader())
    {
        while (tablesReader.Read())
        {
            tableNames.Add(tablesReader.GetString(0));
        }
    }

    // 遍历所有表,生成导出 SQL
    var exportSql = new StringWriter();
    foreach (var tableName in tableNames)
    {
        // 查询表结构
        var schemaCommand = new SQLiteCommand($"PRAGMA table_info({tableName})", connection);
        var schemaReader = schemaCommand.ExecuteReader();

        // 生成 CREATE TABLE 语句
        exportSql.WriteLine($"-- Table: {tableName}");
        exportSql.Write("CREATE TABLE ");
        exportSql.Write(tableName);
        exportSql.Write(" (");
        bool firstColumn = true;
        while (schemaReader.Read())
        {
            string columnName = schemaReader.GetString(1);
            string columnType = schemaReader.GetString(2);
            bool notNull = !schemaReader.IsDBNull(3) && schemaReader.GetInt32(3) != 0;
            bool primaryKey = !schemaReader.IsDBNull(5) && schemaReader.GetInt32(5) != 0;

            if (!firstColumn)
            {
                exportSql.Write(",");
            }
            exportSql.Write(columnName);
            exportSql.Write(" ");
            exportSql.Write(columnType);
            if (notNull)
            {
                exportSql.Write(" NOT NULL");
            }
            if (primaryKey)
            {
                exportSql.Write(" PRIMARY KEY");
            }
            firstColumn = false;
        }
        exportSql.Write(");");
        exportSql.WriteLine();

        // 查询表数据
        var dataCommand = new SQLiteCommand($"SELECT * FROM {tableName}", connection);
        using (var dataReader = dataCommand.ExecuteReader())
        {
            // 生成 INSERT 语句
            while (dataReader.Read())
            {
                exportSql.Write($"INSERT INTO {tableName} VALUES (");
                bool firstValue = true;
                for (int i = 0; i < dataReader.FieldCount; i++)
                {
                    if (!firstValue)
                    {
                        exportSql.Write(",");
                    }
                    object value = dataReader.GetValue(i);
                    if (value == null)
                    {
                        exportSql.Write("NULL");
                    }
                    else if (value is string)
                    {
                        exportSql.Write($"'{value.ToString().Replace("'", "''")}'");
                    }
                    else if (value is DateTime)
                    {
                        exportSql.Write($"'{((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss.fff")}'");
                    }
                    else
                    {
                        exportSql.Write(value.ToString());
                    }
                    firstValue = false;
                }
                exportSql.Write(");");
                exportSql.WriteLine();
            }
        }
        exportSql.WriteLine();
    }

    // 将导出 SQL 写入到文件
    File.WriteAllText("export.sql", exportSql.ToString());
}
csharp 复制代码
public void ImportSqlFile(string sqlFilePath, string databaseFilePath)
{
    // 读取 SQL 文件内容
    string sql = File.ReadAllText(sqlFilePath);

    // 创建一个新的 SQLiteConnection 对象
    using (var connection = new SQLiteConnection($"Data Source={databaseFilePath}"))
    {
        // 打开数据库连接
        connection.Open();

        // 创建一个 SQLiteCommand 对象,并执行 SQL 文件中的 SQL 语句
        using (var command = new SQLiteCommand(sql, connection))
        {
            command.ExecuteNonQuery();
        }
    }

    Console.WriteLine("SQL 文件导入成功!");
}
相关推荐
大飞pkz1 天前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
鸿乃江边鸟1 天前
向量化和列式存储
大数据·sql·向量化
唐青枫1 天前
从入门到进阶:C#.NET Stopwatch 计时与性能测量全攻略
c#·.net
未来之窗软件服务1 天前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟
1uther1 天前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎
阿幸软件杂货间1 天前
Office转PDF转换器v1.0.py
开发语言·pdf·c#
sali-tec1 天前
C# 基于halcon的视觉工作流-章34-环状测量
开发语言·图像处理·算法·计算机视觉·c#
Tiger_shl1 天前
【层面一】C#语言基础和核心语法-02(反射/委托/事件)
开发语言·c#
懒虫虫~2 天前
通过内存去重替换SQL中distinct,优化SQL查询效率
java·sql·慢sql治理
逛逛GitHub2 天前
1 个神级智能问数工具,刚开源就 1500 Star 了。
sql·github