我正在執行從/向Microsoft SQL Server恢復/備份數據庫的應用程序。以編程方式恢復多個數據庫
如何獲取捕獲源文件夾中所有.bak文件的效果,而目標數據庫的文本框用作新數據庫的新名稱並將其還原到sql服務器中?
我的驗證是,如果目標數據庫groupbox中的名稱,它會提示錯誤而不是恢復它。
這裏是我的代碼
CheckDBExist
public List<string> CheckIfDatabaseExists(string SQLServer, string backupRestore)
{
bool result = false;
List<string> DBList = new List<string>();
string sqlConnectionString = this.rbWindow.Checked ?
"Server=" + this.cboSQLServer.Text.Trim() + ";Database=master;Trusted_Connection=Yes" :
"Server=" + this.cboSQLServer.Text.Trim() + ";Database=master;uid=" + this.txtUsername.Text.Trim() + ";pwd=" + this.txtPassword.Text.Trim();
foreach (Control c in groupBox1.Controls)
{
if (c.GetType() == typeof(TextBox))
{
SqlConnection tmpConn = new SqlConnection(sqlConnectionString);
string sqlCreateDBQuery = string.Format("SELECT database_id FROM sys.databases WHERE Name = '{0}'", c.Text);
using (tmpConn)
{
using (SqlCommand sqlCmd = new SqlCommand(sqlCreateDBQuery, tmpConn))
{
tmpConn.Open();
object resultObj = sqlCmd.ExecuteScalar();
int databaseID = 0;
if (resultObj != null)
{
int.TryParse(resultObj.ToString(), out databaseID);
}
tmpConn.Close();
result = (databaseID > 0);
if ((!result) && (backupRestore == "backup"))
{
DBList.Add("[" + c.Text + "]");
}
else if ((result) && (backupRestore == "restore"))
{
DBList.Add("[" + c.Text + "]");
}
}
}
}
}
return DBList;
}
按鈕單擊
private void btnRestore_Click(object sender, EventArgs e)
{
string outputFolder = this.txtFolder.Text;
if (string.IsNullOrEmpty(outputFolder))
{
MessageBox.Show("Please select source folder!", "Empty Source Folder", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
List<string> ExistDB = new List<string>();
ExistDB = this.CheckIfDatabaseExists(this.cboSQLServer.Text, "restore");
if (ExistDB.Count == 0)
{
RestoreDatabase(this.cboSQLServer.Text, this.txtFolder.Text);
}
else
{
MessageBox.Show("Databases " + string.Join(", ", ExistDB) + " exist!", "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
恢復DB代碼
公共無效RestoreDatabase(字符串SQLSERVER,串outputFolder) {
ServerConnection con = new ServerConnection(SQLServer);
Server server = new Server(con);
foreach (Control c in groupBox3.Controls)
{
//try
//{
if (c.GetType() == typeof(TextBox))
{
Restore destination = new Restore();
destination.Action = RestoreActionType.Database;
destination.Database = c.Text;
string backUpFile = outputFolder + "\\" + destination.Database + ".bak";
BackupDeviceItem source = new BackupDeviceItem(backUpFile, DeviceType.File);
string logFile = Path.GetDirectoryName(backUpFile);
logFile = Path.Combine(logFile, destination.Database + "_Log.ldf");
string dataFile = Path.GetDirectoryName(backUpFile);
dataFile = Path.Combine(dataFile, destination.Database + ".mdf");
destination.Devices.Add(source);
DataTable logicalRestoreFiles = destination.ReadFileList(server);
destination.RelocateFiles.Add(new RelocateFile(logicalRestoreFiles.Rows[0][0].ToString(), dataFile));
destination.RelocateFiles.Add(new RelocateFile(logicalRestoreFiles.Rows[1][0].ToString(), logFile));
destination.ReplaceDatabase = true;
destination.SqlRestore(server);
}
//}
//catch (Exception ex)
//{
//MessageBox.Show(ex.Message);
//}
}
}
錯誤說:
「無法打開備份設備'D:\ TestBackup \ VSM642SP2QC__VfsWorkflow.bak'。 ?>操作系統錯誤2(系統無法找到文件>指定)。\ r \ nRESTORE FILELIST正在異常終止。」
我應該怎麼辦?
當其失敗;在第一個數據庫或之後? – Anil
這很奇怪。我從來沒有使用過這些類。但是使用Sql Server命令而不是那個呢?只需執行命令「從磁盤恢復[數據庫名稱] = [備份文件位置] –
您聲明設備類型(源),但絕不會將其掛接到您的Restore對象。調用此方法之前的DeviceType。 「 –