下载SQL Server表的内容,并将其放入CSV文件中? [英] Download contents of SQL Server table, and put the contents in to a CSV file?
问题描述
我有一个应该包含下载功能的项目,当用户按下下载按钮时,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屋!