50٪ تخفیف روی تمام دوره‌ها!
پایان تخفیف تا:
مشاهده دوره‌ها
0

کد مربوط به گرفتن پشیبان از بانک و بازیابی آن

سلام ، کسی هست کمک کنه خطای این کد که مربوط به گرفتن backup , restorاز بانک sql 2008 توی c# باشه:

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.Data.SqlClient;
using Microsoft.SqlServer.Server;
using Microsoft.SqlServer.Management.Smo;

namespace mali_navid_sadghin
{
    class prog
    {
        static void Main(string[] args)
        {
            BackupDB(@"C:\Temp\Test.bak");
            RestoreDB(@"C:\Temp\Test.bak", "Test_Restore");
        }

        public static void BackupDB(string backupDestinationFilePath)
        {
            try
            {
                Console.WriteLine("Backup operation started");
                Backup backup = new Backup();
                //Set type of backup to be performed to database
                backup.Action = BackupActionType.Database;
                backup.BackupSetDescription = "BackupDataBase description";
                //Set the name used to identify a particular backup set.
                backup.BackupSetName = "Backup";
                //specify the name of the database to back up
                backup.Database = "TEST";
                //Set up the backup device to use filesystem.
                BackupDeviceItem deviceItem = new BackupDeviceItem(backupDestinationFilePath, DeviceType.File);
                backup.Devices.Add(deviceItem);

                // Setup a new connection to the data server
                ServerConnection connection = new
    ServerConnection(@"SERVER_NAME");
                // Log in using SQL authentication
                connection.LoginSecure = false;
                connection.Login = "testuser";
                connection.Password = "testuser";
                Server sqlServer = new Server(connection);
                //Initialize devices associated with a backup operation.
                backup.Initialize = true;
                backup.Checksum = true;
                //Set it to true to have the process continue even
                //after checksum error.
                backup.ContinueAfterError = true;
                //Set the backup expiry date.
                backup.ExpirationDate = DateTime.Now.AddDays(3);
                //truncate the database log as part of the backup operation.
                backup.LogTruncation = BackupTruncateLogType.Truncate;
                //start the back up operation
                backup.SqlBackup(sqlServer);
                Console.WriteLine("Backup operation succeeded");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Backup operation failed");
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }

        public static void RestoreDB(string backUpFilePath, string databaseName)
        {
            try
            {
                Console.WriteLine("Restore operation started");
                Restore restore = new Restore();
                //Set type of backup to be performed to database
                restore.Database = databaseName;
                restore.Action = RestoreActionType.Database;
                //Set up the backup device to use filesystem.         
                restore.Devices.AddDevice(backUpFilePath, DeviceType.File);
                //set ReplaceDatabase = true to create new database
                //regardless of the existence of specified database
                restore.ReplaceDatabase = true;
                //If you have a differential or log restore to be followed,
                //you would specify NoRecovery = true
                restore.NoRecovery = false;
                //if you want to restore to a different location, specify
                //the logical and physical file names
                restore.RelocateFiles.Add(new RelocateFile("Test",
    @"C:\Temp\Test.mdf"));
                restore.RelocateFiles.Add(new RelocateFile("Test_Log",
    @"C:\Temp\Test_Log.ldf"));
                ServerConnection connection = new
    ServerConnection(@"SERVER_NAME");
                //my SQL user doesnt have sufficient permissions,
                //so i am using my windows account
                connection.LoginSecure = true;
                //connection.LoginSecure = false;
                //connection.Login = "testuser";
                //connection.Password = "testuser";
                Server sqlServer = new Server(connection);
                //SqlRestore method starts to restore database           
                restore.SqlRestore(sqlServer);
                Console.WriteLine("Restore operation succeeded");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Restore operation failed");
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }
}

پرسیده شده در 1395/03/17 توسط

3 پاسخ

1

سلام . خودت بنویسی که راحت تره ... من قبلا نوشته بودم ولی الان ندارم

داخل sql بصورت گرافیکی بک اپ رو بیار تنظیماتشو انجام بده بعد script بک اپ رو بگیر بیار تو سی شارپ کد کن

پاسخ در 1395/03/17 توسط
0

اصلاً کسی میتونه این کد رو برام توضیح بده که چطور عمل می کنه و پیشنیازاش چی هستن

پاسخ در 1395/03/17 توسط
0

ITPRO ی عزیز در قسمت Comment کد شما کاملا مشخص هست چه کارهایی در هر خط داره انجام میشه ، کجاش براتون ابهام داره ؟

پاسخ در 1395/03/29 توسط

پاسخ شما