分离数据库后附加数据库 [英] Attaching DB after it has been detached

查看:76
本文介绍了分离数据库后附加数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用以下代码将数据库附加到SQL Server.问题是如果我创建一个新文件并通过我的代码附加它,但是当我使用SSMS分离该文件并再次运行相同的代码时,它会出错.
错误为:

Hi,

I am using following code to attach my DB to SQL Server. The problem is if I create a new file and attach it through my code but when I detach the file using SSMS and again run the same code, it gives error.
Error is:

"Unable to open the physical file "e:\dbForATTWithPWD.mdf". Operating system error 2: "2(error not found)""


代码是:


The code is:

Dim conn As New SqlConnection("Server=MyHomeServer\SQLExpress;Database=master;Integrated Security=SSPI")
Dim cmd As New SqlCommand("", conn)
cmd.CommandText = "CREATE DATABASE MyHomeWithPWD ON ( FILENAME = 'e:\dbForATTWithPWD.mdf' ), ( FILENAME = 'e:\dbForATTWithPWD_log.ldf' ) FOR ATTACH"
conn.Open()
cmd.ExecuteNonQuery()
cmd.Dispose()
conn.Dispose()



为什么会这样呢.即使是第一次,所有权限也相同.

另外,如果我需要将DB与uid sa和pwd abc123一起使用,连接字符串应该是什么?

谢谢



Why is it happening. All the permissions are the same even first time.

Also what should be the connection string if I need to use DB with uid sa and pwd abc123 ?

Thanks

推荐答案

www.connectionstrings.com [
www.connectionstrings.com[^] is the place to go to build connection strings. Are you SURE the file is still in the location you''re stating ? The error message says it is not.


这篇关于分离数据库后附加数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆