简体   繁体   English

使用C#将SQL Server数据库备份到文本文件(如.csv)

[英]Backup SQL Server database to a text file(like .csv) using c#

I am creating a program that uses a simple database with only a few tables. 我正在创建一个仅使用几个表的简单数据库的程序。 I need to do a backup and restore of the database to and from a text file like a .csv . 我需要在与.csv类的文本文件之间进行数据库的备份和还原。 I have tried a couple of things but none seems to work. 我尝试了几件事,但似乎都没有用。 Then I came across the following line: 然后我遇到了以下几行:

 sqlcmd = new SqlCommand("backup database  " + dbname + " to disk='" +
 destdir + "\backup.bak", sqlcon);

And I am wondering if I can name it backup.csv instead of .bak . 我想知道是否可以将其命名为backup.csv而不是.bak I would also like to know what to include at the 'using' section. 我也想知道在“使用”部分中要包括的内容。 Any help would be appreciated. 任何帮助,将不胜感激。

You could name the output file whatever you like, but this will not make it a CSV file. 您可以随意命名输出文件,但这不会使它成为CSV文件。
The SQL Backup script produces a backup in a binary file and could be restored only using the appropriate tools like SQL Server Management Studio or a RESTORE script SQL Backup脚本会在二进制文件中生成备份,并且只能使用适当的工具(例如SQL Server Management Studio或RESTORE脚本)来还原

You don't have any particular needs for the using section. 您对using部分没有任何特殊需求。 The System.Data.SqlClient is the namespace that contains the classes SqlConnection and SqlCommand needed to execute the script System.Data.SqlClient是包含执行脚本所需的SqlConnectionSqlCommand类的名称空间

A possible workaround to reach (at least partially) you requirements is to use the BCP utility . 一种可能(至少部分地)满足要求的解决方法是使用BCP实用程序
Usually this utility is required when you need to import large amount of data from a storage medium to your Sql Server database. 通常,当您需要将大量数据从存储介质导入Sql Server数据库时,需要使用此实用程序。 But the BCP utility could also be used to export data from your database in a text file on disk. 但是BCP实用程序也可以用于从数据库中以磁盘上的文本文件导出数据。 However BCP requires the name of a single datatable or a SQL Query that returns a single set of data, so you probably need to call it more than one time. 但是BCP需要单个数据表的名称或返回单个数据集的SQL查询的名称,因此您可能需要多次调用它。
Notice also that BCP is not a Sql command but a standalone utility installed with SQL Server. 还要注意,BCP不是Sql命令,而是随SQL Server安装的独立实用程序。

As an example (for a better explanation of the parameters check the link above) you could write this code 作为示例(有关参数的更好说明,请参见上面的链接),您可以编写此代码

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "bcp.exe";
psi.Arguments = @"TableOrQueryToExtract out d:\temp\myTableData.txt -T -d MyDataBaseName -w";
Process.Start(psi);

As you can see, you need to call this code for each table you need to export and save that data in separate files on disk (or write a complex program that capture the BCP output and joins everything together. At the end of the day, if you don't have a specific requirement to get a text file, I really suggest to opt for the simplicity of the BACKUP command 如您所见,您需要为每个表调用此代码,这些表需要将数据导出并将其保存在磁盘上的单独文件中(或编写一个复杂的程序来捕获BCP输出并将所有内容连接在一起。)如果您对获取文本文件没有特殊要求,我真的建议您选择BACKUP命令的简单性

You should take a look at Backup SQL using C# .It is very helpful ,I dont think there should be any naming issue 您应该看一下使用C#的Backup SQL。这非常有帮助,我认为应该没有任何命名问题

also you can do it from sql Server 2005 take a look at this 您也可以从sql Server 2005中做到这一点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM