在C#中附加SQL Server数据库

时间:2016-09-01 07:02:08

标签: c# sql-server attachment smo detach

我正在用C#开发一个项目。我想分离,然后使用SMO从C#代码附加SQL Server数据库。我可以分离数据库,但是当我想要附加它时,会发生错误:

  

无法打开物理文件“C:\ Program Files \ Microsoft SQL Server \ MSSQL12.MSSQLSERVER \ MSSQL \ DATA \ ghale.mdf”。操作系统错误5:“5(访问被拒绝。)”。

代码:

OpenFileDialog f = new OpenFileDialog();
f.Title = "sqlbakcup";
f.FileName = "";
f.Filter = @"SQL Backup files (*.BAK) |*.BAK|All files(*.*) |*.*";

if (f.ShowDialog() == DialogResult.OK)
{
    SqlConnection sqlconn = new SqlConnection(@"Data Source=.;Initial Catalog=master;Integrated Security=True");
    ServerConnection serverconn = new ServerConnection(sqlconn);

    Server s = new Server(serverconn);
    s.DetachDatabase("ghale", false);

    //MessageBox.Show("ok");
    SqlConnection sqlconn1 = new SqlConnection(@"Data Source=.;Initial Catalog=mydb2;Integrated Security=True");

    SqlCommand sqlcmd = new SqlCommand("ALTER DATABASE mydb2  SET SINGLE_USER with ROLLBACK IMMEDIATE " + "use master  " + "RESTORE DATABASE [mydb2] FROM  DISK = @n WITH REPLACE ", sqlconn1);
    sqlcmd.Parameters.AddWithValue("@n", f.FileName);

    sqlconn1.Open();
    sqlcmd.ExecuteNonQuery();
    sqlconn1.Close();

    SqlConnection sqlconn2 = new SqlConnection(@"Data Source=.;Initial Catalog=master;Integrated Security=True");
    ServerConnection serverconn2 = new ServerConnection(sqlconn2);

    Server s1 = new Server(serverconn2);
    s1.AttachDatabase("ghale", new StringCollection {
            @"C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\ghale.mdf",
            @"C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\ghale_log.ldf" }, AttachOptions.None);
    //MessageBox.Show("ok");
    MessageBox.Show("OK");
}

2 个答案:

答案 0 :(得分:1)

有几点意见:

  1. 变量sqlconn2serverconn2s1是不必要的,因为您可以重复使用变量s来重新附加ghale数据库
  2. 如果我注释掉将备份文件还原到mydb2的中间部分,则重新附加ghale数据库
  3. 我怀疑您正在将ghale备份文件还原到数据库mydb2,如果是,请查看数据库属性| mydb2的文件您将看到它正在使用文件ghale.mdfghale_log.ldf。因此,您无法重新附加ghale.mdf,因为该文件现在正由数据库mydb2使用,因此会出现“拒绝访问”错误消息。
  4. <强>更新: 我认为以下代码是您正在努力实现的目标:

    OpenFileDialog f = new OpenFileDialog();
    f.Title = "sqlbakcup";
    f.FileName = "";
    f.Filter = @"SQL Backup files (*.BAK) |*.BAK|All files(*.*) |*.*";
    
    if (f.ShowDialog() == DialogResult.OK)
    {
        using (SqlConnection sqlconn = new SqlConnection(@"Data Source=.;Initial Catalog=master;Integrated Security=True"))
        {
            ServerConnection serverconn = new ServerConnection(sqlconn);
    
            Server s = new Server(serverconn);
            s.DetachDatabase("ghale", false);
    
            SqlConnection sqlconn1 = new SqlConnection(@"Data Source=.;Initial Catalog=mydb2;Integrated Security=True");
    
            SqlCommand sqlcmd = new SqlCommand(
                "ALTER DATABASE [mydb2] SET SINGLE_USER with ROLLBACK IMMEDIATE use master " 
                + @"RESTORE DATABASE [mydb2] FROM DISK = @n WITH REPLACE, "
                + @"MOVE 'ghale'     TO 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Data\mydb2.mdf', "
                + @"MOVE 'ghale_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Data\mydb2_log.ldf' "
                + @"ALTER DATABASE [mydb2] MODIFY FILE (NAME=N'ghale', NEWNAME=N'mydb2')"
                + @"ALTER DATABASE [mydb2] MODIFY FILE (NAME=N'ghale_log', NEWNAME=N'mydb2_log')",
            sqlconn1);
            sqlcmd.Parameters.AddWithValue("@n", f.FileName);
    
            sqlconn1.Open();
            sqlcmd.ExecuteNonQuery();
            sqlconn1.Close();
    
            s.AttachDatabase("ghale",
                            new StringCollection {
            @"C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\ghale.mdf",
            @"C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\ghale_log.ldf" },
                            AttachOptions.None);
        }
    }
    

    我已更新mydb2数据库还原sql,以便为数据和日志使用不同的物理文件,以及重命名逻辑名称。

答案 1 :(得分:0)

试试这个..

首先你应该找到sqlserver的服务帐号,你可以在任务管理器中看到它同时按ctrl + alt + delete;然后,你必须给予&#34;的读/写权限。 C:\ Murach \ SQL Server 2008 \ Databases&#34;到服务帐户。