c# json字符串转Oracle的insert into的小程序

using Newtonsoft.Json;

using Newtonsoft.Json.Linq;

cs 复制代码
 /// <summary>
        ///json转inser into 
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        static async Task Main(string[] args)
        {
            string json_rows = @$"
 {{""ID"":1111,""NO"":""NO111"",""CREATE_TIME"":""2024-02-03T20:30:34"",""ALNO"":null}}
 {{""ID"":11112,""NO"":""NO11222"",""CREATE_TIME"":""2024-02-03T20:30:34"",""ALNO"":null}}
 {{""ID"":11113,""NO"":""NO11222"",""CREATE_TIME"":""2024-02-03T20:30:34"",""ALNO"":null}}
";
            string[]  arr= json_rows.Split('\n');

            StringBuilder totsqlSb = new StringBuilder();

            foreach (string json in arr)
            {
                StringBuilder sqlSb = new StringBuilder();
                if (json.Length <= 10) continue;

                JObject jsonObj = JObject.Parse(json);

               
                sqlSb.Append("insert into table_name2 (");
                string columnStr = "";
                foreach (var property in jsonObj.Properties())
                {
                    columnStr += $"{property.Name},";
                }
                columnStr = columnStr.TrimEnd(',');
                sqlSb.Append(columnStr);
                sqlSb.Append(") ");
                sqlSb.Append(" values (");

                //  --  values  ---
                string valStr = "";
                foreach (var property in jsonObj.Properties())
                {
                    valStr += $"{GetValueString(property.Value)},";
                }
                valStr = valStr.TrimEnd(',');
                sqlSb.Append(valStr);

                sqlSb.Append(");\r\n ");
                Console.WriteLine(sqlSb.ToString());
                totsqlSb.Append(sqlSb);
            }

            // 指定要写入的文件路径和文件名
            string filePath = "d:\\sql.txt";

            // 创建StreamWriter对象并打开文件进行写入操作
            using (StreamWriter sw = new StreamWriter(filePath))
            {
                // 写入数据到文本文件中
                sw.WriteLine(totsqlSb); 
            }


            //foreach (var property in jsonObj.Properties())
            //{
            //    //Console.WriteLine($"Key: {property.Name}, Value: {property.Value}");
            //    Console.WriteLine($"Key: {property.Name}, Value: {GetValueString(property.Value)}");
            //}



            Console.ReadKey();
        }

        static string GetValueString(JToken token)
        {
            switch (token.Type)
            {
                case JTokenType.String:
                    return $"'{token.ToString().Replace("'", "''")}'";
                case JTokenType.Integer:
                case JTokenType.Float:
                case JTokenType.Boolean:
                    return token.ToString();
                case JTokenType.Date:
                    return $"to_date('{token.ToString()}','yyyy/MM/dd hh24:mi:ss')";// string.Format("{0:yyyy-MM-dd HH:mm:ss}", token.ToString()) ;
                default:
                    return "null";
            }
        }
相关推荐
好记忆不如烂笔头abc1 分钟前
oracle迁移到sqlserver的注意点
数据库·oracle·sqlserver
雪域迷影7 分钟前
nlohmann::json库对象和json结构体转换的新方式
c++·json·nlohmann_json库
锋君32 分钟前
Orcale数据库在Asp.Net Core环境下使用EF Core 生成实体
数据库·后端·oracle·asp.net
木头软件37 分钟前
批量将 Word 文档重命名为其标题
开发语言·c#·word
ERROR:9938 分钟前
野路子:把海量文档一次性转换成多个PPT
开发语言·人工智能·c#
dingdingfish3 小时前
Oracle 《数据库 2 天开发人员指南》第10章:部署Oracle数据库应用程序
oracle·database·application·developer·deploy·guide
云和恩墨3 小时前
OceanBase企业版会话级SQL跟踪实操:DBMS_MONITOR(类Oracle 10046事件)
数据库·sql·oracle·oceanbase
为什么不问问神奇的海螺呢丶3 小时前
oracle 数据库巡检 sql
数据库·sql·oracle
生产队队长10 小时前
Web:免费的JSON接口
json
文刀竹肃13 小时前
DVWA -SQL Injection-通关教程-完结
前端·数据库·sql·安全·网络安全·oracle