protected int Save(string table, List <string> columns, List <SqlParameter> parameters, int id = 0) { string query = ""; if (id == 0) { string col = "("; string val = "("; foreach (string item in columns) { col += item + ", "; val += "@" + item + ", "; } col = col.Substring(0, col.Length - 2) + ")"; val = val.Substring(0, val.Length - 2) + ")"; query = "INSERT INTO " + table + " " + col + " OUTPUT INSERTED.id VALUES " + val + ";"; } else { string update = ""; foreach (string item in columns) { update += item + "=@" + item + ", "; } update = update.Substring(0, update.Length - 2); query = "UPDATE " + table + " SET " + update + " WHERE id = @id"; parameters.Add(new SqlParameter("@id", id)); } SqlDataReader rdr = DBHandler.DatabaseOperation(query, parameters); while (rdr.Read() && id == 0) { id = rdr.GetInt32(0); } DBHandler.DatabaseCleanup(rdr, _conn); return(id); }
protected static SqlDataReader DatabaseOperation(string query, SqlParameter parameter) { return(DBHandler.DatabaseOperation(query, new List <SqlParameter> { parameter })); }