0

我的模型类如下所示:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.Mvc;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OEWebPortalMVCVer5.Models
{
 class DenialModel
{
    public Dictionary< string, DataSet> DataSets { get; set; }

    public static DataSet ReadRows(DataSet dataset)
    {
        try
        {
            SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["demo10ConnectionString"].ConnectionString);

            string startDate = "1/1/1900";
            string endDate = "12/31/2020";
            conn.Open();
            DataSet dtDS = new DataSet();
            SqlCommand dtCom = new SqlCommand("dbo.cusGenDenialReport", conn);
            dtCom.Parameters.AddWithValue("@StartDate", SqlDbType.DateTime).Value = Convert.ToDateTime(startDate);
            dtCom.Parameters.AddWithValue("@EndDate", SqlDbType.DateTime).Value = Convert.ToDateTime(endDate);
            dtCom.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter dtDa = new SqlDataAdapter(dtCom);

            dtDa.Fill(dtDS);
            conn.Close();
            dtCom.Dispose();
        }
        catch (Exception ex)
        {
            string message = "It didn't work because: " + ex.Message.ToString();
        }
    }
}
}

当我尝试在 VS2012 中构建解决方案时,它会引发以下错误:

Error   1   'OEWebPortalMVCVer5.Models.DenialModel.ReadRows(System.Data.DataSet)': not all code paths return a value    C:\Users\Orchestrate\documents\visual studio 2012\Project\OEWebPortalMVCVer5\OEWebPortalMVCVer5\Models\DenialModel.cs   22  31  OEWebPortalMVCVer5

我的第一个想法是我没有处理参数 startDate 和 endDate 的空值,所以我对它们进行了硬编码,稍后将解决这个问题。情况并非如此,所以我添加了一个 try catch 块来获取错误。好吧,这当然没有帮助(我一定已经很累了,以为 try catch 会神奇地修复它)。我已经用谷歌搜索了这个错误并搜索了这个网站,但没有找到任何与这个问题相关的答案。

我不确定此时如何让代码正确编译。任何帮助表示赞赏

谢谢

4

1 回答 1

0

该错误准确地说明了问题所在:您的函数中没有return声明。

public static DataSet ReadRows(DataSet dataset)
    {
        DataSet dtDS = null;
        try
        {
            SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["demo10ConnectionString"].ConnectionString);

            string startDate = "1/1/1900";
            string endDate = "12/31/2020";
            conn.Open();
            dtDS = new DataSet();
            SqlCommand dtCom = new SqlCommand("dbo.cusGenDenialReport", conn);
            dtCom.Parameters.AddWithValue("@StartDate", SqlDbType.DateTime).Value = Convert.ToDateTime(startDate);
            dtCom.Parameters.AddWithValue("@EndDate", SqlDbType.DateTime).Value = Convert.ToDateTime(endDate);
            dtCom.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter dtDa = new SqlDataAdapter(dtCom);

            dtDa.Fill(dtDS);
            conn.Close();
            dtCom.Dispose();
        }
        catch (Exception ex)
        {
            //You probably should let it break...
            string message = "It didn't work because: " + ex.Message.ToString();
        }

        return dtDS;
    }
于 2013-06-07T16:25:58.783 回答