C#数据库备份与还原问题
发布网友
发布时间:2天前
我来回答
共1个回答
热心网友
时间:2天前
在C#的某事件中执行sql备份语句
完整备份
Backup Database NorthwindCS
To disk=’G:\Backup\NorthwindCS_Full_20070908.bak’
--差异备份
Backup Database NorthwindCS
To disk=’G:\Backup\NorthwindCS_Diff_20070908.bak’
With Differential
--日志备份,默认截断日志
Backup Log NorthwindCS
To disk=’G:\Backup\NorthwindCS_Log_20070908.bak’
--日志备份,不截断日志
Backup Log NorthwindCS
To disk=’G:\Backup\NorthwindCS_Log_20070908.bak’
With No_Truncate
--截断日志不保留
Backup Log NorthwindCS
With No_Log
--或者
Backup Log NorthwindCS
With Truncate_Only
--截断之后日志文件不会变小
--有必要可以进行收缩
--文件备份
Exec Sp_Helpdb NorthwindCS --查看数据文件
Backup Database NorthwindCS
File=’NorthwindCS’ --数据文件的逻辑名
To disk=’G:\Backup\NorthwindCS_File_20070908.bak’
--备份设置密码保护备份
Backup Database NorthwindCS
To disk=’G:\Backup\NorthwindCS_Full_20070908.bak’
With Password = ’123’,init
Restore Database NorthwindCS
From disk=’G:\Backup\NorthwindCS_Full_20070908.bak’
With Password = ’123’
private void button1_Click(object sender, EventArgs e)
{
try
{
string backupFolder = System.IO.Path.Combine(Application.StartupPath, "data");
if (!System.IO.Directory.Exists(backupFolder))//判断文件夹是否存在
{
//创建文件夹
System.IO.Directory.CreateDirectory(backupFolder);
}
string backupFileName = System.IO.Path.Combine(backupFolder, DateTime.Today.ToString("yyyyMMdd.bak"));
string sqltxt = string.Format("BACKUP DATABASE [FOOT] TO DISK=''", backupFileName);
SqlConnection conn = baseclass.DBConn.cycon();
conn.Open();
SqlCommand cmd = new SqlCommand(sqltxt, conn);
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("备份成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
this.Close();
}
希望对你有帮助