[英]How to save the data in a Client machine directory from SQL Server database of a .net application?
I have written the following C# code using console application to fetch the data from database and save it in a .CSV
file. 我已经使用控制台应用程序编写了以下C#代码,以从数据库中获取数据并将其保存在
.CSV
文件中。
My question is: here I am saving to an EXCEL file which already exists on my local machine. 我的问题是:在这里,我将保存到本地计算机上已经存在的EXCEL文件。 But I wish that the code should create a file in one of the client machine directories and save the data.
但是我希望代码可以在客户端计算机目录之一中创建一个文件并保存数据。 How to change this code accordingly?
如何相应地更改此代码?
I use this code in asp.net application for BUTTON_Click EVENT. 我在asp.net应用程序中将此代码用于BUTTON_Click EVENT。 Do I need to add any namespaces at that point of time?!!
那时候我需要添加任何名称空间吗?!
class Program
{
static void Main(string[] args)
{
string strConn = "Data Source=ARYAANCLASS;Initial Catalog=Master;Integrated Security=True";
SqlConnection conn = new SqlConnection(strConn);
SqlDataAdapter da = new SqlDataAdapter("select * from QuickBook", conn);
DataSet ds = new DataSet();
da.Fill(ds, "QuickBooks");
DataTable dt = ds.Tables["QuickBooks"];
StreamWriter sw = new StreamWriter(@"d:\Rudresh\QuickBookData.csv", false);
int iColCount = dt.Columns.Count;
for (int i = 0; i < iColCount; i++)
{
sw.Write(dt.Columns[i]);
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < iColCount; i++)
{
if (!Convert.IsDBNull(dr[i]))
{
sw.Write(dr[i].ToString());
}
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
}
sw.Close();
}
}
I don't think you understand what client/server-sides means.. 我认为您不了解客户端/服务器端的含义。
The program you have written is a client program, so if a user opens the executable on his Computer it should work(more or less there are some improvements like: testing if the write folder exists else create it). 您编写的程序是一个客户端程序,因此,如果用户在其计算机上打开可执行文件,它应该可以工作(或多或少有一些改进,例如:测试写入文件夹是否存在,否则创建它)。
Also he must be able to connect to the database so a "try catch"-block around the sql connection is not a bad plan. 他还必须能够连接到数据库,因此围绕SQL连接的“ try catch”块不是一个坏计划。
If you are trying to create a webpage, it is a total different story. 如果您尝试创建网页,则完全不同。 It is not possible to "save" the file to a client location.
无法将文件“保存”到客户位置。 Then you have to save it on your server in the folder accessible for a web user like in the www/savedcsv folder.
然后,您必须将其保存在服务器上的Web用户可访问的文件夹中,例如www / savedcsv文件夹中。 Then when the stream is finished, redirect the "web user" to folder www.mydomain.com/savedcsv/newfile.csv and he will be able to download it.
然后,当流完成时,将“ Web用户”重定向到文件夹www.mydomain.com/savedcsv/newfile.csv,他将能够下载它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.