如何使用asp.net c#从SQL Server Management下载文件?

时间:2011-08-11 03:49:11

标签: c# asp.net visual-studio

任何人都可以帮我解决如何下载上传文件的代码,该文件是csv格式并存储在SQL Server Management中。我目前正在使用asp.net,c#,并在Microsoft Visual Studio中执行此操作。请帮忙!谢谢!

2 个答案:

答案 0 :(得分:0)

您可以这样做是在Excel中打开CSV,在SQL MS中打开要添加的表,然后只需复制和粘贴。只要列数相同就应该工作。

答案 1 :(得分:0)

您需要获取数据,在字符串中创建逗号分隔列表,然后将其流式传输到用户浏览器。

StringBuilder sw = new StringBuilder();
//assuming you have a datatable named dt
int NumColumns = dt.Columns.Count;
    for (int i = 0; i < NumColumns; i++)
    {
        sw.Write(dt.Columns[i]);
        if (i < dt.Columns.Count - 1)
        {
            sw.Write(",");
        }
    }

    sw.Write(sw.NewLine);
    // write = the rows.
    foreach (DataRow dr in dt.Rows)
   {
        for (int i = 0; i < NumColumns; i++)
        {
            sw.Write(dr[i].ToString());
            if (i < NumColumns - 1)
            {
                sw.Write(",");
            }
        }
        sw.Write(sw.NewLine);
    }



    Response.Clear();
    Response.ClearHeaders();
    Response.ClearContent();
    Response.AddHeader("content-disposition", "attachment; filename=file.csv");
    Response.ContentType = "text/csv";
    Response.AddHeader("Pragma", "public");
    Response.Write(sw.ToString());
    Response.Write(sw.NewLine);
    Response.End();