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";
            }
        }
相关推荐
其实防守也摸鱼6 小时前
Fastjson 反序列化漏洞(JNDI注入)
android·数据库·安全·oracle·自动化·fastjson·反序列化
淡海水7 小时前
01-03-认知篇-C#内存模型深度解析
unity·c#·游戏引擎·.net·gc
星河耀银海7 小时前
数据解析:AI返回JSON数据在HTML5中的渲染方法
人工智能·json·html5
lzhdim12 小时前
C#不为人知的10个魔法特性:资深开发者也会震惊的底层奥秘
java·前端·javascript·算法·c#
三84413 小时前
Fastjson 漏洞 · 01 · 认识 Fastjson 与序列化基础
json·fastjson·反序列化
ClouGence14 小时前
开源数据库管理工具 CloudDM 4.3.0 发布,支持 SQL 收藏,多数据源语法支持大幅增强
数据库·oracle·sql server
前端 贾公子15 小时前
LangChain核心组件 == 提示词(Prompts)
开发语言·c#
一直在努力的小宁15 小时前
【阅读笔记】具身操作的数采方案概览
后端·json·restful·具身智能·vla·vlm
码艺-Alimjan18 小时前
Web 网站打包桌面应用的另一种方式,超级简单(C# exe 33Kb)
开发语言·前端·c#
Leo.yuan18 小时前
2026企业级数据集成平台市场报告:国内外厂商格局与技术路线
数据库·oracle