question

kashnala-5222 avatar image
0 Votes"
kashnala-5222 asked KB-8828 edited

Write C# Code to get the data from Rest API using SSIS Script Task

Hello All,
I am a SQL Developer, and I don't have a good knowledge on C# Code.
Can you please help me on how to write the code in getting the data by calling a Rest API and storing the data in a SQL Server table?

Below is the URL:
curl-X GET-H "Content-Type:application/json"-u <username>:<password> "https://agent-pos-na1.fourth.com/CompanyID/controllers/vertx/hotschedules/getConcepts"

Below are the output columns and its datatypes.

Key Type Description
extId Number Concept external ID
name String Concept Name

FYI... I Have the CompanyID( It is a parameter), I have the UserName and Password details also with me.
My plan is to keep the URL, parameters, Credentials in the Variables and use them in the C# Code.

I tried to see the results by calling the API using Postman and I am able to achieve it.

Thank you so much in advance.

sql-server-integration-servicespartner-center-api
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

COZYROC avatar image
0 Votes"
COZYROC Suspended answered COZYROC Suspended commented

I would recommend you check the commercial COZYROC SSIS+ library. It includes superior REST integration technology where you can build your custom configurations to connect to any REST API.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Wanted to try using Script task only.

0 Votes 0 ·
COZYROC avatar image COZYROC kashnala-5222 ·

The Script Task requires programming skills and it is not simple to use. You said you don't have C# knowledge.

0 Votes 0 ·
kashnala-5222 avatar image
1 Vote"
kashnala-5222 answered KB-8828 edited

Was able to achieve it.
Below is the code for reference to others.

         try
         {
             string connetionString = null;
             string sql = null;
             var serializer = new JavaScriptSerializer();

             string serviceUrl = "APIURL";
         HttpClient client = new HttpClient();
         client.BaseAddress = new Uri(serviceUrl);



         var byteArray = Encoding.ASCII.GetBytes("UserName:Password");
         client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
         connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName; Trusted_Connection=True;";
         string APIUrl = "/controllers/vertx/hotschedules/getConcepts";
         var response = client.GetAsync(APIUrl).Result;
         if (response.IsSuccessStatusCode)
         {
             var result = response.Content.ReadAsStringAsync().Result;

                 var dt = serializer.Deserialize<Dictionary<string,

                   string>[]>(result);

                 using (SqlConnection cnn = new SqlConnection(connetionString))

                 {

                     cnn.Open();

                     sql = "insert into dbo.Concept (ExtID,Name) values(@ExtID, @Name)";

                     foreach (var item in dt)

                     {

                         using (SqlCommand cmd = new SqlCommand(sql, cnn))

                         {

                             cmd.Parameters.Add("@ExtID", SqlDbType.NVarChar).Value = item["extId"].ToString();

                             cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = item["name"].ToString();

                             int rowsAdded = cmd.ExecuteNonQuery();

                         }
                     }

                     cnn.Close();

                 }

                 Dts.TaskResult = (int)ScriptResults.Success;
         }
             //Dts.TaskResult = (int)ScriptResults.Success;
         }
         catch (Exception ex)

         {
             Dts.Events.FireError(0, "Fire Error", "An error occurred: " + ex.Message.ToString(), "", 0);
             Dts.TaskResult = (int)ScriptResults.Failure;
         }
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, I tried the solution given by you, but getting the error for for dt, Result and Authorization. Attached the snapshot. Could you please help me with this on how to proceed?



200589-image.png


0 Votes 0 ·
image.png (136.7 KiB)