以编程方式创建SQL Server 2005数据库的备份。 [英] Create backup of SQL Server 2005 database programmatically.
问题描述
我使用C#和SQL Server 2005开发了一个Windows窗体应用程序。它在客户端计算机上运行并正常运行。现在,客户端告诉我通过应用创建数据库备份工具来更新应用程序。我想以编程方式或通过编写一些代码来应用此工具。我不知道该怎么做。请有人帮助我。
I developed a Windows Form Application by using C# and SQL Server 2005. It is running and functioning properly on the client machine. Now the client told me to update the application by applying a facility of "Creating Backup of database". I want to apply this facility programmatically or by writing some code. I don't know how to do this. Please someone help me.
推荐答案
您好
是的,你可以很容易地做到这一点,请看看链接请
使用C#和.NET进行SQL Server 2005数据库备份和还原2.0 [ ^ ]
祝你好运。
Hi
Yes you can do it really easily just take a look follow of link please
SQL Server 2005 Database Backup and Restore using C# and .NET 2.0[^]
Best regards.
你必须拥有以下参考资料
you have to have the following references
using Microsoft.SqlServer.Management.Smo;
更多名称空间Microsoft.sqlserver.smoextended is实现在一起叫做Microsoft.sqlserver.smoextended.dll,如果你有SMO,应该在目录C:\program Files \ microsros SQL Server \\\\sdk \assemblies \中发现它安装。
现在代码是
what's more the namespace Microsoft.sqlserver.smoextended is actualized in the get together called Microsoft.sqlserver.smoextended.dll which ought to be discovered in the catalog C:\program Files\microsoft SQL Server\100\sdk\assemblies\ in the event that you have SMO installed.
now the code is
private void btn_Start_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(saveFileDialog1.FileName))
{
MessageBox.Show("Select file first!");
}
else
{
// selection.restoreDatabase();
//int num = selection.backupDatabase(saveFileDialog1.FileName);
//this.Close();
// MessageBox.Show(num.ToString());
//MessageBox.Show("DONE");
BackupDb();
}
}
private void btnBackupDb_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(saveFileDialog1.FileName))
{
MessageBox.Show("Select file first!");
}
else
{
BackupDb();
}
}
private void BackupDb()
{
dbName = ((Database)cmbBackupDb.SelectedItem).Name;
Backup dbBackup = new Backup();
try
{
dbBackup.Action = BackupActionType.Database;
dbBackup.Database = dbName;
dbBackup.BackupSetName = string.Format("{0} backup set.", dbName);
dbBackup.BackupSetDescription = string.Format("Database: {0}. Date: {1}.", dbName, DateTime.Now.ToString("dd.MM.yyyy hh:m"));
dbBackup.MediaDescription = "Disk";
BackupDeviceItem device = new BackupDeviceItem(saveFileDialog1.FileName, DeviceType.File);
dbBackup.Devices.Add(device);
txtBackupSql.Text = dbBackup.Script(sqlServer);
progBar.Visible = true;
progBar.Value = 0;
//get server id
string myHost = System.Net.Dns.GetHostName();
var ip_Address = Dns.GetHostEntry(myHost).AddressList.First(ipAddress => ipAddress.AddressFamily == AddressFamily.InterNetwork);
if(ip_Address.ToString()=="192.168.1.10".ToString())
{
dbBackup.Complete += new ServerMessageEventHandler(dbBackup_Complete);
dbBackup.PercentCompleteNotification = 5;
dbBackup.PercentComplete += new PercentCompleteEventHandler(dbBackup_PercentComplete);
dbBackup.SqlBackup(sqlServer);
progBar.Visible = false;
}
else
{
MessageBox.Show("You can only create or restore backup only from server computer!");
}
this.Close();
}
catch (Exception exc)
{
dbBackup.Abort();
MessageBox.Show(string.Format("Exception occured.\nMessage: {0}", exc.Message));
}
finally
{
sqlConn.Close();
}
}
void dbBackup_PercentComplete(object sender, PercentCompleteEventArgs e)
{
if (progBar.Value < progBar.Maximum)
{
if ((progBar.Value + e.Percent) <= 100)
{
progBar.Value += e.Percent;
}
else
progBar.Value = 100;
}
}
void dbBackup_Complete(object sender, ServerMessageEventArgs e)
{
MessageBox.Show("Backup complete");
lbl_confirm.Text = "DATABASE SUCCESSFULLY BACKED UP";
lbl_confirm.Visible = true;
lbl_confirm.ForeColor = Color.Green;
}
这很重要:
用C#填充SQL数据库 [ ^ ]
这篇关于以编程方式创建SQL Server 2005数据库的备份。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!