针对附加数据库运行 sql 脚本?
我有一个附加到 Sql Server 2008 Express 实例的 MDF,我需要针对它运行一些 sql 脚本来生成表、索引等。
但我不知道如何让它工作。如果我在 Visual Studio 中加载脚本,它只允许我连接到服务器并针对数据库运行它。我无法选择其他提供程序(Microsoft Sql Server 数据库文件),因此无法选择我的 MDF。
这让我只能选择将脚本作为单独的查询运行,但这不起作用,因为它似乎不支持 TSQL CREATE 语句。
如何针对附加数据库运行我的 sql 脚本?
I've got an MDF attached to an instance of Sql Server 2008 Express, and I need to run some sql scripts against it to generate tables, indexes, etc.
But I can't figure out how to get this to work. If I load the scripts in Visual Studio, it only allows me to connect to the server and run it against a database. I can't choose a different provider (Microsoft Sql Server Database File), so I can't select my MDF.
This leaves me the only option of running the script as individual queries, but that won't work as it appears it doesn't support TSQL CREATE statements.
How can I run my sql script against an attached database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我遇到了同样的问题,这对我有用。
右键单击脚本并选择连接>连接
应该已经为本地 SQLEXPRESS 实例设置了服务器名称和身份验证。
单击选项按钮
单击其他连接参数选项卡
使用以下内容作为指导粘贴到数据库文件的路径:
AttachDBFilename=C:\Path\To\Database\LocalData.mdf;database=LocalData;
单击连接按钮
如果连接仍然遇到问题,可能是因为已经有一个打开的连接。检查服务器资源管理器,如果连接已打开,请右键单击并选择关闭连接。
只要 SQL 脚本保持连接,此过程还将创建持久连接。您需要关闭脚本或选择右键单击>连接>断开。
更多信息可以在这个问题中找到:
EF4 生成数据库
I ran into this same problem and here is what worked for me.
Right-click on the script and choose Connection > Connect
The server name and authentication should already be set for the local SQLEXPRESS instance.
Click the Options button
Click the Additional Connection Parameters tab
Paste in the the path to your database file using the following as a guide:
AttachDBFilename=C:\Path\To\Database\LocalData.mdf;database=LocalData;
Click the Connect button
If you still have trouble connecting it may be because there is already an open connection. Check Server Explorer and if connection is open, Right-click and choose Close Connection.
This process will also create a persistent connection as long as the SQL Script remains connected. You will want to close the script or choose Right-click > Connection > Disconnect.
More information can be found at this question:
EF4 Generate Database
我假设您的意思是您在 Visual Studio 中有一个本地 MDF 文件(如 App_Data 文件夹)?
您可以通过连接命名管道来使用 SQL Management Studio。
首先,使用烦人的查询工具在 Visual Studio 中对附加数据库打开一个新查询。在属性窗口中的 ServerName 下,以“your-PC-name\mini-guid”格式保存该迷你 guid。使用 Management Studio 进行连接,如下所示:
例如,
您应该看到数据库下列出的文件名。
I'm assuming you mean you have an local MDF file in Visual Studio (like an App_Data folder)?
You can use SQL Management Studio by connecting with the named pipe.
First, open up a new query in Visual Studio on the attached DB using their anoying query tool. In the properties window, under ServerName, save that mini guid thats in the format of "your-PC-name\mini-guid". Connect using Management Studio like this:
e.g.,
You should see the filename listed under databases.
使用 SqlCmd 实用程序怎么样?
How about using the SqlCmd utility?
嗯,当我尝试将 ASPNETDB 的内容导入到不同的 MDF 中时,我遇到了同样的问题,这是我的做法:
在服务器资源管理器中右键单击 ASPNETDB.mdf 并发布到 .sql 文件。
使用 file - open 打开 .sql 文件。 (未连接)将出现在名称中。
右键单击附加的目标数据库并显示属性 (TargetDatabase.MDF)
从属性窗口中获取连接字符串。
复制“Data Source=.\SQLEXPRESS;”之后连接字符串中的所有内容
单击“执行 Sql”按钮 (Ctrl + Shift + E)
在“附加连接参数”选项卡中,粘贴您在步骤 4 中复制的数据
单击“连接”并选择相关从文件列表中按其磁盘位置选择目标 MDF 文件。
单击“连接”。
刷新目标数据库
Hmm i ran into the same problem when trying to import the contents of ASPNETDB into a different MDF here was how i did it:
Right click ASPNETDB.mdf in server explorer and publish to a .sql file.
open the .sql file using file - open. (not connected) will appear in the name.
Right click the attached target database and show properties (TargetDatabase.MDF)
Get the connection string out of the properties window..
copy everything in the connection string after "Data Source=.\SQLEXPRESS;"
Click the Execute Sql button (Ctrl + Shift + E)
in the server name type .\SQLEXPRESS
In the Additional Connection paramaters tab, paste the data you copied in step 4
Click connect and select the relevant target MDF file by its disk location from the list of files.
Click connect.
refresh the target DB