如何用C#编写 实现数据备份和还原
我正在做物业管理系统中的“系统管理”模块,需要实现数据库的备份和还原。请问:在Microsoftvisualstudio中,用C#语言编写,如何通过“浏览”按钮,从电脑中...
我正在做物业管理系统中的 “系统管理” 模块,需要实现数据库的备份和还原。请问:在Microsoft visual studio中,用C#语言编写,如何通过“浏 览”按钮,从电脑中提取URL写入文本框,然后通过“备份”和“还原”实现相应的 备份数据库和还原数据库 功能。紧急!
展开
1个回答
2013-07-24
展开全部
///<summary>
/// 数据库备份
/// 参数信息要备份的数据库的名称Backup .Database = "Northwind";
/// 另外一个是存放备份文件的路径和文件名称Backup .Files = @"c:\databak.bak";
///</summary>
protected void backupButton_Click(object sender, EventArgs e)
{
SQLDMO.Backup Backup = new SQLDMO.BackupClass(); SQLDMO.SQLServer SQLServer = new SQLDMO.SQLServerClass();
string pathname =Label1.Text+pathTextBox.Text.Trim().ToString();
try
{
SQLServer.LoginSecure = false;
SQLServer.Connect(@".\SQLEXPRESS", "sa", "123");
Backup .Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
Backup.Database = "JJGoodShopDB";
//要备份的数据库的名称,例如Northwind
Backup.Files =Server.MapPath( @"~\Backup\"+pathname+".bak");
//存放路径,和备份文件的名称
Backup.BackupSetName = "JJGoodShopDB";
Backup .BackupSetDescription = "数据库备份";
Backup .Initialize = true;
Backup .SQLBackup(SQLServer);
backupButton.Enabled = false;
infoLabel.Text = "数据库备份成功!!<br>文件成功保存在了" + Backup.Files;
}catch(Exception eee)
{
infoLabel.Text = "数据库备份失败!!<br>"+eee.Message;
//throw eee;
}
finally
{
SQLServer.DisConnect();
}
}
}
要全的还有, //还原SqlServer数据库:
string path = this.FileUpload1.PostedFile.FileName; //获得备份路径及数据库名称
string SqlStr1 = System.Web.Configuration
.WebConfigurationManager
.ConnectionStrings["JJGoodShopDBConnectionString"]
.ConnectionString;
string SqlStr2 = "use master restore database JJGoodShopDB from disk='" + path + "'";
SqlConnection con = new SqlConnection(SqlStr1);
con.Open();
try
{
SqlCommand com = new SqlCommand(SqlStr2, con);
com.ExecuteNonQuery();
Label1.Text = "还原数据成功!";
Button1.Enabled = false;
}
catch (Exception error)
{
Label1.Text = "还原数据失败!<br>" + error.Message ;
}
finally
{
con.Close();
}//这是还原数据库
/// 数据库备份
/// 参数信息要备份的数据库的名称Backup .Database = "Northwind";
/// 另外一个是存放备份文件的路径和文件名称Backup .Files = @"c:\databak.bak";
///</summary>
protected void backupButton_Click(object sender, EventArgs e)
{
SQLDMO.Backup Backup = new SQLDMO.BackupClass(); SQLDMO.SQLServer SQLServer = new SQLDMO.SQLServerClass();
string pathname =Label1.Text+pathTextBox.Text.Trim().ToString();
try
{
SQLServer.LoginSecure = false;
SQLServer.Connect(@".\SQLEXPRESS", "sa", "123");
Backup .Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
Backup.Database = "JJGoodShopDB";
//要备份的数据库的名称,例如Northwind
Backup.Files =Server.MapPath( @"~\Backup\"+pathname+".bak");
//存放路径,和备份文件的名称
Backup.BackupSetName = "JJGoodShopDB";
Backup .BackupSetDescription = "数据库备份";
Backup .Initialize = true;
Backup .SQLBackup(SQLServer);
backupButton.Enabled = false;
infoLabel.Text = "数据库备份成功!!<br>文件成功保存在了" + Backup.Files;
}catch(Exception eee)
{
infoLabel.Text = "数据库备份失败!!<br>"+eee.Message;
//throw eee;
}
finally
{
SQLServer.DisConnect();
}
}
}
要全的还有, //还原SqlServer数据库:
string path = this.FileUpload1.PostedFile.FileName; //获得备份路径及数据库名称
string SqlStr1 = System.Web.Configuration
.WebConfigurationManager
.ConnectionStrings["JJGoodShopDBConnectionString"]
.ConnectionString;
string SqlStr2 = "use master restore database JJGoodShopDB from disk='" + path + "'";
SqlConnection con = new SqlConnection(SqlStr1);
con.Open();
try
{
SqlCommand com = new SqlCommand(SqlStr2, con);
com.ExecuteNonQuery();
Label1.Text = "还原数据成功!";
Button1.Enabled = false;
}
catch (Exception error)
{
Label1.Text = "还原数据失败!<br>" + error.Message ;
}
finally
{
con.Close();
}//这是还原数据库
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |