在c#中利用SQLDMO备份恢复数据库
Code
里面注意:
1:在项目工程中引用sqldmo
2: 无论是备份路径还是恢复路径,都要使用"[]”把路径括起来,要不路径有空格就会报错
1
private void buttonShuJuHuiFu_Click(object sender, EventArgs e)2

{3
DialogResult result = openFileDialogShuJuHuiFu.ShowDialog();4
if (result == DialogResult.OK)5

{6
formProgress = new FormProgress();7
string strBeiFenWenJian = openFileDialogShuJuHuiFu.FileName;8
formProgress.Show();9
System.Windows.Forms.Application.DoEvents();10
if (RestoreDB(server, user, password, "consumer", strBeiFenWenJian, formProgress.progressBar1))11

{12
MessageBox.Show("数据库恢复成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);13
}14
formProgress.Close();15
}16
17
18
}19

20
private void buttonShuJuBeiFen_Click(object sender, EventArgs e)21

{22
formProgress = new FormProgress();23
DialogResult result = folderBrowserDialogShuJuBeiFen.ShowDialog(); 24
if (result == DialogResult.OK)25

{26
textBoxShuJuBeiFen.Text = folderBrowserDialogShuJuBeiFen.SelectedPath;27
}28
string strBeiFenLuJing = textBoxShuJuBeiFen.Text.Trim();29
if (strBeiFenLuJing.Length > 0)30

{31
if (!Directory.Exists(strBeiFenLuJing))32

{33
strBeiFenLuJing = Application.StartupPath ; 34
}35
}36
else37

{38
strBeiFenLuJing = Application.StartupPath;39
}40
formProgress.Show();41
System.Windows.Forms.Application.DoEvents();42
if (BackUPDB(server, user, password, "consumer", strBeiFenLuJing + @"\" + "consumer" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".bak", formProgress.progressBar1))43

{ 44
MessageBox.Show("数据库备份成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);45
}46
formProgress.Close();47
}48

49
private void button3_Click(object sender, EventArgs e)50

{51
this.Close();52
}53

/**//// <summary>54
/// 数据库的备份和实时进度显示55
/// </summary>56
/// <param name="ServerName">服务器名称</param>57
/// <param name="UserName">用户名</param>58
/// <param name="Password">用户密码</param>59
/// <param name="strDbName">数据库名称</param>60
/// <param name="strFileName">数据库文件</param>61
/// <param name="pgbMain">进度条</param>62
/// <returns></returns>63
public bool BackUPDB(string ServerName, string UserName, string Password, string strDbName, string strFileName, ProgressBar pgbMain)64

{ 65
strFileName = "[" + strFileName + "]";66
SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass();67
try68

{69
svr.LoginSecure = false;70
svr.Connect(ServerName, UserName, Password);71
SQLDMO.Backup bak = new SQLDMO.BackupClass();72
bak.Action = 0 ;73
bak.Initialize = true;74
SQLDMO.BackupSink_PercentCompleteEventHandler pceh = new SQLDMO.BackupSink_PercentCompleteEventHandler(formProgress.Step);75
bak.PercentComplete += pceh;76
bak.Files = strFileName;77
bak.Database = strDbName;78
bak.SQLBackup(svr);79

80
return true;81
}82
catch (Exception err)83

{84
throw (new Exception("备份数据库失败" + err.Message));85
}86
finally87

{88
svr.DisConnect();89
}90
}91

92

/**//// <summary>93
/// 数据库的恢复94
/// </summary>95
/// <param name="ServerName">服务器名称</param>96
/// <param name="UserName">用户名</param>97
/// <param name="Password">用户密码</param>98
/// <param name="strDbName">数据库名称</param>99
/// <param name="strFileName">数据库文件</param>100
/// <param name="pgbMain">进度条</param>101
/// <returns></returns>102
public bool RestoreDB(string ServerName, string UserName, string Password, string strDbName, string strFileName, ProgressBar pgbMain)103

{104
strFileName = "[" + strFileName + "]";105
SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass();106
try107

{108
svr.LoginSecure = false;109
svr.Connect(ServerName, UserName, Password);110
SQLDMO.QueryResults qr = svr.EnumProcesses(-1);111
int iColPIDNum = -1;112
int iColDbName = -1;113
for (int i = 1; i <= qr.Columns; i++)114

{115
string strName = qr.get_ColumnName(i);116
if (strName.ToUpper().Trim() == "SPID")117

{118
iColPIDNum = i;119
}120
else if (strName.ToUpper().Trim() == "DBNAME")121

{122
iColDbName = i;123
}124
if (iColPIDNum != -1 && iColDbName != -1)125
break;126
}127

128
for (int i = 1; i <= qr.Rows; i++)129

{130
int lPID = qr.GetColumnLong(i, iColPIDNum);131
string strDBName = qr.GetColumnString(i, iColDbName);132
if (strDBName.ToUpper() == strDbName.ToUpper())133
svr.KillProcess(lPID);134
}135

136

137
SQLDMO.Restore res = new SQLDMO.RestoreClass();138
res.Action = 0;139
SQLDMO.RestoreSink_PercentCompleteEventHandler pceh = new SQLDMO.RestoreSink_PercentCompleteEventHandler(formProgress.Step);140
res.PercentComplete += pceh;141
res.Files = strFileName;142

143
res.Database = strDbName;144
res.ReplaceDatabase = true;145
res.SQLRestore(svr);146
return true;147
}148
catch (Exception err)149

{150
throw (new Exception("恢复数据库失败,请关闭所有和该数据库连接的程序!" + err.Message));151
}152
finally153

{154
svr.DisConnect();155
}156
}
浙公网安备 33010602011771号