0

错误:并非所有代码路径都返回值,

我正在尝试从函数返回数据集但收到此错误,

代码:

public partial class crystalReport_manual : System.Web.UI.Page
{
    String conStr = WebConfigurationManager.ConnectionStrings["LoginDatabaseConnectionString"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
            DataSet1 ds = Dataset_load("Select * from login"); 

            ReportDocument rd = new ReportDocument();
            rd.Load(Server.MapPath("CrystalReport.rpt"));
            rd.SetDataSource(ds);
            CrystalReportViewer1.ReportSource = rd;



    }

    public DataSet1 Dataset_load(String query) 
    {
        SqlConnection sqlcon = new SqlConnection(conStr);
        SqlCommand sqlCom = new SqlCommand("select * from Login", sqlcon);
        SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCom);
       // DataSet ds = new DataSet("CRDataSet");

        try
        {
            sqlcon.Open();
            //sqlCom.ExecuteNonQuery();
            //sqlDA.Fill(ds,"Login");

            DataSet1 ds = new DataSet1();
            DataTable dt = new DataTable("DT_CR");
            sqlDA.Fill(dt);
            ds.Tables[0].Merge(dt);
            return ds;    
        }
        catch (Exception exc)
        {
            Response.Write(exc.Message);
        }
        finally 
        {
            sqlcon.Close();
        }



    }
4

2 回答 2

4

如果发生异常怎么办,你的catchorfinally块应该返回一些值。

由于您只是关闭连接,finally因此您可以使用using如下块:

public DataSet1 Dataset_load(String query)
{
    DataSet1 ds = new DataSet1();
    using(SqlConnection sqlcon = new SqlConnection(conStr))
    using(SqlCommand sqlCom = new SqlCommand("select * from Login", sqlcon))
    using (SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCom))
    {
        try
        {
            //sqlCom.ExecuteNonQuery();
            //sqlDA.Fill(ds,"Login");
            DataTable dt = new DataTable("DT_CR");
            sqlDA.Fill(dt);
            ds.Tables[0].Merge(dt);
            return ds;

        }
        catch (SqlException se)
        {
            Response.Write(se.Message);
            return null;
        }
        catch (Exception exc)
        {
            Response.Write(exc.Message);
            return null;
        }
    }
}

using在内部转换为try-finally块,并与实现的那些一起工作IDisposable,在finally块中它调用该Dispose方法,因为SqlConnection,SqlCommandSqlDataAdapter,所有实现IDisposableusing语句都将Dispose在结束时调用,这将关闭连接。

于 2013-10-29T17:53:19.947 回答
1
public DataSet1 Dataset_load(String query)
{
    DataSet1 ds = new DataSet1();
    using(SqlConnection sqlcon = new SqlConnection(conStr))
    using(SqlCommand sqlCom = new SqlCommand("select * from Login", sqlcon))
    using (SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCom))
    {
        try
        {
            //sqlCom.ExecuteNonQuery();
            //sqlDA.Fill(ds,"Login");
            DataTable dt = new DataTable("DT_CR");
            sqlDA.Fill(dt);
            ds.Tables[0].Merge(dt);
            //return ds;

        }
        catch (SqlException se)
        {
            Response.Write(se.Message);
            //return null;
        }
        catch (Exception exc)
        {
            Response.Write(exc.Message);
            //return null;
        }
    }

    return ds;

}

根据之前用户的响应,如果在 using 语句的末尾返回了一个空的 DataSet,您可以随时检查ds.Tables.Count是否返回了任何内容。否则,您可以使用上面的逻辑并null检查

于 2013-10-29T18:03:22.490 回答