返回数据集值时出错

3
我定义了以下函数,该函数将从表中返回3列。
public DataSet GetFunc()
    {
        int iRet = 0;
        DataSet ds = new DataSet();
        SqlConnection sqlConnection = new SqlConnection();
        try
        {
            iRet = connect(ref sqlConnection);
            if (DB_SUCCESS_CONNECT == iRet)
            {
                SqlCommand sqlCommand = new SqlCommand("", sqlConnection);                   
                String strQuery = "Select ID, Did, FirstName from Users";
                sqlCommand.CommandText = strQuery;

                SqlDataAdapter adaptor = new SqlDataAdapter(sqlCommand);
                adaptor.Fill(ds);
                sqlConnection.Close();
                return ds;                    
            }

        }
        catch (Exception e)
        {
            disconnect(ref sqlConnection);
        }
    }

但是,当我尝试构建它时,出现了以下错误:

错误 172 'GetFunc()': 并非所有代码路径都返回值

我不确定哪里出错了。有人能指导我吗?

7个回答

5
在try块中,您指定了返回类型,而在catch块中没有返回类型。通常当编译器未找到适当的返回值时会出现此错误。尝试在catch中返回ds,但请确保在逻辑中进一步检查ds是否为空。

2
public DataSet GetFunc()
{
    int iRet = 0;
    DataSet ds = new DataSet();
    SqlConnection sqlConnection = new SqlConnection();
    try
    {
        iRet = connect(ref sqlConnection);
        if (DB_SUCCESS_CONNECT == iRet)
        {
            SqlCommand sqlCommand = new SqlCommand("", sqlConnection);                   
            String strQuery = "Select ID, Did, FirstName from Users";
            sqlCommand.CommandText = strQuery;

            SqlDataAdapter adaptor = new SqlDataAdapter(sqlCommand);
            adaptor.Fill(ds);
            sqlConnection.Close();
            return ds;                    
        }

    }
    catch (Exception e)
    {
        disconnect(ref sqlConnection);
    }
    return null;
}

并非所有代码路径都返回值,但必须如此。如果DB_SUCCESS_CONNECT!=iRet,则不会返回结果。尝试返回一些默认值,例如上面的null。另一个问题是,如果抛出异常,您没有返回值。当抛出异常时,您正在断开连接并且没有返回任何值。


2

您在try块中只有返回语句,这并不能保证它总是会被执行,因为编译器假设可能会出现异常。添加另一个返回语句,在try之外返回nulldataset,这样就不会出错了。您可以只使用一个返回语句而不是两个或三个。

public DataSet GetFunc()
{
    int iRet = 0;
    DataSet ds = null;
    SqlConnection sqlConnection = new SqlConnection();
    try
    {
        iRet = connect(ref sqlConnection);
        if (DB_SUCCESS_CONNECT == iRet)
        {
            SqlCommand sqlCommand = new SqlCommand("", sqlConnection);                   
            String strQuery = "Select ID, Did, FirstName from Users";
            sqlCommand.CommandText = strQuery;

            SqlDataAdapter adaptor = new SqlDataAdapter(sqlCommand);
            ds = new DataSet();
            adaptor.Fill(ds);
            sqlConnection.Close();                
        }         
    }
    catch (Exception e)
    {
        disconnect(ref sqlConnection);
    }
   return ds;
}

在结尾处只需要一个 return null 就足够了 :) - Kamil Budziewski
1
谢谢指出,只需要一个返回语句就可以了,而不是两个。 - Adil

2

如果在 try ... catch 块内部抛出了异常,则没有指定返回值。

补充:

return ds;

在捕获块之后,在您的函数末尾添加。

2

因为在以下情况下没有返回路径:- DB_SUCCESS_CONNECT != iRet


并且在 try-catch 中返回 :) - Kamil Budziewski

1

你的代码失败是因为只有条件为真时才返回值。如果条件失败或发生异常,则从方法中没有返回任何内容。

另外请注意,你没有正确处理连接。你需要关闭或处理连接对象。我会将您的方法更改如下:

public DataSet GetFunc()
{
    string strQuery = "Select ID, Did, FirstName from Users";
    DataSet ds = new DataSet();
    using (var sqlConnection = new SqlConnection())
    using (var sqlCommand = new SqlCommand(strQuery, sqlConnection))
    using (var adaptor = new SqlDataAdapter(sqlCommand))
    {
        adaptor.Fill(ds);
    }
    return ds;
}

1
trycatch块之后放置返回语句,尝试使用以下代码:
public DataSet GetFunc()
{
    int iRet = 0;
    DataSet ds = new DataSet();
    SqlConnection sqlConnection = new SqlConnection();
    try
    {
        iRet = connect(ref sqlConnection);
        if (DB_SUCCESS_CONNECT == iRet)
        {
            SqlCommand sqlCommand = new SqlCommand("", sqlConnection);                   
            String strQuery = "Select ID, Did, FirstName from Users";
            sqlCommand.CommandText = strQuery;

            SqlDataAdapter adaptor = new SqlDataAdapter(sqlCommand);
            adaptor.Fill(ds);
            sqlConnection.Close();                    
        }
    }
    catch (Exception e)
    {
        disconnect(ref sqlConnection);
    }
   return ds;
}

如果函数有返回类型,则在所有情况下都应该返回某些东西,因此函数应该为 Try 块以及 Catch 块返回返回值。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接