简体   繁体   English

SQL Server使用C#备份到文件

[英]SQL Server backup to file with C#

Is it possible to create backup file from a SQL Server database programmatically with C# (without using SMO or changing permissions)? 是否可以使用C#以编程方式(不使用SMO或更改权限)从SQL Server数据库创建备份文件? SQL Server hasn't rights to create (write) *.bak file in user folder, but program can do this. SQL Server无权在用户文件夹中创建(写入) *.bak文件,但是程序可以执行此操作。 So I want to create some query to database that will give dump of database, that I can write in file. 因此,我想对数据库创建一些查询,该查询将给出数据库转储,并可以在文件中写入。

Something like that: 像这样:

SqlCommand sc = new SqlCommand(@"backup database test1 to stream", cnnct);
SqlDataReader r = sc.ExecuteReader();
while(r.read())file.WriteLine(reader.GetString(0));

This is neither possible nor a good idea. 这既不可能也不是一个好主意。 MSSQL databases are too complex to be reliably dumped as SQL. MSSQL数据库过于复杂,无法可靠地作为SQL转储。 I know of no tool that gets this right all the time. 我不知道有什么工具能始终做到这一点。

The right way to deal with this is to give the SQL Server user the rights to access that folder. 解决此问题的正确方法是授予SQL Server用户访问该文件夹的权限。 Find out what user is being used (maybe procexp.exe?) and add the necessary rights. 找出正在使用的用户(也许是procexp.exe?)并添加必要的权限。

YEs it is, but is way more complicated than you think. 的确是这样,但比你想象更复杂。 You need to implement a VDI specification component (which is by definition native code, not managed). 您需要实现一个VDI规范组件(按定义是本机代码,而不是托管的)。 Then you can load this VDI into SQL Server and backup into it. 然后,您可以将此VDI加载到SQL Server中并备份到其中。 See SHEDDING LIGHT on VSS & VDI Backups in SQL Server . 请参见SQL Server中有关VSS和VDI备份的摘要 Have alook at SQL Server Compressed Backup . 看一下SQL Server压缩备份

Of course is very unlikely this is what you want, folk who implement VDI do not need to ask such questions. 当然这不是您想要的,实施VDI的人员不需要提出这样的问题。 So what is what you really try to do? 那么,您真正想做什么呢? It seems that what you want is more to Script out the database, for which there is SMO Scripter . 似乎您想要的更多是对数据库进行脚本编写 ,为此提供了SMO Scripter

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

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