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

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

 /// <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";
            }
        }

相关推荐

  1. c# json字符串Oracleinsert into程序

    2024-02-07 15:02:03       52 阅读
  2. 字符串、数组

    2024-02-07 15:02:03       28 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-07 15:02:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-07 15:02:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-02-07 15:02:03       82 阅读
  4. Python语言-面向对象

    2024-02-07 15:02:03       91 阅读

热门阅读

  1. LeetCode210. Course Schedule II——拓扑排序

    2024-02-07 15:02:03       46 阅读
  2. 小程序:windows下C++调用打印机过程简介

    2024-02-07 15:02:03       56 阅读
  3. 傅里叶变换在图像处理中的应用

    2024-02-07 15:02:03       54 阅读
  4. C&C++内存泄露和检测

    2024-02-07 15:02:03       49 阅读
  5. LeetCode //C - 875. Koko Eating Bananas

    2024-02-07 15:02:03       49 阅读
  6. C语言---—————— 快速创建指定个数的文件

    2024-02-07 15:02:03       44 阅读
  7. 2 进程(上)

    2024-02-07 15:02:03       48 阅读
  8. LeetCode树总结

    2024-02-07 15:02:03       54 阅读
  9. mysql order by 排序原理

    2024-02-07 15:02:03       41 阅读
  10. 每月AI科研动向(2024年1月)

    2024-02-07 15:02:03       42 阅读
  11. 如何快速上手Vue框架

    2024-02-07 15:02:03       63 阅读
  12. 解决 python CV2 imread读取中文文件名的问题

    2024-02-07 15:02:03       54 阅读
  13. 达梦数据库适配Springboot+MybatisPlus+达梦数据库

    2024-02-07 15:02:03       56 阅读
  14. 渗透测试工具库总结(全网最全)

    2024-02-07 15:02:03       80 阅读
  15. redis相关问题

    2024-02-07 15:02:03       43 阅读