繁体   English   中英

如何在不同的返回类型函数中返回错误消息?

[英]How do i return error message in different return type function?

此函数连接到postgres数据库并返回数据集。

我想强调的两件事

  1. 如果我遇到错误,我该如何退回?
  2. 这是返回数据集的最佳方法吗?

     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 { } } } } } } 

我认为,如果您返回null ,那会很好; 如果你需要返回一些定制的消息以及意味着你可以使用out参数这一点。 因此,如果发生任何异常,则返回值将为null,在这种情况下, out参数将保存异常详细信息。 如果数据集填充良好,则意味着outParameter将具有“成功”或类似的值。 因此方法签名将如下更改

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;
    }

}

您可以这样调用此方法:

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);
}

如果有任何异常,这里的resultDataset将为null,否则您可以继续使用其值。

创建一个类:

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

在查询期间保存错误:

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;
}

结果:

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

我喜欢具有可以重用的通用Result class

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;
    }

不仅可以使用DataSet ,还可以使用任何类型。

在您的情况下,返回Result<DataSet> ,返回值可以变为:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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