我正在用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");
}
答案 0 :(得分:1)
有几点意见:
sqlconn2
,serverconn2
和s1
是不必要的,因为您可以重复使用变量s
来重新附加ghale
数据库mydb2
的中间部分,则重新附加ghale
数据库ghale
备份文件还原到数据库mydb2
,如果是,请查看数据库属性| mydb2
的文件您将看到它正在使用文件ghale.mdf
和ghale_log.ldf
。因此,您无法重新附加ghale.mdf
,因为该文件现在正由数据库mydb2
使用,因此会出现“拒绝访问”错误消息。<强>更新强>: 我认为以下代码是您正在努力实现的目标:
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;到服务帐户。