1

所以我使用存储过程从数据库中选择一个注册表,存储过程唯一要做的就是SELECT ...问题是我使用该存储过程来填充我在WebService中返回的DataSet。当我想发送异常而不是 DataSet 时,问题就出现了,因为存储过程会检查 DB 并返回一个空行,DataSet 没有填充任何内容并且不会将我发送到异常......现在,我保存了我的所有同一数据库中的日志表中的异常...我的问题是,如果 SELECT 值为空,我可以转到异常块吗?

这是我的代码

[WebMethod(Description = "Private", EnableSession = false)]
public DataSet M812(string p_transaction)
{
    string extran, enclosure, eDate;
    try
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "dbo.sp_M812";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@p_trans", SqlDbType.NChar, 18).Value = p_transaction;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds, "Entradas");
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        cmd.Connection.Close();
        return ds;
    }
    catch(SqlException Ex)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "dbo.sp_reqdataerrorlog";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@p_inTrans", SqlDbType.NChar, 12).Value = p_transaction;
        cmd.Parameters.Add("@vo_enclosure", SqlDbType.VarChar, 250).Direction = ParameterDirection.Output;
        cmd.Parameters.Add("@vo_trans", SqlDbType.VarChar, 10).Direction = ParameterDirection.Output;
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        cmd.Connection.Close();
        enclosure = "" + cmd.Parameters["@vo_enclosure"].Value;
        extran = "" + cmd.Parameters["@vo_trans"].Value;

        SqlConnection con2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
        SqlCommand cmd2 = new SqlCommand();
        cmd2.Connection = con2;
        cmd2.CommandText = "dbo.sp_errorlog";
        cmd2.CommandType = CommandType.StoredProcedure;
        cmd2.Parameters.Add("@p_inTrans", SqlDbType.NChar, 12).Value = p_transaction;
        cmd2.Parameters.Add("@p_enclosure", SqlDbType.NChar, 6).Value = enclosure;
        cmd2.Parameters.Add("@p_trans", SqlDbType.NChar, 18).Value = extran;
        cmd2.Parameters.Add("@p_method", SqlDbType.NChar, 6).Value = "812";
        cmd2.Parameters.Add("@p_message", SqlDbType.NVarChar, 250).Value = "SQL Error: " + Ex.Message;
        cmd2.Parameters.Add("@vo_message", SqlDbType.VarChar, 250).Direction = ParameterDirection.Output;
        cmd2.Parameters.Add("@vo_errorDate", SqlDbType.DateTime).Direction = ParameterDirection.Output;
        con2.Open();
        cmd2.ExecuteNonQuery();
        con2.Close();
        cmd2.Connection.Close();
        eDate = "" + cmd2.Parameters["@vo_errorDate"].Value;

        SqlConnection con3 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
        SqlCommand cmd3 = new SqlCommand();
        cmd3.Connection = con3;
        cmd3.CommandText = "dbo.sp_selecterrorlog";
        cmd3.CommandType = CommandType.StoredProcedure;
        cmd3.Parameters.Add("@p_trans", SqlDbType.NChar, 18).Value = p_transaction;
        cmd3.Parameters.Add("@p_date", SqlDbType.DateTime).Value = eDate;
        SqlDataAdapter da = new SqlDataAdapter(cmd3);
        DataSet ds = new DataSet();
        da.Fill(ds, "ErrorLog");
        con3.Open();
        cmd3.ExecuteNonQuery();
        con3.Close();
        cmd3.Connection.Close();
        return ds;
    }
    catch (Exception Ex)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "dbo.sp_reqdataerrorlog";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@p_inTrans", SqlDbType.NChar, 12).Value = p_transaction;
        cmd.Parameters.Add("@vo_enclosure", SqlDbType.VarChar, 250).Direction = ParameterDirection.Output;
        cmd.Parameters.Add("@vo_trans", SqlDbType.VarChar, 10).Direction = ParameterDirection.Output;
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        cmd.Connection.Close();
        enclosure = "" + cmd.Parameters["@vo_enclosure"].Value;
        extran = "" + cmd.Parameters["@vo_trans"].Value;

        SqlConnection con2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
        SqlCommand cmd2 = new SqlCommand();
        cmd2.Connection = con2;
        cmd2.CommandText = "dbo.sp_errorlog";
        cmd2.CommandType = CommandType.StoredProcedure;
        cmd2.Parameters.Add("@p_inTrans", SqlDbType.NChar, 12).Value = p_transaction;
        cmd2.Parameters.Add("@p_enclosure", SqlDbType.NChar, 6).Value = enclosure;
        cmd2.Parameters.Add("@p_trans", SqlDbType.NChar, 18).Value = extran;
        cmd2.Parameters.Add("@p_method", SqlDbType.NChar, 6).Value = "812";
        cmd2.Parameters.Add("@p_message", SqlDbType.NVarChar, 250).Value = "WEB Error: " + Ex.Message;
        cmd2.Parameters.Add("@vo_message", SqlDbType.VarChar, 250).Direction = ParameterDirection.Output;
        cmd2.Parameters.Add("@vo_errorDate", SqlDbType.DateTime).Direction = ParameterDirection.Output;
        con2.Open();
        cmd2.ExecuteNonQuery();
        con2.Close();
        cmd2.Connection.Close();
        eDate = "" + cmd2.Parameters["@vo_errorDate"].Value;

        SqlConnection con3 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
        SqlCommand cmd3 = new SqlCommand();
        cmd3.Connection = con3;
        cmd3.CommandText = "dbo.sp_selecterrorlog";
        cmd3.CommandType = CommandType.StoredProcedure;
        cmd3.Parameters.Add("@p_trans", SqlDbType.NChar, 18).Value = p_transaction;
        cmd3.Parameters.Add("@p_date", SqlDbType.DateTime).Value = eDate;
        SqlDataAdapter da = new SqlDataAdapter(cmd3);
        DataSet ds = new DataSet();
        da.Fill(ds, "ErrorLog");
        con3.Open();
        cmd3.ExecuteNonQuery();
        con3.Close();
        cmd3.Connection.Close();
        return ds;
    }

}
4

1 回答 1

1

以下是如何检查DataSet任何表中是否有任何行:

bool hasRows = ds.Tables.Cast<DataTable>().Any(table => table.Rows.Count != 0);

现在您可以选择如何将错误数据返回给调用者:

  1. 将错误构建逻辑提取到您的异常catch处理程序块和您的if条件都可以调用的单独方法中,如下所示:

    private void BuildAndReturnErrorDataSet()
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "dbo.sp_reqdataerrorlog";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@p_inTrans", SqlDbType.NChar, 12).Value = p_transaction;
        cmd.Parameters.Add("@vo_enclosure", SqlDbType.VarChar, 250).Direction = ParameterDirection.Output;
        cmd.Parameters.Add("@vo_trans", SqlDbType.VarChar, 10).Direction = ParameterDirection.Output;
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        cmd.Connection.Close();
        enclosure = "" + cmd.Parameters["@vo_enclosure"].Value;
        extran = "" + cmd.Parameters["@vo_trans"].Value;
    
        SqlConnection con2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
        SqlCommand cmd2 = new SqlCommand();
        cmd2.Connection = con2;
        cmd2.CommandText = "dbo.sp_errorlog";
        cmd2.CommandType = CommandType.StoredProcedure;
        cmd2.Parameters.Add("@p_inTrans", SqlDbType.NChar, 12).Value = p_transaction;
        cmd2.Parameters.Add("@p_enclosure", SqlDbType.NChar, 6).Value = enclosure;
        cmd2.Parameters.Add("@p_trans", SqlDbType.NChar, 18).Value = extran;
        cmd2.Parameters.Add("@p_method", SqlDbType.NChar, 6).Value = "812";
        cmd2.Parameters.Add("@p_message", SqlDbType.NVarChar, 250).Value = "WEB Error: " + Ex.Message;
        cmd2.Parameters.Add("@vo_message", SqlDbType.VarChar, 250).Direction = ParameterDirection.Output;
        cmd2.Parameters.Add("@vo_errorDate", SqlDbType.DateTime).Direction = ParameterDirection.Output;
        con2.Open();
        cmd2.ExecuteNonQuery();
        con2.Close();
        cmd2.Connection.Close();
        eDate = "" + cmd2.Parameters["@vo_errorDate"].Value;
    
        SqlConnection con3 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
        SqlCommand cmd3 = new SqlCommand();
        cmd3.Connection = con3;
        cmd3.CommandText = "dbo.sp_selecterrorlog";
        cmd3.CommandType = CommandType.StoredProcedure;
        cmd3.Parameters.Add("@p_trans", SqlDbType.NChar, 18).Value = p_transaction;
        cmd3.Parameters.Add("@p_date", SqlDbType.DateTime).Value = eDate;
        SqlDataAdapter da = new SqlDataAdapter(cmd3);
        DataSet ds = new DataSet();
        da.Fill(ds, "ErrorLog");
        con3.Open();
        cmd3.ExecuteNonQuery();
        con3.Close();
        cmd3.Connection.Close();
        return ds;
    }
    
    if(!hasRows)
    {
        // Call error data set building logic
        BuildAndReturnErrorDataSet();
    }
    
    catch (Exception Ex)
    {
        // Call error data set building logic
        BuildAndReturnErrorDataSet();
    }
    
  2. 如果hasRows为 false 则引发异常 - 不建议这样做,因为这是使用异常处理来导致程序控制流,但它在技术上会起作用。

于 2013-09-25T19:17:03.593 回答