返回给出错误的数据集值

Esh*_*sha 3 c# asp.net dataset sqldataadapter

我已经定义了以下函数,它将从表中返回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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

但是当我尝试构建它时,我收到错误:

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

我很困惑我哪里出错了.有人可以指导我吗?

小智 5

在try块中你在catch块中给出了一个返回类型,没有返回类型.当编译器找不到合适的返回值时,通常会发生此错误.尝试在catch中返回ds

但请确保在逻辑中进一步检查ds是否进行空检查