YetAnotherForum
Hoşgeldiniz Arama | Aktif Konular | Giriş | Kayıt Ol

Yeni Konu
Kod ile DB backup ve restore işlemi Ayarlar
@butun
#1 Gönderildi : 20 Ağustos 2009 Perşembe 13:39:32
Sıralama: Yönetici



Gruplar: Yöneticiler

Katılındı: 19.05.2009
Mesajlar: 98
İkamet: Istanbul
Merhaba arkdaşlar,

C# kullanarak DB backup ve restore işlemleri için SQL Server 2008'in bize sağladığı,

Microsoft.SqlServer.Management.Smo
Microsoft.SqlServer.Management.Common

referanslarını kullanabilirsiniz. Nasıl mı? Şöyle ki,


Backup işlemi:
Kod:
       
private void BackUp()
        {
            try
            {
                string filename = "MyDBBackup.bak";

                Server server = new Server(new ServerConnection(new System.Data.SqlClient.SqlConnection("MyDBConnStr")));

                // Create a new backup operation
                Backup bkpDatabase = new Backup();

                // Set the backup type to a database backup
                bkpDatabase.Action = BackupActionType.Database;

                // Set the database that we want to perform a backup on
                bkpDatabase.Database = "MyDBName"

                // Set the backup device to a file
                BackupDeviceItem bkpDevice = new BackupDeviceItem(filename, DeviceType.File);

                // Add the backup device to the backup
                bkpDatabase.Devices.Add(bkpDevice);

                bkpDatabase.PercentComplete += new PercentCompleteEventHandler("BackupPercentaceCallBack");

                // Perform the backup
                bkpDatabase.SqlBackup(server);
            }
            catch (Exception ex)
            {
                //NOP
            }
        }



Restore işlemi için:
Kod:

private void Restore()
        {
            //Restore
            try
            {
                Server server = new Server(new ServerConnection(new System.Data.SqlClient.SqlConnection("MyDBConnStr")));

                    string filepath = "MyDBBackup.bak";

                    if (filepath != "")
                    {
                        if (File.Exists(filepath))
                        {
                            // Create a new database restore operation
                            Restore rstDatabase = new Restore();

                            // Set the restore type to a database restore
                            rstDatabase.Action = RestoreActionType.Database;

                            // Set the database that we want to perform the restore on
                            rstDatabase.Database = "MyDBName";

                            // Set the backup device from which we want to restore, to a file
                            BackupDeviceItem bkpDevice = new BackupDeviceItem(filepath, DeviceType.File);

                            // Add the backup device to the restore type
                            rstDatabase.Devices.Add(bkpDevice);

                            // If the database already exists, replace it
                            rstDatabase.ReplaceDatabase = true;

                            rstDatabase.PercentComplete += new PercentCompleteEventHandler(RestorePercentaceCallBack);

                            server.KillAllProcesses("MyDBName");

                            rstDatabase.Wait();

                            // Perform the restore
                            rstDatabase.SqlRestore(server);

                            Application.Restart();
                        }
                    }
            }
            catch (Exception ex)
            {
                //NOP
            }
        }
@butun
#2 Gönderildi : 20 Ağustos 2009 Perşembe 13:44:39
Sıralama: Yönetici



Gruplar: Yöneticiler

Katılındı: 19.05.2009
Mesajlar: 98
İkamet: Istanbul
Bir uyarı, kodlarda geçen (tümü kırmızı renkle belirtilmiş zaten),

MyDBBackup.bak -> yedek alınacak dosya adı
MyDBName -> Üzerind işlem yapılacak veri tabanı adı
BackupPercentaceCallBack -> Veri tabanı yedeklenirken kullanılacak event handler
MyDBConnStr -> Veri tabanı connection string değeri
RestorePercentaceCallBack -> Veri tabanı geri yüklenirken kullanılacak event handler


ve veri tabanı geri yüklenirken mutlaka DİKKAT edilmesi gereken bir nokta, geri yükleme işlemi yapılmadan önce, veri tabanındaki mevcut connection'ları kapatmak. Bunun için,


Kod:

server.KillAllProcesses("MyDBName");

rstDatabase.Wait();


kod satırlanını kullanmanız gerekiyor.

sorularınız için buralardayım, umarım faydalı olur bu bilgiler...
Bu konudaki kullanıcılar
Ziyaretçi
Yeni Konu
Foruma Atla  
Bu foruma yeni bir konu gönderebilirsiniz
Bu forumda ki konulara yeni posta gönderemezsiniz.
Bu forumdaki postalarınızı silemezsiniz.
Bu forumdaki postalarınızı düzenleyemezsiniz.
Bu forumda anketler yaratamazsınız.
Bu forumdaki anketlere oy veremezsiniz.

YAFPro Theme Created by Jaben Cargman (Tiny Gecko)
Powered by YAF | YAF © 2003-2009, Yet Another Forum.NET