Sqlserver2014通过Windows应用程序C#进行备份 [英] Sqlserver2014 taking backup by windows application C#
本文介绍了Sqlserver2014通过Windows应用程序C#进行备份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图通过编写程序来备份我的Windows应用程序数据库。我的问题是我可以保存.bak文件。如果我们创建一个备份文件到文件夹位置,那么我们不能选择相同的文件夹path.Its真的很明显,如果可以创建一个具有唯一ID的文件夹,在每种情况下创建备份过程。
我的LOC是
private void button3_Click( object sender,EventArgs e)
{
try
{
System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder(ConnectionStringTwo);
string serverName = builder.DataSource;
string DbName = builder.InitialCatalog;
string dbuserName = builder.UserID;
string dbPassword = builder.Password;
if (DestPath == || DbName == )
{
MessageBox.Show( 尝试选择数据库和目标文件夹!);
}
其他
{
string databaseName = DbName; // dataGridView1.Rows [e.RowIndex] .Cells [e.ColumnIndex] .FormattedValue。 ToString();
// 定义备份对象变量。
备份sqlBackup = 新备份();
/// /指定备份类型,描述,名称和要备份的数据库。
sqlBackup.Action = BackupActionType.Database;
sqlBackup.BackupSetDescription = 备份: + databaseName + on + DateTime.Now.ToShortDateString();
sqlBackup.BackupSetName = FullBackUp;
sqlBackup.Database = databaseName;
/// /声明BackupDeviceItem
string destinationPath = DestPath;
string backupfileName = DbName + .bak 跨度>;
// string backupfileName = DbName + DateTime.Now.Day + DateTime.Now.TimeOfDay +.bak ;
BackupDeviceItem deviceItem = new BackupDeviceItem(destinationPath + \\ + backupfileName,DeviceType.File);
/// /定义服务器连接
// ServerConnection connection = new ServerConnection(frm.serverName,frm.userName,frm.password);
ServerConnection connection = new ServerConnection(serverName,dbuserName,dbPassword);
/// /避免TimeOut异常
服务器sqlServer = 新服务器(连接);
sqlServer.ConnectionContext.StatementTimeout = 60 * 60 ;
数据库db = sqlServer.Databases [databaseName];
sqlBackup.Initialize = true ;
sqlBackup.Checksum = true ;
sqlBackup.ContinueAfterError = true ;
/// /将设备添加到备份对象。
sqlBackup.Devices.Add(deviceItem);
/// /将Incremental属性设置为False以指定这是完整数据库备份。
sqlBackup.Incremental = false ;
sqlBackup.ExpirationDate = DateTime.Now.AddDays( 3 );
/// /指定在备份完成后必须截断日志。
sqlBackup.LogTruncation = BackupTruncateLogType.Truncate;
sqlBackup.FormatMedia = false ;
/// /运行SqlBackup以在SQL Server实例上执行完整数据库备份。
sqlBackup.SqlBackup(sqlServer);
/// /从备份对象中删除备份设备。
sqlBackup.Devices.Remove(deviceItem);
toolStripStatusLabel1.Text = 创建成功的备份!;
}
}
catch (例外情况)
{
toolStripStatusLabel1.Text = ex.Message;
// MessageBox.Show(ex.Message);
}
}
我的尝试:
创建备份到同一位置时
sqlBackup.SqlBackup(sqlServer);给我一个例外备份失败服务器'Outlet-PC'
解决方案
我知道你已经编写了代码,但是......这会有帮助吗?
用C#备份SQL数据库 [ ^ ]
I am trying to take back up of my windows application database by writing a program.My problem is i can save the .bak file.If we create a backup file to a folder location then we can't choose the same folder path.Its really appreciable if can create a folder with a unique id in each case of creating back up process.
My LOC is
private void button3_Click(object sender, EventArgs e)
{
try
{
System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder(ConnectionStringTwo);
string serverName = builder.DataSource;
string DbName = builder.InitialCatalog;
string dbuserName = builder.UserID;
string dbPassword = builder.Password;
if (DestPath == "" || DbName == "")
{
MessageBox.Show("Try to select Database and Destination Folder !");
}
else
{
string databaseName = DbName;//dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].FormattedValue.ToString();
//Define a Backup object variable.
Backup sqlBackup = new Backup();
////Specify the type of backup, the description, the name, and the database to be backed up.
sqlBackup.Action = BackupActionType.Database;
sqlBackup.BackupSetDescription = "BackUp of:" + databaseName + "on" + DateTime.Now.ToShortDateString();
sqlBackup.BackupSetName = "FullBackUp";
sqlBackup.Database = databaseName;
////Declare a BackupDeviceItem
string destinationPath = DestPath;
string backupfileName = DbName+".bak";
//string backupfileName = DbName+DateTime.Now.Day + DateTime.Now.TimeOfDay + ".bak";
BackupDeviceItem deviceItem = new BackupDeviceItem(destinationPath + "\\" + backupfileName, DeviceType.File);
////Define Server connection
//ServerConnection connection = new ServerConnection(frm.serverName, frm.userName, frm.password);
ServerConnection connection = new ServerConnection(serverName, dbuserName, dbPassword);
////To Avoid TimeOut Exception
Server sqlServer = new Server(connection);
sqlServer.ConnectionContext.StatementTimeout = 60 * 60;
Database db = sqlServer.Databases[databaseName];
sqlBackup.Initialize = true;
sqlBackup.Checksum = true;
sqlBackup.ContinueAfterError = true;
////Add the device to the Backup object.
sqlBackup.Devices.Add(deviceItem);
////Set the Incremental property to False to specify that this is a full database backup.
sqlBackup.Incremental = false;
sqlBackup.ExpirationDate = DateTime.Now.AddDays(3);
////Specify that the log must be truncated after the backup is complete.
sqlBackup.LogTruncation = BackupTruncateLogType.Truncate;
sqlBackup.FormatMedia = false;
////Run SqlBackup to perform the full database backup on the instance of SQL Server.
sqlBackup.SqlBackup(sqlServer);
////Remove the backup device from the Backup object.
sqlBackup.Devices.Remove(deviceItem);
toolStripStatusLabel1.Text = "Successful backup is created!";
}
}
catch (Exception ex)
{
toolStripStatusLabel1.Text = ex.Message;
// MessageBox.Show(ex.Message);
}
}
What I have tried:
while creating the back up to the same location
sqlBackup.SqlBackup(sqlServer);gives me an exception of "Back Up failed for server 'Outlet-PC'"
解决方案
I know you have written code, but ... would this help?
Backing up an SQL Database in C#[^]
这篇关于Sqlserver2014通过Windows应用程序C#进行备份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文