I am finding it difficult to implement a .NET web service to insert data in my database created in SQL Server 2008 . I am stuck after implementing the following code :
namespace DataService
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld(String entity)
{
String firstName = "";
SqlConnection myConnection = new SqlConnection(
@"Data Source=.\SQLEXPRESS;" +
@"Initial Catalog=student;User ID=sa;Password=123");
try
{
myConnection.Open();
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = myConnection;
myCommand.CommandText = "insert into stud values " +
"stud_name = '" + firstName + "'";
SqlDataReader myReader = myCommand.ExecuteReader();
//while
if (myReader.Read())
{
firstName = myReader["stud_name"].ToString();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
myConnection.Close();
}
return firstName;
}
}
}
Here entity is the JSONArray i get in the form:
[{"key0":"john","key2":"ann","key1":"joe"}]
I need to insert each value for eg "john" in the database table.
Newtonsoft.Json