public static readonly string ConnectionString = @"Data Source=JOSEPH-PC\SQLEXPRESS;Initial Catalog=Storage;Integrated Security=True";
private void button2_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrEmpty(txtrestore.Text) | string.IsNullOrEmpty(ComboBoxserverName.Text) | string.IsNullOrEmpty(ComboBoxDatabaseName.Text))
{
// label3.Visible = true;
MessageBox.Show("Server Name & Database & Restore file can not be Blank");
return;
}
else
{
, ComboBoxDatabaseName.Text, ComboBoxserverName.Text);
RestoreDatabase(ComboBoxDatabaseName.Text, "C:\RESTORE\hw.bak", ComboBoxserverName.Text);
label4.Visible = true;
label4.ForeColor = System.Drawing.Color.Green;
label4.Text = "Database restore has been created successfully";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Backup_Click(object sender, EventArgs e)
{
try {
if (string.IsNullOrEmpty(ComboBoxserverName.Text) | string.IsNullOrEmpty(ComboBoxDatabaseName.Text))
{
// label3.Visible = true;
MessageBox.Show("Server Name & Database can not be Blank");
return;
}
else {
SaveFileDialog1.FileName = ComboBoxDatabaseName.Text;
SaveFileDialog1.ShowDialog();
string s = null;
s = SaveFileDialog1.FileName;
BackupDatabase(s + ".bak", ComboBoxDatabaseName.Text, ComboBoxserverName.Text);
label3.Visible = true;
label3.ForeColor = System.Drawing.Color.Green;
label3.Text = "Database backup has been created successfully";
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
backup</param>
public void BackupDatabase(string backUpFile,string db,string srv)
{
try {
ServerConnection con = new ServerConnection(@""+srv);
Server server = new Server(con);
Backup source = new Backup();
source.Action = BackupActionType.Database;
source.Database = db;
BackupDeviceItem destination = new BackupDeviceItem(backUpFile, DeviceType.File);
source.Devices.Add(destination);
source.SqlBackup(server);
con.Disconnect();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public void RestoreDatabase(String databaseName, String backUpFile, String srv)
{
Microsoft.SqlServer.Management.Smo.Server smoServer = new Server(new ServerConnection(srv));
Database db = smoServer.Databases["Storage"];
string dbPath = System.IO.Path.Combine(db.PrimaryFilePath, "Storage.MDF");
string logPath = System.IO.Path.Combine(db.PrimaryFilePath, "Storage_1.LDF");
Restore restore = new Restore();
BackupDeviceItem deviceItem = new BackupDeviceItem("C:/RESTORE/hw.BAK", DeviceType.File);
restore.Devices.Add(deviceItem);
restore.Database = "C:/RESTORE/hw.BAK";
string fileMdf = System.IO.Path.Combine(smoServer.MasterDBPath, "Storage.mdf");
string fileLdf = System.IO.Path.Combine(smoServer.MasterDBLogPath, "Storage_1.ldf");
RelocateFile relocateMdf = new RelocateFile("Storage", fileMdf);
RelocateFile relocateLdf = new RelocateFile("Storage_1", fileLdf);
restore.RelocateFiles.Add(relocateMdf);
restore.RelocateFiles.Add(relocateLdf);
restore.FileNumber = 1;
restore.Action = RestoreActionType.Database;
restore.ReplaceDatabase = true;
restore.SqlRestore(smoServer);
db = smoServer.Databases["Storage"];
db.SetOnline();
smoServer.Refresh();
db.Refresh();
}
好的,这是我的代码。顺便说一下,我知道我不会在恢复方法中使用params,但这是我尝试实现此方法的众多方法之一。
我从两种方法得到同样的错误
服务器JOSEPH-PC / SQLEXPRESS的备份失败
或
服务器JOSEPH-PC / SQLEXPRESS的恢复失败。
备份昨天正在运行,恢复没有。
现在这些方法都没有工作,我没有在BACKUP的代码中改变它。
我也试过这两个命令
private string singleUserCmd = "alter database Storage set SINGLE_USER WITH ROLLBACK IMMEDIATE";
private string multiUserCmd = "alter database Storage set MULTI_USER";
再次出现同样的错误......
我花了10多个小时搜索帖子,我已经尝试了很多方法来实现这些方法,我希望得到答案。
谢谢。