针对附加数据库运行 sql 脚本?

发布于 2024-08-18 06:44:23 字数 283 浏览 4 评论 0原文

我有一个附加到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冷心人i 2024-08-25 06:44:23

我遇到了同样的问题,这对我有用。

  1. 右键单击脚本并选择连接>连接

  2. 应该已经为本地 SQLEXPRESS 实例设置了服务器名称和身份验证。

  3. 单击选项按钮

  4. 单击其他连接参数选项卡

  5. 使用以下内容作为指导粘贴到数据库文件的路径:

    AttachDBFilename=C:\Path\To\Database\LocalData.mdf;database=LocalData;

  6. 单击连接按钮

如果连接仍然遇到问题,可能是因为已经有一个打开的连接。检查服务器资源管理器,如果连接已打开,请右键单击并选择关闭连接。

只要 SQL 脚本保持连接,此过程还将创建持久连接。您需要关闭脚本或选择右键单击>连接>断开。

更多信息可以在这个问题中找到:
EF4 生成数据库

I ran into this same problem and here is what worked for me.

  1. Right-click on the script and choose Connection > Connect

  2. The server name and authentication should already be set for the local SQLEXPRESS instance.

  3. Click the Options button

  4. Click the Additional Connection Parameters tab

  5. Paste in the the path to your database file using the following as a guide:

    AttachDBFilename=C:\Path\To\Database\LocalData.mdf;database=LocalData;

  6. 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

叹梦 2024-08-25 06:44:23

我假设您的意思是您在 Visual Studio 中有一个本地 MDF 文件(如 App_Data 文件夹)?

您可以通过连接命名管道来使用 SQL Management Studio。

首先,使用烦人的查询工具在 Visual Studio 中对附加数据库打开一个新查询。在属性窗口中的 ServerName 下,以“your-PC-name\mini-guid”格式保存该迷你 guid。使用 Management Studio 进行连接,如下所示:

\\.\pipe\mini-guid-here\tsql\query

例如,

\\.\pipe\7789925E-DCAA-4A\tsql\query

您应该看到数据库下列出的文件名。

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:

\\.\pipe\mini-guid-here\tsql\query

e.g.,

\\.\pipe\7789925E-DCAA-4A\tsql\query

You should see the filename listed under databases.

甩你一脸翔 2024-08-25 06:44:23

使用 SqlCmd 实用程序怎么样?

sqlcmd 实用程序可让您输入
Transact-SQL 语句、系统
程序和脚本文件位于
命令提示符,在查询编辑器中
SQLCMD 模式,在 Windows 脚本文件中
或在操作系统中 (Cmd.exe)
SQL Server 代理作业的作业步骤。
该实用程序使用 OLE DB 来执行
Transact-SQL 批处理。

How about using the SqlCmd utility?

The sqlcmd utility lets you enter
Transact-SQL statements, system
procedures, and script files at the
command prompt, in Query Editor in
SQLCMD mode, in a Windows script file
or in an operating system (Cmd.exe)
job step of a SQL Server Agent job.
This utility uses OLE DB to execute
Transact-SQL batches.

苏佲洛 2024-08-25 06:44:23

嗯,当我尝试将 ASPNETDB 的内容导入到不同的 MDF 中时,我遇到了同样的问题,这是我的做法:

  1. 在服务器资源管理器中右键单击 ASPNETDB.mdf 并发布到 .sql 文件。

  2. 使用 file - open 打开 .sql 文件。 (未连接)将出现在名称中。

  3. 右键单击附加的目标数据库并显示属性 (TargetDatabase.MDF)

  4. 从属性窗口中获取连接字符串。
    复制“Data Source=.\SQLEXPRESS;”之后连接字符串中的所有内容

  5. 单击“执行 Sql”按钮 (Ctrl + Shift + E)

  6. 键入 .\SQLEXPRESS

  7. 在“附加连接参数”选项卡中,粘贴您在步骤 4 中复制的数据

  8. 单击“连接”并选择相关从文件列表中按其磁盘位置选择目标 MDF 文件。

  9. 单击“连接”。

  10. 刷新目标数据库

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:

  1. Right click ASPNETDB.mdf in server explorer and publish to a .sql file.

  2. open the .sql file using file - open. (not connected) will appear in the name.

  3. Right click the attached target database and show properties (TargetDatabase.MDF)

  4. Get the connection string out of the properties window..
    copy everything in the connection string after "Data Source=.\SQLEXPRESS;"

  5. Click the Execute Sql button (Ctrl + Shift + E)

  6. in the server name type .\SQLEXPRESS

  7. In the Additional Connection paramaters tab, paste the data you copied in step 4

  8. Click connect and select the relevant target MDF file by its disk location from the list of files.

  9. Click connect.

  10. refresh the target DB

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文