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";
            }
        }
相关推荐
数据的世界015 小时前
使用Avalonia UI实现DataGrid
c#
hihaojie6 小时前
异常的使用
c#·总结
powershell 与 api7 小时前
C#,shell32 + 调用控制面板项(.Cpl)实现“新建快捷方式对话框”(全网首发)
开发语言·windows·c#·.net
Kelvin_Ngan10 小时前
C#从XmlDocument提取完整字符串
c#
iqay12 小时前
【C语言】填空题/程序填空题1
c语言·开发语言·数据结构·c++·算法·c#
中游鱼19 小时前
C# 数组和列表的基本知识及 LINQ 查询
c#·linq·数组·数据处理·list数列
32码奴20 小时前
C#基础知识
开发语言·c#
来恩10031 天前
C# 类与对象详解
开发语言·c#
想做富婆1 天前
oracle: 多表查询之联合查询[交集intersect, 并集union,差集minus]
数据库·oracle·联合查询
Dr.勿忘1 天前
C#面试常考随笔8:using关键字有哪些用法?
开发语言·unity·面试·c#·游戏引擎