开发者

Running sql scripts against an attached database?

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 ge开发者_JAVA技巧nerate 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?


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


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜