views:

251

answers:

2

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?

A: 

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.

Russ Cam
That'd be nice, but I don't think it works with attached databases (at least my attempts failed horribly). Or am I wrong?
Will
A: 

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.

ericvg