السلام عليكم اخواني
عندي قاعدة بيانات ابحث عن طريقه لنسخها احتياطيا سكربت او ملف باتش بس المهم اتاكد ان موضوع النسخه الاحتياطيه ماشي وضعه اتركم مع الكود الذي وجدته مع مشكلة ظهرت معي ولكن لم اتمكن من حلها :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.SqlServer.Management.Smo;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string MachineName = @"SERVER\SQLEXPRESS";
string DBName = "manidata";
string backupName = "tempBackup201";
Microsoft.SqlServer.Management.Smo.Server server = new Microsoft.SqlServer.Management.Smo.Server(MachineName);
Database db = server.Databases[DBName];
//To Avoid TimeOut Exception
server.ConnectionContext.StatementTimeout = 60 * 60;
RecoveryModel recoverymodel = db.DatabaseOptions.RecoveryModel;
//Define a Backup object variable.
Backup backup = new Backup();
//backup.Co.CompressionOption = BackupCompressionOptions.On;
//Specify the type of backup, the description, the name, and the database to be backed up.
backup.Action = BackupActionType.Database;
backup.BackupSetDescription = "Full backup of " + DBName + ".";
backup.BackupSetName = backupName;
backup.Database = DBName;
//Declare a BackupDeviceItem
BackupDeviceItem bdi = default(BackupDeviceItem);
bdi = new BackupDeviceItem(@"C:\ " + backupName + ".bak", DeviceType.File);
//Add the device to the Backup object.
backup.Devices.Add(bdi);
//Set the Incremental property to False to specify that this is a full database backup.
backup.Incremental = false;
//Set the expiration date.
System.DateTime backupdate = new System.DateTime();
backupdate = new System.DateTime(2011, 10, 11);
backup.ExpirationDate = backupdate;
//Specify that the log must be truncated after the backup is complete.
backup.LogTruncation = BackupTruncateLogType.Truncate;
//Run SqlBackup to perform the full database backup on the instance of SQL Server.
backup.SqlBackup(server); // (Backup failed for Server 'SERVER\SQLEXPRESS'.) هنا يظهر الخطأ
//Remove the backup device from the Backup object.
backup.Devices.Remove(bdi);
}
}
}
