//Insert private bool Insert(string firstName, string lastName, string synonym) { bool isInserted = false; try { int rowsAffected = 0; StringBuilder insertQuery = new StringBuilder(); insertQuery.Append("INSERT INTO Customer "); insertQuery.Append("("); insertQuery.Append("FirstName, "); insertQuery.Append("LastName, "); insertQuery.Append("Synonym "); insertQuery.Append(") "); insertQuery.Append("VALUES "); insertQuery.Append("("); insertQuery.Append("@FirstName, "); insertQuery.Append("@LastName, "); insertQuery.Append("@Synonym "); insertQuery.Append(");"); using (SqlCommand commandSql = new SqlCommand(insertQuery.ToString())) { commandSql.Parameters.AddWithValue("@FirstName", firstName); commandSql.Parameters.AddWithValue("@LastName", lastName); commandSql.Parameters.AddWithValue("@Synonym", synonym); DataAccessLayer dal = new DataAccessLayer(); rowsAffected = dal.Execute(commandSql); // SqlCommand ???? rowsAffected = dal.Insert(commandSql); // SqlDataAdapter ??? } if (rowsAffected > 0) { isInserted = true; } } catch (Exception ex) { Data.ShowMessage(ex.ToString()); } return isInserted; }
//UPDATE private bool Update(int custId, string firstName, string lastName, string synonym) { bool isUpdated = false; try { int rowsAffected = 0; StringBuilder updateQuery = new StringBuilder(); updateQuery.Append("UPDATE Customer "); updateQuery.Append("SET "); updateQuery.Append("FirstName = @FirstName, "); updateQuery.Append("LastName = @LastName, "); updateQuery.Append("Synonym = @Synonym "); updateQuery.Append("WHERE "); updateQuery.Append("CustomerId = @CustomerId;"); using (SqlCommand commandSql = new SqlCommand(updateQuery.ToString())) { commandSql.Parameters.AddWithValue("@CustomerId", custId); commandSql.Parameters.AddWithValue("@FirstName", firstName); commandSql.Parameters.AddWithValue("@LastName", lastName); commandSql.Parameters.AddWithValue("@Synonym", synonym); DataAccessLayer dal = new DataAccessLayer(); rowsAffected = dal.Execute(commandSql); // SqlCommand ???? rowsAffected = dal.Update(commandSql); // SqlDataAdapter ??? } if (rowsAffected > 0) { isUpdated = true; } } catch (Exception ex) { Data.ShowMessage(ex.ToString()); } return isUpdated; }
//DELETE private bool Delete(int custId) { bool isDeleted = false; try { int rowsAffected = 0; StringBuilder deleteQuery = new StringBuilder(); deleteQuery.Append("DELETE FROM Customer "); deleteQuery.Append("WHERE "); deleteQuery.Append("CustomerId = @CustomerId;"); using (SqlCommand commandSql = new SqlCommand(deleteQuery.ToString())) { commandSql.Parameters.AddWithValue("@CustomerId", custId); DataAccessLayer dal = new DataAccessLayer(); rowsAffected = dal.Delete(commandSql); rowsAffected = dal.Execute(commandSql); // SqlCommand ???? rowsAffected = dal.Delete(commandSql); // SqlDataAdapter ??? } if (rowsAffected > 0) { isDeleted = true; } } catch (Exception ex) { Data.ShowMessage(ex.ToString()); } return isDeleted; }