简体   繁体   中英

How do i return error message in different return type function?

This function connects to postgres database and returns Dataset.

Two things i want to understant

  1. If i get an error how can i return it ?
  2. Is this the best way to return Dataset ?

     string strODBCDriverName = "DSN=Postgres_32"; public DataSet SelectDataSet(string sql, bool isProcedure, Dictionary<string, object> parameters = null) { using (OdbcConnection odbcConnection = new OdbcConnection(strODBCDriverName)) { odbcConnection.Open(); using (OdbcCommand odbcCommand = new OdbcCommand(sql, odbcConnection)) { if (isProcedure) odbcCommand.CommandType = CommandType.StoredProcedure; else odbcCommand.CommandType = CommandType.Text; if (parameters != null) foreach (KeyValuePair<string, object> parameter in parameters) odbcCommand.Parameters.AddWithValue(parameter.Key, parameter.Value); using (OdbcDataAdapter adapter = new OdbcDataAdapter(odbcCommand)) { using (DataSet ds = new DataSet()) { try { adapter.Fill(ds); return ds; } catch (Exception ex) { throw (ex); } finally { } } } } } } 

I think it would be great if you return null ; If you need return some customized message as well means you can use out parameters for this. So that the return value will be null if any Exception occurs in this case out parameter will holds the exception details. if Dataset is populated well means outParameter will have a values "Success" or something like that. So the method signature will be changed as like the following

public static DataSet SelectDataSet(string sql, bool isProcedure, out string message, Dictionary<string, object> parameters = null)
{
    // Rest of codes here

    try
    {
        message = "Success";
        adapter.Fill(ds);
        return ds;
    }
    catch (Exception ex)
    {
        message = ex.Message;
        return null;
    }

}

And you can call this method like this:

string message = String.Empty;
DataSet resultDataset = SelectDataSet("query here", false, out message);
if (resultDataset != null)
{
    Console.WriteLine(message);
    // proceed with resultDataset
}
else
{
    Console.WriteLine(message);
}

Here resultDataset will be null in case of any exception otherwise you can proceed with its value.

Create a class:

class DataSetWithError: DataSet
{
    public Exception msg { get; set; }
}

Save error during query:

using (OdbcDataAdapter adapter = new OdbcDataAdapter(odbcCommand))
{
    DataSetWithError ds = new DataSetWithError();

    try
    {
        adapter.Fill(ds);
    }
    catch (Exception ex)
    {
        ds.msg = ex;
    }
    finally
    {
        adapter.Close();
    }

    return ds;
}

And result:

DataSetWithError dataSetWithError = SelectDataSet();
if (dataSetWithError.msg == null)
{
    // Show data
}
else
{
    MessageBox.Show(dataSetWithError.msg.ToString());
}

I like having generic Result class that can be reused:

internal class Result
    {
    internal bool IsFailure => !IsSuccess;

    internal bool IsSuccess { get; }

    internal string Error { get; }

    protected Result(bool isSuccess, string error) {
        IsSuccess = isSuccess;
        Error = error;
    }

    private Result(bool isSuccess) : this(isSuccess, null) { }

    internal static Result Fail(string error) => new Result(false, error);

    internal static Result<T> Fail<T>(string error) =>
        new Result<T>(default(T), false, error);

    internal static Result Ok() => new Result(true);

    internal static Result<T> Ok<T>(T value) => new Result<T>(value, true);
}

internal sealed class Result<T> : Result
    {
    internal T Value { get; }

    internal Result(T value, bool isSuccess) : this(value, isSuccess, null) { }

    internal Result(T value, bool isSuccess, string error) : base(isSuccess, error) {
        Value = value;
    }

This can be used not only DataSet , but any type.

In your case return would be Result<DataSet> and returns can become:

returns ds --> new Result.Ok(d)

throw ex --> new Result.Fail<DataSet>(ex.Message)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM