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

reddy421 26 Reputation points
2022-02-08T04:41:55.033+00:00

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 Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,702 questions
Microsoft Partner Center API
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. reddy421 26 Reputation points
    2022-02-15T21:25:04.82+00:00

    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 person found this answer helpful.

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    2 deleted comments

    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.