简体   繁体   English

C#数据导出到Excel-错误

[英]C# data export to excel - error

C# data export to excel - error C#数据导出到Excel-错误

Hi all, hope in your help. 大家好,希望对您有所帮助。

I am creating a C# 2010 Web application. 我正在创建一个C#2010 Web应用程序。

I have researched and test this and this should work...but I get an error and I can't get past this problem...my code below. 我已经研究并测试了这一点,这应该可以工作...但是我遇到了一个错误,无法解决这个问题...下面的代码。

I would greatly appreciate any help you can give me in working this problem 我非常感谢您在解决此问题方面能给我的任何帮助

CS0161: '_Default.GetData(System.Data.Odbc.OdbcCommand)': not all code paths return a value CS0161:“ _ Default.GetData(System.Data.Odbc.OdbcCommand)”:并非所有代码路径都返回值

private DataTable GetData(OdbcCommand cmd)
{
    DataTable dt = new DataTable();
    OdbcDataAdapter sda = new OdbcDataAdapter();

    cmd.CommandType = CommandType.Text;
    cmd.Connection = con;

    try
    {
        con.Open();
        sda.SelectCommand = cmd;
        sda.Fill(dt);

        if (dt.Rows.Count > 0)
        {
            return dt;
        }
        else
        {
            string strScript = "<script>" + "alert('Not found!.');";
            strScript += "window.location='default.aspx';";
            strScript += "</script>";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Startup", strScript);
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        con.Close();
        sda.Dispose();
        con.Dispose();
    }
}

Well, the compiler already told you what your problem is. 好了,编译器已经告诉您问题所在了。 You only return the DataTable if certain requirements are met. 仅在满足某些要求时才返回DataTable However a non-void method most return something in all possible code paths. 但是,非无效方法通常会在所有可能的代码路径中返回某些内容。

只需在结束括号之前添加

return dt;

The if clause checking for rows in the data table excludes the rest of the method from returning the signature the method dictates to be returned. 如果if子句检查数据表中的行,则该方法的其余部分将不返回该方法指示要返回的签名。 You most likely want to get rid of the if/else clause and just return the data table regardless of the row count. 您很可能希望摆脱if / else子句,而只返回数据表,而不管行数如何。 Your code calling the method should be doing the checking for row count and rendering the alert. 您的调用该方法的代码应进行行计数检查并呈现警报。 I would also highly recommend using StringBuilder when concatenating strings like that... Or since the string seems to be static text, use it as such with a constant. 我还强烈建议在连接这样的字符串时使用StringBuilder ...或由于字符串似乎是静态文本,因此可以将其与常量一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM