【问题标题】:Import Json data from URL to SQL server将 Json 数据从 URL 导入 SQL Server
【发布时间】:2021-02-09 18:49:30
【问题描述】:

我有一个包含 Json 格式数据的 .Json URL。

HTTPS://XXXX/fetch-transaction?fromdate=2020-10-13&toDate=2020-10-20(只是一个例子

[
    {
        "orderId": 110,
        "consignmentNumber": "TEST",
        "itemNumbers": [
            "TEST"
        ],
        "country": "UK",
        "orderType": "ORDER_HOME_DELIVERY",
        "paymentTransactionId": "395611",
        "priceInOre": 5900,
        "paidAt": "2020-10-16 10:51:08",
        "orderNumber": "7000067718",
        "articleName": "SOUTH-2"
    }
]

我想将数据插入到 SQL server 表中,想知道是否可以直接在这里使用 SQL server 和 t-SQL 还是应该使用 VS 和 C#?

如果 C# 是首选,有人可以指导我如何完成它吗? 我在 Visual Studio 中创建了一个控制台应用程序(但是,使用 els 来创建命令行应用程序可能是更好的解决方案?)或引导我朝着正确的方向前进。

【问题讨论】:

    标签: c# json visual-studio tsql sql-server-2017


    【解决方案1】:

    你可以试试下面的代码,从json txt中获取值,传到sql server表中。

    using Newtonsoft.Json;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.IO;
    using System.Linq;
    class Program
        {
            static void Main(string[] args)
            {
                string json = File.ReadAllText("D:\\test1.txt");
                List<Example> list = JsonConvert.DeserializeObject<List<Example>>(json);
                string strcon = @"Connstr";
                SqlConnection connection = new SqlConnection(strcon);
                connection.Open();
                string sql = "Insert into JsonData(orderId,consignmentNumber,itemNumbers,country,orderType,paymentTransactionId,priceInOre,paidAt,orderNumber,articleName) values(@orderId,@consignmentNumber,@itemNumbers,@country,@orderType,@paymentTransactionId,@priceInOre,@paidAt,@orderNumber,@articleName)";
                SqlCommand command = new SqlCommand(sql, connection);
                foreach (Example item in list)
                {
                    command.Parameters.AddWithValue("@orderId", item.orderId);
                    command.Parameters.AddWithValue("@consignmentNumber", item.consignmentNumber);
                    command.Parameters.AddWithValue("@itemNumbers", item.itemNumbers.First());
                    command.Parameters.AddWithValue("@country", item.country);
                    command.Parameters.AddWithValue("@orderType", item.orderType);
                    command.Parameters.AddWithValue("@paidAt", item.paidAt);
                    command.Parameters.AddWithValue("@paymentTransactionId", item.paymentTransactionId);
                    command.Parameters.AddWithValue("@priceInOre", item.priceInOre);
                    command.Parameters.AddWithValue("@articleName", item.articleName);
                    command.Parameters.AddWithValue("@orderNumber", item.orderNumber);
                }
                command.ExecuteNonQuery();
                connection.Close();
          
                
    
            }
        }
        public class Example
        {
            public int orderId { get; set; }
            public string consignmentNumber { get; set; }
            public List<string> itemNumbers { get; set; }
            public string country { get; set; }
            public string orderType { get; set; }
            public string paymentTransactionId { get; set; }
            public int priceInOre { get; set; }
            public string paidAt { get; set; }
            public string orderNumber { get; set; }
            public string articleName { get; set; }
        }
    

    最终结果:

    编辑:

    var json = new WebClient().DownloadString("URL");
    

    【讨论】:

    • 这很好,但是第一个问题是 URL,当我单击链接时,它会提示我另存为.... 在您的代码上,我看不到可以保存文件的部分首先在我的本地桌面上作为 .json。
    • @Nils,因为我没有具体的 url 来测试,所以我使用了你提供的 json txt。如果可以的话,可以直接访问下载的文件,使用上面的代码。
    • @Nils,我找到了从 URL 获取 json 字符串的方法。我已经在我的答案中对其进行了编辑。
    • 太棒了!我想我应该用 var json... 替换字符串 json...?
    • @Nils,你需要安装 nuget-package Newtonsoft.Json。另外,我在答案中添加了 using 语句。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    • 2015-08-26
    • 2012-03-11
    • 2014-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多