c# sqlite 批量生成insert语句的函数

函数开始

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Text;

public class SqliteHelper
{
    public static List<string> GenerateInsertStatements(string tableName, List<string> columns, List<List<object>> data)
    {
        List<string> insertStatements = new List<string>();

        foreach (var row in data)
        {
            if (row.Count != columns.Count)
            {
                throw new ArgumentException("The number of columns and data items in a row must match.");
            }

            StringBuilder sb = new StringBuilder();
            sb.Append($"INSERT INTO {tableName} (");

            // Add column names
            sb.Append(string.Join(", ", columns));

            sb.Append(") VALUES (");

            // Add values
            for (int i = 0; i < row.Count; i++)
            {
                if (row[i] is string)
                {
                    sb.Append($"'{row[i]}'");
                }
                else if (row[i] is DateTime)
                {
                    sb.Append($"'{((DateTime)row[i]).ToString("yyyy-MM-dd HH:mm:ss")}'");
                }
                else
                {
                    sb.Append(row[i]);
                }

                if (i < row.Count - 1)
                {
                    sb.Append(", ");
                }
            }

            sb.Append(");");

            insertStatements.Add(sb.ToString());
        }

        return insertStatements;
    }
}

调用方式

csharp 复制代码
class Program
{
    static void Main()
    {
        string tableName = "Users";
        List<string> columns = new List<string> { "Id", "Name", "Age", "CreatedAt" };
        List<List<object>> data = new List<List<object>>
        {
            new List<object> { 1, "Alice", 25, DateTime.Now },
            new List<object> { 2, "Bob", 30, DateTime.Now },
            new List<object> { 3, "Charlie", 35, DateTime.Now }
        };

        List<string> insertStatements = SqliteHelper.GenerateInsertStatements(tableName, columns, data);

        foreach (var sql in insertStatements)
        {
            Console.WriteLine(sql);
        }
    }
}

输出

csharp 复制代码
INSERT INTO Users (Id, Name, Age, CreatedAt) VALUES (1, 'Alice', 25, '2023-10-05 12:34:56');
INSERT INTO Users (Id, Name, Age, CreatedAt) VALUES (2, 'Bob', 30, '2023-10-05 12:34:56');
INSERT INTO Users (Id, Name, Age, CreatedAt) VALUES (3, 'Charlie', 35, '2023-10-05 12:34:56');
相关推荐
初级代码游戏8 分钟前
套路化编程 C# winform 自适应缩放布局
开发语言·c#·winform·自动布局·自动缩放
大空大地20261 小时前
流程控制语句--switch多分支语句使用、while循环语句的使用、do...while语句、for循环
c#
玄同7653 小时前
SQLite + LLM:大模型应用落地的轻量级数据存储方案
jvm·数据库·人工智能·python·语言模型·sqlite·知识图谱
kylezhao20193 小时前
C#序列化与反序列化详细讲解与应用
c#
JQLvopkk3 小时前
C# 实践AI :Visual Studio + VSCode 组合方案
人工智能·c#·visual studio
故事不长丨3 小时前
C#线程同步:lock、Monitor、Mutex原理+用法+实战全解析
开发语言·算法·c#
kingwebo'sZone3 小时前
C#使用Aspose.Words把 word转成图片
前端·c#·word
大空大地20264 小时前
表达式与运算符
c#
向上的车轮5 小时前
为什么.NET(C#)转 Java 开发时常常在“吐槽”Java:checked exception
java·c#·.net
ggabb5 小时前
中文的精确与意境,从来都不是英文能比肩的
sqlite