简体   繁体   English

如何从.net应用程序的SQL Server数据库中将数据保存在客户端计算机目录中?

[英]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.

相关问题 从客户端保存到sql server数据库 - save to sql server database from client side 客户端计算机上的SQL Server数据库 - SQL Server database on Client machine 如何从客户端计算机连接到SQL Server? - How to connect to SQL Server from a client machine? 如何授予SQL Server 2008数据库从C#Windows应用程序中的客户端系统插入数据的权限? - How to give the permissions to sql server 2008 database to insert the data from client system in c# windows application? 如何在连接字符串中添加数据目录以在客户端计算机上部署应用程序? - How to add data directory in connection string to deploy application on client machine? 如何在客户端计算机上使用SQL数据库应用程序运行Crystal Reports - How to run Crystal Reports with SQL Database Application on Client Machine 如何在客户端计算机上安装Windows应用程序和SQL Server? - How to install windows application and SQL server on the client machine? ASP.NET应用程序如何将最近添加的数据从SQL Server数据库复制到不同SQL Server上的另一个相似数据 - How ASP.NET application can copy recently added data from SQL Server database to another similar one on different SQL Server 如何减少来自.NET Windows应用程序的数据库(SQL Server)调用 - How to reduce database (SQL Server) calls from .NET windows application 来自Google文档的Excel使用asp.net C#读取数据并将其保存在SQL Server数据库中 - Excel from Google Docs read and save data in SQL Server database using asp.net c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM