views:

167

answers:

2

I have AdventureWorks database mdf file in my VS 2010 project. Is there anyway I can create Membership tables inside my AdventureWorks database. I know I can detach the database attach in SQL SERVER 2008. Create the tables and then detach. But I don't have SQL SERVER 2008 and I want to see if this can be done using command line tool.

I tried this but no use:

aspnet_regsql.exe -d AdventureWorks.mdf -A mr -E -S .\SQLEXPRESS

Update:

If I right click and see the properties of the AdventureWorks.mdf database then it shows the name as

"C4BE6C8DA139A060D14925377A7E63D0_64A_10\ADVENTUREWORKSWEBFORMS\ADVENTUREWORKSWEBFORMS\ADVENTUREWORKS\APP_DATA\ADVENTUREWORKS.MDF"

This is interesting!

+1  A: 

I think the problem is that you're trying to do this on a detached SQLExpress database. You might have another copy of AdventureWorks that's actually permanently attached, which is why you're not seeing any errors.

Please try the following:

1) If you don't already have a copy of SQL Server Management Studio [Express], get it - it'll be easier to work with for this than Visual Studio.

2) Run the following script:

USE AdventureWorks

SELECT name, physical_name
FROM sys.database_files

3) If you get an error that says the database does not exist, skip to step 5. If you see physical_name entries that don't match the local in your app_data folder, continue to the next step. If you see entries that are in that same app_data folder, then I'm stumped.

4) Run the following to detach the existing DB:

EXEC sp_detach_db 'AdventureWorks'

5) Run the following to attach the SQL Express DB for your app:

EXEC sp_attach_db 'AdventureWorks',
    'C:\inetpub\wwwroot\MyApp\App_Data\ASPNETDB.MDF',
    'C:\inetpub\wwwroot\MyApp\App_Data\ASPNETDB_log.ldf'

6) Run the aspnet_regsql tool again with the same options you used originally, except do not include .mdf at the end of the database name.

7) Verify in SSMS[E] that the tables were created.

8) Detach the database again with EXEC sp_detach_db 'AdventureWorks' (you'll need to do this if the app relies on ad-hoc attachment in its connection string, which I'm betting it does).

Aaronaught
Can I do this without using SS 2008 Express and only using the command line options for the aspnet_regsql?
azamsharp
@azam: No, the `aspnet_regsql` tool doesn't support the `AttachDbFileName` connection string keyword or ad-hoc databases in general, and you won't be able to use those particular `EXEC` commands from Visual Studio. You don't need to install the entire SS 2008 Server, just the Management Studio (SSMSE). The 2005 version is also fine if you're running SQL Server 2005 Express. Or you can use any other SQL tool - the old Query Analyzer, Toad, whatever - but Visual Studio and the `aspnet_regsql` tool won't cut the mustard, to the best of my knowledge.
Aaronaught
There is one other option, which is to just change your application's connection string to use a permanently-attached database instead. The problem you're having is that you're trying to run `aspnet_regsql` against a different database from the one that your application is using. If you create a normal, attached database, which you *can* do from Visual Studio, then you can run the tool against it.
Aaronaught
A: 

Run this command. Replace C:\My Project\APP_DATA\aspnetdb.mdf with the path to your mdf file:

aspnet_regsql -A all -C "Data Source=.\SQLEXPRESS;Integrated Security=True;User Instance=True" -d "C:\MyProject\APP_DATA\aspnetdb.mdf"
37Stars