The below example performs CRUD operations against remote data using a form.
public ActionResult get(String pq_oper, String customerid, string companyname, string contactname, string contacttitle, string address)
{
pqTestContext db = new pqTestContext();
if (pq_oper == "add")
{
object[] param=new object[5];
param[0] = new SqlParameter("@customerid", customerid);
param[1] = new SqlParameter("@companyname", companyname);
param[2] = new SqlParameter("@contactname", contactname);
param[3] = new SqlParameter("@contacttitle", contacttitle);
param[4] = new SqlParameter("@address", address);
db.Database.ExecuteSqlCommand("insert into Customers (customerid, companyname,contactname,contacttitle,address) values (@customerid, @companyname,@contactname,@contacttitle,@address)", param);
}
else if (pq_oper == "delete")
{
object[] param = new object[1];
param[0] = new SqlParameter("@customerid", customerid);
db.Database.ExecuteSqlCommand("delete from Customers where customerid=@customerid", param);
}
else if (pq_oper == "edit")
{
object[] param = new object[5];
param[0] = new SqlParameter("@companyname", companyname);
param[1] = new SqlParameter("@contactname", contactname);
param[2] = new SqlParameter("@contacttitle", contacttitle);
param[3] = new SqlParameter("@address", address);
param[4] = new SqlParameter("@customerid", customerid);
db.Database.ExecuteSqlCommand("update Customers set companyname=@companyname,contactname=@contactname,contacttitle=@contacttitle,address=@address where customerid=@customerid", param);
}
List customers = db.Database.SqlQuery("Select customerid, companyname, contactname, contacttitle, address from customers").ToList();
JavaScriptSerializer js = new JavaScriptSerializer();
string json = js.Serialize(customers);
String response = "{\"data\":" + json + "}";
return this.Content(response, "text/text");
}