Would this be considered good or bad design?

I'm looking for an elegant way to handle SQL exceptions. I thought that if I could simply check a property to determine if the SQL interaction was successful, instead of try…catch'ing each and every call to my SQLHelper class, it might clean up the code a little, although I guess the checking brings its own bit of clutter. Any suggestions?

class Reporter { static void Main(string[] arguments) { private string connectionString = "..."; using (SQLHelper sql = new SQLHelper(connectionString)) { string command = "..."; SQLResult fetchReports = sql.FetchData(command); if (fetchReports.Success) { using (DataTable reports = fetchReports.Data) { foreach (DataRow report in reports.Rows) { try { // Create report file on disk } catch (IOException fileError) { log4netLogger.Error("Could not create report file.", fileError); } catch (Exception genralError) { log4netLogger.Error("Unable to process report.", genralError); } } } } else { log4netLogger.Error(fetchReports.FullMessage, fetchReports.Error); } } } } class SQLHelper : IDisposable { #region Private properties private SqlConnection _connection; #endregion #region Initialisation public SQLHelper(string connectionString) { _connection = new SqlConnection(connectionString); } #endregion #region Public methods public SQLResult ExecuteStatement(string commandText, params SqlParameter[] parameters) { SQLResult result = new SQLResult() { Success = false, Command = commandText }; using (SqlCommand command = this.InitiateCommand(commandText, parameters)) { try { this._connection.Open(); command.ExecuteNonQuery(); result.Success = true; result.Message = string.Format("The command executed successfully!"); } catch (SqlException sqlError) { result.Message = sqlError.Message; result.Error = sqlError; } } return result; } public SQLResult FetchData(string commandText, params SqlParameter[] parameters) { SQLResult result = new SQLResult() { Success = false, Command = commandText }; using (SqlCommand command = this.InitiateCommand(commandText, parameters)) using (SqlDataAdapter adapter = new SqlDataAdapter(command)) { try { DataTable results = new DataTable(); adapter.Fill(results); result.Data = results; result.Success = true; result.Message = string.Format("The command executed successfully!"); } catch (SqlException sqlError) { result.Message = sqlError.Message; result.Error = sqlError; } } return result; } #endregion #region Private methods private SqlCommand InitiateCommand(string commandText, params SqlParameter[] parameters) { SqlCommand command = this._connection.CreateCommand(); command.CommandText = commandText; foreach (SqlParameter parameter in parameters) { command.Parameters.Add(parameter); } return command; } #endregion #region Disposal private bool disposed = false; protected virtual void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { if (this._connection.State == ConnectionState.Open) this._connection.Close(); this._connection.Dispose(); } } this.disposed = true; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } #endregion } public class SQLResult { public bool Success { get; set; } public string Message { get; set; } public string Command { get; set; } public string FullMessage { get { return string.Format( "Message: {1}{0}Command: {2}", Environment.NewLine, this.Message, this.Command); } } public Exception Error { get; set; } public DataTable Data { get; set; } } 

Thanks!

by that0th3rGuy via /r/csharp

Leave a Reply