我可以備份硬盤上的數據庫以下方法:使用存儲過程從硬盤備份SQL Server數據庫?
void BackUp(string ConnectionString, string DatabaseFullPath, string backUpPath)
{
progressBar1.Value = 0;
using (SqlConnection con = new SqlConnection(ConnectionString))
{
con.Open();
string UseMaster = "USE master";
SqlCommand UseMasterCommand = new SqlCommand(UseMaster, con);
UseMasterCommand.ExecuteNonQuery();
progressBar1.Value += 25;
string Alter1 = @"ALTER DATABASE [" + DatabaseFullPath + "] SET Single_User WITH Rollback Immediate";
SqlCommand Alter1Cmd = new SqlCommand(Alter1, con);
Alter1Cmd.ExecuteNonQuery();
progressBar1.Value += 25;
string Restore = @"BACKUP DATABASE [" + DatabaseFullPath + "] TO DISK = N'" + backUpPath + @"' WITH NOFORMAT, NOINIT, NAME = N'" + DatabaseFullPath + "-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10";
SqlCommand RestoreCmd = new SqlCommand(Restore, con);
RestoreCmd.ExecuteNonQuery();
progressBar1.Value += 25;
string Alter2 = @"ALTER DATABASE [" + DatabaseFullPath + "] SET Multi_User";
SqlCommand Alter2Cmd = new SqlCommand(Alter2, con);
Alter2Cmd.ExecuteNonQuery();
progressBar1.Value += 25;
labelReport.Text = "Successful";
}
}
和,我怎樣才能將其轉換爲一個存儲過程?
(我是新手在SQL Server中)
其實我以前看過,但我自己無法創建上面的'SP'。 – 2010-11-19 07:24:49
謝謝,但我的數據庫在我的Win App exe文件旁邊的硬盤上! – 2010-11-19 07:50:25
編輯,但我不相信你的DatabaseFullPath參數可以是mdf文件的路徑,因爲它是無效的語法。這將是數據庫名稱。 另外我不相信你的數據庫需要在單用戶模式下執行備份 – 2010-11-19 08:05:15