public TireModel Read(int id) { TireModel model = null; String sql = "select brand, Name, Size from [Tire] where Id=@Id"; using (SqlConnection connection = new SqlConnection(connectionString)) using (SqlCommand command = new SqlCommand(sql, connection)) { command.Parameters.AddWithValue("@Id", id); command.Connection.Open(); SqlDataReader reader = command.ExecuteReader(); if (reader.Read()) { model = new TireModel(); model.Id = id; model.Name = reader["Name"].ToString(); model.Brand = reader["brand"].ToString(); model.Size = reader["Size"].ToString(); } } return(model); }
public List <TireModel> List() { List <TireModel> tireList = new List <TireModel>(); String sql = "select Id, Brand, Name, size from [Tire]"; using (SqlConnection connection = new SqlConnection(connectionString)) using (SqlCommand command = new SqlCommand(sql, connection)) { command.Connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { TireModel model = new TireModel(); model.Id = Convert.ToInt32(reader["Id"]); model.Name = reader["Name"].ToString(); model.Brand = reader["brand"].ToString(); model.Size = reader["Size"].ToString(); tireList.Add(model); } return(tireList); } }
public void Update(TireModel model) { String sql = "Update [TIre] set Brand=@Brand, Name=@Name, Size=@Size where Id=@Id"; using (SqlConnection connection = new SqlConnection(connectionString)) using (SqlCommand command = new SqlCommand(sql, connection)) { command.Connection.Open(); command.Parameters.AddWithValue("@Brand", model.Brand); command.Parameters.AddWithValue("@Name", model.Name); command.Parameters.AddWithValue("@Size", model.Size); command.Parameters.AddWithValue("@Id", model.Id); command.ExecuteNonQuery(); } }
public bool Create(TireModel model) { String sql = "insert into [Tire] (brand, Name, Size) values (@Brand, @Name, @Size)"; using (SqlConnection connection = new SqlConnection(connectionString)) using (SqlCommand command = new SqlCommand(sql, connection)) { command.Parameters.AddWithValue("@Brand", model.Brand); command.Parameters.AddWithValue("@Name", model.Name); command.Parameters.AddWithValue("@Size", model.Size); command.Connection.Open(); command.ExecuteNonQuery(); return(true); } }