[英]Set database backup file on local machine
POSSIBLE DUPLICATE OF : 可能的重复:
How to create SQL Server 2008 database full backup programmatically in desired folder 如何在所需的文件夹中以编程方式创建SQL Server 2008数据库完全备份
I have a database on my computer (SQL Server 2008 Express). 我的计算机上有一个数据库(SQL Server 2008 Express)。
I need any sample code in C# that I can use to backup the database to a file using Visual Studio 2010. 我需要C#中的任何示例代码,我可以使用Visual Studio 2010将数据库备份到文件。
Thanks.. 谢谢..
i'll using this code to connect Database 我将使用此代码连接数据库
public SqlConnection SqlSaverConn()
{
string path = Application.StartupPath + "\\";
String conStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename="+ path +"SMS_DB.mdf;Integrated Security=True;User Instance=True";
SqlConnection con = new SqlConnection(conStr);
try
{
con.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
return con;
}
Just execute SQL Server command 只需执行SQL Server命令
BACKUP DATABASE database_name TO DISK='d:\path\to\backup\file\on\the\server.bak'
from your program 来自你的程序
EDIT 编辑
public SqlConnection SqlSaverConn()
{
string path = Application.StartupPath + "\\";
String conStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename="+ path +"SMS_DB.mdf;
Integrated Security=True; User Instance=True";
SqlConnection con = new SqlConnection(conStr);
try
{
con.Open();
SqlCommand command;
command = new SqlCommand(@"backup database SMS_DB.mdf to disk ='" + path + "\\" + name, con);
command.ExecuteNonQuery();
MessageBox.Show("Backup Created.");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
return con;
}
Try this peice of code. 试试这个代码。
Im using flowing code to connect database 我使用流动代码连接数据库
public SqlConnection SqlSaverConn()
{
string path = Application.StartupPath + "\\";
String conStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename="+ path +"SMS_DB.mdf;Integrated Security=True;User Instance=True";
SqlConnection con = new SqlConnection(conStr);
try
{
con.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
return con;
}
can i use your SQL Command to set backup using upon type connection 我可以使用您的SQL命令在类型连接上设置备份
This what I did and it Worked! 这就是我所做的和它的工作!
private void BackupButtonClick(object sender, RoutedEventArgs e)
{
// FILE NAME WITH DATE DISTICNTION
string fileName = string.Format("SchoolBackup_{0}.bak", DateTime.Now.ToString("yyyy_MM_dd_h_mm_tt"));
try
{
// YOUR SEREVER OR MACHINE NAME
Server dbServer = new Server (new ServerConnection("DESKTOP"));
Microsoft.SqlServer.Management.Smo.Backup dbBackup = new Microsoft.SqlServer.Management.Smo.Backup()
{
Action = BackupActionType.Database,
Database = "School"
};
dbBackup.Devices.AddDevice(@backupDirectory() +"\\"+ fileName, DeviceType.File);
dbBackup.Initialize = true;
dbBackup.SqlBackupAsync(dbServer);
MessageBox.Show("Backup", "Backup Completed!");
}
catch(Exception err)
{
System.Windows.MessageBox.Show(err.ToString());
}
}
// THE DIRECTOTRY YOU WANT TO SAVE IN
public string backupDirectory()
{
using (var dialog = new FolderBrowserDialog())
{
var result = dialog.ShowDialog();
return dialog.SelectedPath;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.