public string Insert(Place entity) { transaction = null; string message = string.Empty; try { entity.PlaceId = broker.GetId("PlaceId", "Place"); broker.Connection.Open(); broker.Command = new SqlCommand("PROC_INSERT_PLACE", broker.Connection) { CommandType = CommandType.StoredProcedure }; broker.Command.Parameters.AddWithValue("PlaceId", Convert.ToInt32(entity.PlaceId)); broker.Command.Parameters.AddWithValue("Name", SqlDbType.VarChar).Value = entity.Name; broker.Command.Parameters.AddWithValue("Zipcode", SqlDbType.Int).Value = entity.Zipcode; if (entity.Population != null) { broker.Command.Parameters.AddWithValue("Population", SqlDbType.Int).Value = entity.Population; } else { broker.Command.Parameters.AddWithValue("Population", DBNull.Value); } int result = broker.Command.ExecuteNonQuery(); if (result != -1) { message = $"{entity.Name} successfully inserted!"; } else { throw new Exception("There was error inserting a place!"); } return(message); } catch (Exception ex) { throw new Exception(ex.Message); } finally { if (broker.Connection != null) { broker.Connection.Close(); } } }
public string Insert(Person entity) { transaction = null; string message = string.Empty; try { entity.PersonId = broker.GetId("PersonId", "Person"); broker.Connection.Open(); broker.Command = new SqlCommand("PROC_INSERT_PERSON", broker.Connection); broker.Command.CommandType = CommandType.StoredProcedure; broker.Command.Parameters.AddWithValue("PersonId", Convert.ToInt32(entity.PersonId)); broker.Command.Parameters.AddWithValue("RegistrationNumber", SqlDbType.VarChar).Value = entity.RegistrationNumber; broker.Command.Parameters.AddWithValue("FirstName", SqlDbType.NVarChar).Value = entity.FirstName; broker.Command.Parameters.AddWithValue("LastName", SqlDbType.NVarChar).Value = entity.LastName; if (entity.Height == 0) { broker.Command.Parameters.AddWithValue("Height", DBNull.Value); } else { broker.Command.Parameters.AddWithValue("Height", entity.Height); } if (entity.Weight == 0) { broker.Command.Parameters.AddWithValue("Weight", DBNull.Value); } else { broker.Command.Parameters.AddWithValue("Weight", entity.Weight); } if (entity.EyeCollor == 0) { broker.Command.Parameters.AddWithValue("EyeCollor", DBNull.Value); } else { broker.Command.Parameters.AddWithValue("EyeCollor", entity.EyeCollor); } if (String.IsNullOrEmpty(entity.PhoneNumber)) { broker.Command.Parameters.AddWithValue("PhoneNumber", DBNull.Value); } else { broker.Command.Parameters.AddWithValue("PhoneNumber", entity.PhoneNumber); } broker.Command.Parameters.AddWithValue("Email", SqlDbType.VarChar).Value = entity.Email; broker.Command.Parameters.AddWithValue("DateOfBirth", SqlDbType.Date).Value = entity.DateOfBirth; if (String.IsNullOrEmpty(entity.Address)) { broker.Command.Parameters.AddWithValue("Address", DBNull.Value); } else { broker.Command.Parameters.AddWithValue("Address", entity.Address); } if (String.IsNullOrEmpty(entity.Note)) { broker.Command.Parameters.AddWithValue("Note", DBNull.Value); } else { broker.Command.Parameters.AddWithValue("Note", entity.Note); } broker.Command.Parameters.AddWithValue("PlaceId", SqlDbType.Int).Value = entity.PlaceId; int result = broker.Command.ExecuteNonQuery(); if (result != -1) { message = $"{entity.FirstName} {entity.LastName} successfully inserted!"; } else { throw new Exception("There was error inserting a person!"); } return(message); } catch (Exception ex) { throw new Exception(ex.Message); } finally { if (broker.Connection != null) { broker.Connection.Close(); } } }