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";
            }
        }
相关推荐
Stackflowed2 小时前
Docker安装Oracle
docker·oracle·容器
zxbmmmmmmmmm4 小时前
在 Avalonia 中编写高性能动画
c#·xaml·avalonia·compositon
宸凉6 小时前
Oracle 19C的安装
数据库·oracle
AC赳赳老秦6 小时前
数据安全合规:OpenClaw 敏感信息脱敏、操作日志审计、权限精细化管控方案,符合等保要求
网络·数据库·python·安全·web安全·oracle·openclaw
加号36 小时前
【C#】 HTTP 请求通讯实现指南
开发语言·http·c#
原来是猿7 小时前
JSON 序列化与反序列化 —— 用 Jsoncpp 打造自己的网络协议
网络·网络协议·json
步步为营DotNet7 小时前
.NET 11 中 Microsoft.Extensions.AI 在智能后端推理与决策优化的应用
云原生·c#·.net
TEC_INO7 小时前
Linux56:读取人脸图片并把特征值保存到sqlite3数据库
数据库·oracle
鸽芷咕7 小时前
KingbaseES中的PL_SQL编程:存储过程、函数、触发器与包的开发指南
数据库·sql·oracle