2

可能重复:
SQL Server - 数据库“数据库”不存在。确保名称输入正确

我写了这段代码,但是数据库不识别备份?请指导

  SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Resources\dbcn.mdf;Integrated Security=True;User Instance=True";

            SqlCommand cmd = new SqlCommand();
            string MasterPath = "c://";
            try
            {
                con.Open();
                //BACKUP DATABASE نام بانک اطلاعاتی TO DISK = 'مسیر پشتیبان گیری بانک اطلاعاتی'
                string query = "BACKUP DATABASE dbcn TO DISK = '" + MasterPath + @"\BackupLibrary" + "\\dbcn.mdf" + "'";
                cmd.CommandText = query;
                cmd.Connection = con;
                cmd.ExecuteNonQuery();
                con.Close();

                MessageBox.Show("error");
            }
            catch(System.Exception EX)
            {
                System.Windows.Forms.MessageBox.Show(EX.Message);

            }

错误;

Database 'dbcn' does not exist. Make sure that the name is entered correctly.

BACKUP DATABASE 异常终止。

4

1 回答 1

0

它可能超出了您对这个项目的需要,但将来您可能想要研究 SQL 管理对象 (SMO)。它使您能够在托管代码(例如 c#)中执行 SQL Server 维护任务。

我相信它是 SQL Server SDK 的一部分。您需要将以下引用添加到您的项目中:

Microsoft.SqlServer.Smo
Microsoft.SqlServer.SmoExtended
Microsoft.SqlServer.ConnectionInfo
Microsoft.SqlServer.Management.Sdk.Sfc
Microsoft.SqlServer.SqlEnum

我能够在以下位置找到这些程序集:C:\Program Files\Microsoft SQL Server\110\SDK\Assemblies\

string ServerName = "server";
string DatabaseName = database;
string UserId = userId;
string Password = password;
ServerConnection serverConnection;

private Server _server;
public Server Server
{
    get
    {
        if (_server == null)
        {
            Connect();
        }
        return _server;
    }
}

string connectionString 
{
    get
    {
        return string.Format("Data Source={0};Initial Catalog={1};UID={2};Password={3}", ServerName, DatabaseName, UserId, Password);
    }
}

public void Connect()
{
    _serverConnection = new ServerConnection();
    _serverConnection.LoginSecure = false;
    _serverConnection.Login = UserId;
    _serverConnection.Password = Password;

    _server = new Server(_serverConnection);
}

public void BackupDatabase()
{
    using (SqlConnection cn = new SqlConnection(connectionString))
    {
        ServerConnection svCon = new ServerConnection(cn);
        Server svr = new Server(svCon);
        cn.Open();
        cn.ChangeDatabase("master");

        string testFolder = @"C:\temp";
        string databaseName = _databaseName;
        Backup backup = new Backup();
        backup.Action = BackupActionType.Database;
        backup.Database = databaseName;
        backup.Incremental = false;
        backup.Initialize = true;
        backup.LogTruncation = BackupTruncateLogType.Truncate;

        string fileName = string.Format("{0}\\{1}.bak", testFolder, databaseName);
        BackupDeviceItem backupItemDevice = new BackupDeviceItem(fileName, DeviceType.File);
        backup.Devices.Add(backupItemDevice);
        backup.PercentCompleteNotification = 10;
        //backup.PercentComplete += backup_PercentComplete;
        //backup.Complete += backup_Complete;

        backup.SqlBackup(svr);
        if (!VerifyBackup(svr))
        {
            //throw new Exception("Backup could not be verified.");
        }
        svr = null;
        cn.Close();
    }
}

public bool VerifyBackup(Server svr)
{
    string testFolder = @"C:\temp";
    string databaseName = _databaseName;
    Restore restore = new Restore();
    restore.Action = RestoreActionType.Database;
    string fileName = string.Format("{0}\\{1}.bak", testFolder, databaseName);
    BackupDeviceItem backupItemDevice = new BackupDeviceItem(fileName, DeviceType.File);
    restore.Devices.AddDevice(fileName, DeviceType.File);
    restore.Database = databaseName;
    restore.PercentCompleteNotification = 10;
    //restore.PercentComplete += new PercentCompleteEventHandler(ProgressEventHandler);

    bool verified = restore.SqlVerify(svr);
    return verified;
}

public void RestoreDatabase()
{
    using (SqlConnection cn = new SqlConnection(connectionString))
    {
        ServerConnection svCon = new ServerConnection(cn);
        Server svr = new Server(svCon);
        cn.Open();
        cn.ChangeDatabase("master");

        string testFolder = @"C:\temp";
        string databaseName = _databaseName;
        Restore restore = new Restore();
        restore.Action = RestoreActionType.Database;
        string fileName = string.Format("{0}\\{1}.bak", testFolder, databaseName);
        BackupDeviceItem backupItemDevice = new BackupDeviceItem(fileName, DeviceType.File);
        restore.Devices.AddDevice(fileName, DeviceType.File);
        restore.Database = databaseName;
        restore.ReplaceDatabase = true;
        restore.PercentCompleteNotification = 10;
        //restore.PercentComplete += new PercentCompleteEventHandler(ProgressEventHandler);
        svr.KillAllProcesses(databaseName);

        restore.SqlRestore(svr);
        svr = null;
        cn.Close();
    }
}
于 2012-10-21T06:15:28.807 回答