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 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?

Answers


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.


Need Your Help

Preventing multiple objects from being printed

java inheritance printing polymorphism frequency

For my final CS180 project we have been given the task of creating a program that randomly generates some perishable and nonperishable items that would be found in a grocery cart and then printing a

Titanium titlebar button to toggle about screen

javascript iphone webview titanium appcelerator

Trying to get a button working in the titlebar is becoming a little difficult. At present the button opens when clicked (calls a window with a webview), and closes when an html button is clicked. But

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.