本文介绍了完全备份&使用SMO c#sqlserver 2008或2012恢复数据库网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我编写备份代码,但我调试代码找不到数据库但有错误 请帮帮我 // 添加参考 使用 Microsoft.SqlServer.Management.Smo; 使用 Microsoft.SqlServer.Management.Smo.Wmi; 使用 Microsoft.SqlServer.Management.Common; 使用 Microsoft.SqlServer.Management.Smo.Agent; 使用 Microsoft.SqlServer.Management.Smo.Broker; 使用 Microsoft.SqlServer.Management.Smo.Mail; 使用 Microsoft.SqlServer.Management.Smo.RegisteredServers; 使用 Microsoft.SqlServer.Management.Trace; // 连接 SqlConnection sq = new SqlConnection(ClassAccessDatac.con); // 代码btn private void btnBackup_Click( object sender,EventArgs e) { string MethodeName = btnBackup_Click ; 尝试 { string strFileName = 串 .Empty; saveFileDialog1.DefaultExt = mdf; saveFileDialog1.FileName = BackupFile + un1.OnUniq(); saveFileDialog1.Filter = @ SQL备份文件*。* |所有文件(*。*); saveFileDialog1.FilterIndex = 1 ; saveFileDialog1.OverwritePrompt = true ; saveFileDialog1.Title = 备份SQL文件; if (saveFileDialog1.ShowDialog()== DialogResult.OK) { strFileName = saveFileDialog1.FileName; BackupDatabase( BozorgMehr, sa, 09124863440, SERVER001,strFileName); 备份(strFileName); } } catch (例外情况) { Keys.RegError(MethodeName,ex.ToString(),sq); } } public Microsoft.SqlServer.Management.Smo.Database db = new 数据库(); public void BackupDatabase(字符串 databaseName,字符串 userName, 字符串密码,字符串 serverName,字符串 destinationPath) { try { 备份sqlBackup = 新备份(); sqlBackup.Action = BackupActionType.Database; sqlBackup.BackupSetDescription = ArchiveDataBase: + DateTime.Now.ToShortDateString (); sqlBackup.BackupSetName = 存档; sqlBackup.Database = databaseName; BackupDeviceItem deviceItem = new BackupDeviceItem(destinationPath,DeviceType.File); ServerConnection connection = new ServerConnection(serverName,userName,password); Server sqlServer = new 服务器(连接); 数据库db = sqlServer.Databases [databaseName]; sqlBackup.Initialize = true ; sqlBackup.Checksum = true ; sqlBackup.ContinueAfterError = true ; sqlBackup.Devices.Add(deviceItem); sqlBackup.Incremental = false ; sqlBackup.ExpirationDate = DateTime.Now.AddDays( 3 ); sqlBackup.LogTruncation = BackupTruncateLogType.Truncate; sqlBackup.FormatMedia = false ; sqlBackup.SqlBackup(sqlServer); } catch (例外情况) { Keys .RegError(MethodeName,ex.ToString(),sq); } } 解决方案 您没有提供有关您的问题的任何详细信息......但是,有stil有机会帮助你。 以下是使用SMO的备份数据库的完整示例: SQL Server 2008 - 使用SMO备份和恢复数据库 [ ^ ] 使用SMO以编程方式备份​​和还原SQL Server数据库 [ ^ I write code for back up but I debug code don't find database and have error please help me // add refusing Microsoft.SqlServer.Management.Smo;using Microsoft.SqlServer.Management.Smo.Wmi;using Microsoft.SqlServer.Management.Common;using Microsoft.SqlServer.Management.Smo.Agent;using Microsoft.SqlServer.Management.Smo.Broker;using Microsoft.SqlServer.Management.Smo.Mail;using Microsoft.SqlServer.Management.Smo.RegisteredServers;using Microsoft.SqlServer.Management.Trace;// ConnectionSqlConnection sq = new SqlConnection(ClassAccessDatac.con);// behind code btn private void btnBackup_Click(object sender, EventArgs e) { string MethodeName = "btnBackup_Click"; try { string strFileName = string.Empty; saveFileDialog1.DefaultExt = "mdf"; saveFileDialog1.FileName = "BackupFile" + un1.OnUniq(); saveFileDialog1.Filter = @"SQL Backup files *.*|All files(*.*) "; saveFileDialog1.FilterIndex = 1; saveFileDialog1.OverwritePrompt = true; saveFileDialog1.Title = "Backup SQL File"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { strFileName = saveFileDialog1.FileName; BackupDatabase("BozorgMehr", "sa", "09124863440", "SERVER001", strFileName); Backup(strFileName); } } catch (Exception ex) { Keys.RegError(MethodeName, ex.ToString(), sq); } } public Microsoft.SqlServer.Management.Smo.Database db = new Database(); public void BackupDatabase(String databaseName, String userName, String password, String serverName, String destinationPath) { try{ Backup sqlBackup = new Backup(); sqlBackup.Action = BackupActionType.Database; sqlBackup.BackupSetDescription = "ArchiveDataBase:" + DateTime.Now.ToShortDateString(); sqlBackup.BackupSetName = "Archive"; sqlBackup.Database = databaseName; BackupDeviceItem deviceItem = new BackupDeviceItem(destinationPath, DeviceType.File); ServerConnection connection = new ServerConnection(serverName, userName, password); Server sqlServer = new Server(connection); Database db = sqlServer.Databases[databaseName]; sqlBackup.Initialize = true; sqlBackup.Checksum = true; sqlBackup.ContinueAfterError = true; sqlBackup.Devices.Add(deviceItem); sqlBackup.Incremental = false; sqlBackup.ExpirationDate = DateTime.Now.AddDays(3); sqlBackup.LogTruncation = BackupTruncateLogType.Truncate; sqlBackup.FormatMedia = false; sqlBackup.SqlBackup(sqlServer); } catch (Exception ex) { Keys.RegError(MethodeName, ex.ToString(), sq); } } 解决方案 You did not provide any details about your issue... Nevertheless, there is stil the chance to help you.Here is a complete sample of backuping database by using SMO: SQL Server 2008 - Backup and Restore Databases using SMO[^]Backup and Restore SQL Server databases programmatically with SMO[^] 这篇关于完全备份&使用SMO c#sqlserver 2008或2012恢复数据库网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-21 16:04