public Client getClient(int ID) { Client cli = new Client(); SqlConnection con = new SqlConnection(str); con.Open(); string sql = "SELECT _id,Name,Surname,Age FROM Clients WHERE _id=@id"; SqlCommand cmd = new SqlCommand(sql, con); cmd.Parameters.Add("@id", System.Data.SqlDbType.Int).Value = ID; SqlDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection); if (reader.Read()) { cli = new Client(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetInt32(3)); } reader.Close(); return cli; }
public JsonResult Client(int? id, Client item) { switch (Request.HttpMethod) { case "POST": return Json(cManager.insertClient(item)); case "PUT": return Json(cManager.updateClient(item)); case "GET": return Json(cManager.getClient(id.GetValueOrDefault()), JsonRequestBehavior.AllowGet); case "DELETE": return Json(cManager.deleteClient(id.GetValueOrDefault())); } return Json(new { Error = true, Message = "Unknown HTTP Operation" }); }
public bool updateClient(Client cli) { SqlConnection con = new SqlConnection(str); con.Open(); string sql = "UPDATE Clients SET Name=@name, Surname=@surname, Age=@age WHERE _id=@id"; SqlCommand cmd = new SqlCommand(sql, con); cmd.Parameters.Add("@id", System.Data.SqlDbType.VarChar).Value = cli.ID; cmd.Parameters.Add("@name", System.Data.SqlDbType.VarChar).Value = cli.Name; cmd.Parameters.Add("@surname", System.Data.SqlDbType.VarChar).Value = cli.Surname; cmd.Parameters.Add("@age", System.Data.SqlDbType.VarChar).Value = cli.Age; int res = cmd.ExecuteNonQuery(); con.Close(); return (res == 1); }
public bool insertClient(Client cli) { SqlConnection con = new SqlConnection(str); con.Open(); string sql = "INSERT INTO Clients (Name, Surname, Age) VALUES (@name, @surname, @age)"; SqlCommand cmd = new SqlCommand(sql, con); cmd.Parameters.Add("@name", System.Data.SqlDbType.VarChar).Value =cli.Name; cmd.Parameters.Add("@surname", System.Data.SqlDbType.VarChar).Value = cli.Surname; cmd.Parameters.Add("@age", System.Data.SqlDbType.VarChar).Value = cli.Age; int res = cmd.ExecuteNonQuery(); con.Close(); return (res==1); }