1

I am currently working on a task which takes student information through web service and saves it to database. I have successfully implemented the task for adding single student information, but I want to add multiple students at runtime using the same web method.

Here is the code:

 public string insertStudent(int id,string f_name , string l_name)
 {
        var con = new SqlConnection(ConnectionState());

        con.Open();

        string insert =  "INSERT INTO STUDENT(std_id, first_name,last_name)";
        //var cmd = new SqlCommand("Insert into [Student](std_id,first_name, last_name,) values('" + id + "','" + f_name + "','" + l_name+ "')", con);
        insert += " VALUES (";
        insert += "'" + id + "'";
        insert += ", '" + f_name + "'";
        insert += ", '" + l_name + "'";
        insert += ")";

        var  cmd = new SqlCommand(insert, con);
        cmd.ExecuteNonQuery();

        string result = "Data Added to Database";
        con.Close();

        return result;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • You should have web service to accept information for multiple students in json format and use looping to insert it into the database? On the side note web API is better solution then web service. – Chetan Apr 21 '18 at 08:05
  • 1
    I have a requirement of using web services so that's why I am using this option. – Deepak Kumar Khatri Apr 21 '18 at 08:07
  • Web API is a better solution, it does not mean that web service must not be used. You can achieve the same functionality with web service too. – Chetan Apr 21 '18 at 08:19
  • https://stackoverflow.com/questions/19557184/how-to-pass-json- This Link may help you – Gans Apr 21 '18 at 08:24
  • [SQL Injection alert](http://msdn.microsoft.com/en-us/library/ms161953%28v=sql.105%29.aspx) - you should **not** concatenate together your SQL statements - use **parametrized queries** instead to avoid SQL injection - check out [Little Bobby Tables](https://xkcd.com/327/) – marc_s Apr 21 '18 at 08:30
  • https://www.c-sharpcorner.com/blogs/pass-json-array-to-webservices1 this wll help you – Gans Apr 21 '18 at 08:38

0 Answers0