下载SQL Server表的内容,并将其放入CSV文件中? [英] Download contents of SQL Server table, and put the contents in to a CSV file?

查看:176
本文介绍了下载SQL Server表的内容,并将其放入CSV文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应该包含下载功能的项目,当用户按下下载按钮时,SQL Server表的内容将作为csv文件下载.

有什么办法可以做到这一点?

名为Characters的表的内容将作为csv文件下载.我所知道的是我已经使用查询Select* from Characters,但是我不知道如何实现它.

        protected void btnDownload_Click(object sender, EventArgs e)
    {

        string fileName = string.Empty;
        string filepath = Request.MapPath("~/Uploads");// this needs to be changed with the SQL query, but I do not know how to implement it
        string downloadFileName = "Attendance.zip";
        Response.ContentType = "application/zip";
        Response.AddHeader("content-disposition", "filename=" + downloadFileName);

        using (ZipFile zip = new ZipFile())
        {
            foreach (GridView row in gvFiles.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("chkSelect");
                if (cb != null && cb.Checked)
                {
                    fileName = (row.FindControl("lblFileName") as Label).Text;
                    zip.AddFile(Server.MapPath(Path.Combine(filepath, fileName)), "");
                }
            }
            zip.Save(Response.OutputStream);
        }

解决方案

您对环境的了解不多.例如:

  • 您使用哪种数据访问技术?
  • 您是否必须转义csv文件中的字段?

对于处理csv文件,我建议使用 filehelpers库.

如果您有选择数据访问的自由,也许您应该看看 LINQ2SQL ADO EF 或仅使用普通的SqlCommand/SqlDataReader./p>

I have a project which should include a download function whereby when the user press the download button, the contents of the SQL Server table will be downloaded as a csv file.

Is there any way I can do this?

The contents of the table called Characters will be downloaded as a csv file. What I know is that I have use the query Select* from Characters, but I do not know how to implement it.

        protected void btnDownload_Click(object sender, EventArgs e)
    {

        string fileName = string.Empty;
        string filepath = Request.MapPath("~/Uploads");// this needs to be changed with the SQL query, but I do not know how to implement it
        string downloadFileName = "Attendance.zip";
        Response.ContentType = "application/zip";
        Response.AddHeader("content-disposition", "filename=" + downloadFileName);

        using (ZipFile zip = new ZipFile())
        {
            foreach (GridView row in gvFiles.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("chkSelect");
                if (cb != null && cb.Checked)
                {
                    fileName = (row.FindControl("lblFileName") as Label).Text;
                    zip.AddFile(Server.MapPath(Path.Combine(filepath, fileName)), "");
                }
            }
            zip.Save(Response.OutputStream);
        }

解决方案

You havn't told much about your environment. For example:

  • Which data access technology do you use ?
  • Do you have to escape the fields in your csv file?

For dealing with csv files i recommend using the filehelpers library.

If you are free in choosing data access, maybe you should take a look at LINQ2SQL or ADO EF or just use plain SqlCommand/SqlDataReader.

这篇关于下载SQL Server表的内容,并将其放入CSV文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆