public static CrashIncident Add(int customerId) { CrashIncident result = null; DateTime when = DateTime.Now; string query = "INSERT INTO CrashIncident (CrashIncidentCustomerId, CrashIncidentWhen, CrashIncidentInitialCrashReportId) " + "VALUES (@CrashIncidentCustomerId, @CrashIncidentWhen, 0);" + "SELECT CAST(scope_identity() AS int)"; using (SqlConnection conn = new SqlConnection(DatabaseHelper.ConnectionString)) { using (SqlCommand cmd = new SqlCommand(query, conn)) { DatabaseHelper.BuildSqlParameter(cmd, "@CrashIncidentCustomerId", SqlDbType.Int, customerId); DatabaseHelper.BuildSqlParameter(cmd, "@CrashIncidentWhen", SqlDbType.DateTime, when); conn.Open(); int id = (Int32)cmd.ExecuteScalar(); if (id > 0) { result = new CrashIncident(id, customerId, when, 0); } } conn.Close(); } return(result); }
private static CrashIncident Get(SqlConnection cn, int id) { CrashIncident result = null; SqlCommand cmd = new SqlCommand("SELECT * FROM CrashIncident WHERE CrashIncidentId=" + id, cn); SqlDataReader rdr = cmd.ExecuteReader(); if (rdr.Read()) { result = BuildCrashIncident(rdr); } rdr.Close(); return(result); }
/// <summary> /// Update an entry in the CrashIncident table /// </summary> public static bool Update(CrashIncident entry) { bool result = false; using (SqlConnection cn = new SqlConnection(DatabaseHelper.ConnectionString)) { cn.Open(); result = Update(cn, entry); cn.Close(); } return(result); }
/// <summary> /// Get an entry from the CrashIncident table /// </summary> public static CrashIncident Get(int id) { CrashIncident result = null; using (SqlConnection cn = new SqlConnection(DatabaseHelper.ConnectionString)) { cn.Open(); result = Get(cn, id); cn.Close(); } return(result); }
private static bool Update(SqlConnection cn, CrashIncident entry) { Int32 rowsAffected = 0; using (SqlCommand cmd = cn.CreateCommand()) { cmd.CommandText = "UPDATE CrashIncident SET CrashIncidentCustomerId=@CrashIncidentCustomerId,CrashIncidentWhen=@CrashIncidentWhen,CrashIncidentInitialCrashReportId=@CrashIncidentInitialCrashReportId WHERE CrashIncidentId=@CrashIncidentId"; DatabaseHelper.BuildSqlParameter(cmd, "@CrashIncidentId", SqlDbType.Int, entry.Id); DatabaseHelper.BuildSqlParameter(cmd, "@CrashIncidentCustomerId", SqlDbType.Int, entry.CustomerId); DatabaseHelper.BuildSqlParameter(cmd, "@CrashIncidentWhen", SqlDbType.DateTime, entry.When); DatabaseHelper.BuildSqlParameter(cmd, "@CrashIncidentInitialCrashReportId", SqlDbType.Int, entry.TopLevelCrashReportId); rowsAffected = cmd.ExecuteNonQuery(); } return(rowsAffected != 0); }