将Json数据从URL导入到SQL Server

问题描述

我有一个.Json URL,其中包含Json格式数据。

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然后创建命令行应用程序可能是一个更好的解决方案?)或指导我朝着正确的方向前进。

解决方法

您可以尝试以下代码从json txt中获取值并将其传输到sql服务器表中。

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

最终结果:

enter image description here

编辑:

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