如何在SqlExpress中从数据库备份数据
问题描述:
我不知道如何从SqlExpress开发的数据库中备份数据.
请采取步骤执行此操作.
谢谢.
Hi,
i have no idea on how to backup data from database which is developed in SqlExpress.
please give steps to do this.
thanking you.
答
如果要自动备份:
步骤:
1.创建一个具有以下内容的SQL文件:
设置@BkpPath =''[备份文件路径] .bak''
备份数据库[DBName]到磁盘= @BkpPath WITH FORMAT;
2.创建计划任务(从控制面板"中)并输入以下文本
在RUN中:"C:\ Program Files \ Microsoft SQL Server \ 100 \ Tools \ Binn \ SQLCMD.EXE" -i"[sql文件路径] .sql"
在开始位置:"C:\ Program Files \ Microsoft SQL Server \ 100 \ Tools \ Binn"
If you want Auto Backup:
Steps:
1. Create a SQL file with following content:
Set @BkpPath=''[backup file path].bak''
BACKUP DATABASE [DBName] TO DISK = @BkpPath WITH FORMAT;
2. Create Schedule Task (from Control Panel) and put following text
in RUN : "C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE" -i"[sql file path].sql"
in Start in : "C:\Program Files\Microsoft SQL Server\100\Tools\Binn"
右键单击数据库,转到任务,单击备份,添加目标路径,单击确定...
right click on the Database ,go to task , click on backup, add destination path,click ok......
我刚刚在我的项目中使用了以下代码:
I have just used this code in my Project:
private void DatabaseBackup()
{
string strDate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString().PadLeft(2, '0') + DateTime.Now.Day.ToString().PadLeft(2, '0');
string[] strTime = DateTime.Now.TimeOfDay.ToString().Split(':');
string strDateTime = strDate + " " + strTime[0].PadLeft(2, '0') + strTime[1].PadLeft(2, '0');
try
{
string backupPath = txtBackupLocation.Text.Trim() + "\\" + strDateTime + ".bak";
int retVal = 0;
retVal = _settingManager.DatabaseBackup(backupPath);
if (retVal != 0)
{
btnBackupDatabase.Enabled = false;
lblBackupStatus.Text = "Database backup successful.";
MessageBox.Show("The backup of database completed successfully." + Environment.NewLine + backupPath, Program.MESSAGEBOXTITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
btnClose.Focus();
}
}
catch (Exception ex)
{
MessageBox.Show("Error [Database Backup]: " + Environment.NewLine + ex.Message, Program.MESSAGEBOXTITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
lblBackupStatus.Text = "Database backup failed.";
}
}
有关更多信息,请参见:使用SQL Server 2005数据库备份和还原C#和.NET 2.0 [ ^ ]
http://midnightprogrammer.net/post/BackupRestore-SQL-database-using-C.aspx [^ ]
For more have look on: SQL Server 2005 Database Backup and Restore using C# and .NET 2.0[^]
http://midnightprogrammer.net/post/BackupRestore-SQL-database-using-C.aspx[^]