简体   繁体   中英

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)? SQL Server hasn't rights to create (write) *.bak file in user folder, but program can do this. 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. 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. Find out what user is being used (maybe procexp.exe?) and add the necessary rights.

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). Then you can load this VDI into SQL Server and backup into it. See SHEDDING LIGHT on VSS & VDI Backups in SQL Server . Have alook at SQL Server Compressed Backup .

Of course is very unlikely this is what you want, folk who implement VDI do not need to ask such questions. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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