【问题标题】:Export to csv has Object reference not set to an instance of an object导出到 csv 的对象引用未设置为对象的实例
【发布时间】:2014-06-27 21:04:47
【问题描述】:

我正在尝试从 SQL Server 导出查询中的 csv 文件。

我收到错误 Object reference not set to an instance of an object on the line

foreach (DataColumn dc in dt.Columns)
{
}

这是我为此运行的代码。

private DataTable GetData()
{
    SqlConnection sqlCon = new SqlConnection("...");
    SqlCommand sqlCmd = new SqlCommand();
    sqlCmd.CommandText = "SELECT top 10 * from products.products";
    sqlCmd.Connection = sqlCon;

    sqlCon.Open();
    sqlCmd.ExecuteScalar();
    sqlCon.Close();
    return dt;
}

protected void Button3_Click(object sender, EventArgs e)
{
    DataTable dt = GetData();

    string attachment = "attachment; filename=DataTable.xls";

    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/vnd.ms-excel";

    string tab = "";
    foreach (DataColumn dc in dt.Columns)
    {
        Response.Write(tab + dc.ColumnName);
        tab = "\t";
    }

    Response.Write("\n");

    foreach (DataRow dr in dt.Rows)
    {
        tab = "";

        for (int i = 0; i < dt.Columns.Count; i++)
        {
            Response.Write(tab + dr[i].ToString());
            tab = "\t";
        }

        Response.Write("\n");
    }

    Response.End();
}

我不知道该怎么办。

【问题讨论】:

  • dt.Columns 中有什么?你调试了吗?
  • 它编译没有错误? DataTable GetData() 没有 dt 声明。 ExecuteScalar 返回一个...标量而不是表;)
  • 是的,编译时没有错误

标签: c# asp.net sql-server csv ado.net


【解决方案1】:

你必须用一些东西填写你的DataTable

private DataTable GetData()
{
    DataTable dt = new DataTable();

    SqlConnection sqlCon = new SqlConnection("...");
    SqlCommand sqlCmd = new SqlCommand();
    sqlCmd.CommandText = "SELECT top 10 * from products.products";
    sqlCmd.Connection = sqlCon;

    sqlCon.Open();
    dt.Load(sqlCmd.ExecuteReader());
    sqlCon.Close();

    return dt;
}

【讨论】:

  • 在 dt.Load(sqlCmd.ExecuteReader()); 上出现错误- 对象引用未设置为对象的实例。
  • DataTable dt = new DataTable() 我确实在方法中错过了它:)。见上文!
  • 非常感谢! :) 我会在 2 分钟内选择答案。
【解决方案2】:

试试这个:

private DataTable GetData()
{
DataTable dt;
        string connectionString = ConfigurationManager.ConnectionStrings["yourConnection"].ConnectionString;
        //
        // In a using statement, acquire the SqlConnection as a resource.
        //
        using (SqlConnection con = new SqlConnection(connectionString))
        {
            //
            // Open the SqlConnection.
            //
            con.Open();
            //
            // The following code uses an SqlCommand based on the SqlConnection.
            //
            using (SqlCommand command = new SqlCommand("SELECT top 10 * from products.products", con))
            {
                //use SqlDataAdapter to fill the dataTable
                using (SqlDataAdapter a = new SqlDataAdapter(command))
                {
                    dt = new DataTable();
                    a.Fill(dt);
                }

            }
        } 
        return dt;
    }

【讨论】:

    猜你喜欢
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-22
    • 1970-01-01
    相关资源
    最近更新 更多