通过 SSIS 脚本任务将 API 的响应插入到 SQL 服务器 table

Insert the response from the API in to SQL server table through SSIS script task

我正在尝试创建一个调用 REST API 并将响应插入 Azure SQL 服务器 table 的 SSIS 包。为此,我正在使用脚本任务和 ADO.NET 连接管理器。 API 响应采用 JSON 格式,如下所示

[{"id":1,"name":"AX BD","description":"D","shippingFreeze":false,"receivingFreeze":false,"mouseNorovirus":false,"irradiatedFeed":true,"createdAt":"2022-02-24T10:03:50.09","lastUpdated":"2022-02-24T10:03:50.09"},
 {"id":2,"name":"AX PD","description":"B","shippingFreeze":false,"receivingFreeze":false,"mouseNorovirus":false,"irradiatedFeed":false,"createdAt":"2022-02-24T10:03:50.09","lastUpdated":"2022-02-24T10:03:50.09"}]

及以下脚本任务中我尝试调用 REST API 并将响应转换为 JSON,但我不确定如何遍历每个 [=27] =] 记录并插入它们,将 JSON 中的值映射到 sqlCmd.Parameters.AddWithValue

   public void Main()
    {
        try
        {
            string serviceUrl = Dts.Variables["$Project::ServiceUrl"].Value.ToString();
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(serviceUrl);
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));
            string APIUrl = string.Format(serviceUrl + "/rooms");
            var response = client.GetAsync(APIUrl).Result;
            if (response.IsSuccessStatusCode)
            {
                var result = response.Content.ReadAsStringAsync().Result;
                dynamic res_JSON = JsonConvert.DeserializeObject(result);

                ConnectionManager cm = Dts.Connections["SurplusMouse_ADONET"];
                var sqlConn = (System.Data.SqlClient.SqlConnection)cm.AcquireConnection(Dts.Transaction);

                using (var sqlCmd = new System.Data.SqlClient.SqlCommand(
                "INSERT INTO [dbo].[RM_Room]([ROOMID],[NAME],[DESCRIPTION],[SHIPPING_FREEZE],[RECEIVING_FREEZE],[MOUSE_NOROVIRUS],[IRRADIATED_FEED])" +
                        "VALUES(@ROOMID,@NAME,@DESCRIPTION,@SHIPPING_FREEZE,@RECEIVING_FREEZE,@MOUSE_NOROVIRUS,@IRRADIATED_FEED,)", sqlConn))
                { 
                      sqlCmd.CommandType = CommandType.Text;
                      sqlCmd.Parameters.AddWithValue("@ROOMID", xxxxx);
                      .................. 


                    int rowsAffected = sqlCmd.ExecuteNonQuery();
                }
                cm.ReleaseConnection(sqlConn);
           }
        }
        catch (Exception ex)
        {
            Dts.TaskResult = (int)ScriptResults.Failure;
        }
    }

这是我第一个使用 SSIS 脚本和调用 API 的项目,非常感谢任何帮助

您可以迭代 res_JSON 如下片段示例代码:

dynamic res_JSON = JsonConvert.DeserializeObject(result);
foreach (var jsonObj in res_JSON)
{
    //Clear Parameter of sqlCmd
    sqlCmd.CommandType = CommandType.Text;
    sqlCmd.Parameters.AddWithValue("@ROOMID", jsonObj.id);
    sqlCmd.Parameters.AddWithValue("@NAME", jsonObj.name);
    sqlCmd.Parameters.AddWithValue("@DESCRIPTION", jsonObj.description);
    //You can access another props of jsonObj also
    int rowsAffected = sqlCmd.ExecuteNonQuery();
}

或者为了可读性,最好有等价的 class 你 json 比如:

public class JsonObj
{
    public int id { get; set; }
    public string name { get; set; }
    public string description { get; set; }
    public bool shippingFreeze { get; set; }
    public bool receivingFreeze { get; set; }
    public bool mouseNorovirus { get; set; }
    public bool irradiatedFeed { get; set; }
    public DateTime createdAt { get; set; }
    public DateTime lastUpdated { get; set; }
}

deserialize result 作为 JsonObj 的数组,例如:

var res_JSON = JsonConvert.DeserializeObject<JsonObj[]>(result);
foreach (var obj in res_JSON)
{
    //obj.id
    //obj.name
    //...
}