Scott on Writing

Musings on technical writing...

Working with SQL Server 2005 Express Databases

I teach an ASP.NET Programming II course at Univeristy of California - San Diego Extension that, for the final project, involves creating a forum site not unlike the ASP.NET Forums. To make the project easier to move from lab to home to work to me for grading, I encourage my students to use SQL Server 2005 Express Edition databases in the App_Data folder.

Earlier in the class the students created a MessageBoard database created with, at minimum, tables for storing Forums, Threads, and Posts. Later, after learning about Membership, they needed to add the membership and roles-related tables to their existing database. This is a lot easier said than done when using SQL Server 2005 Express Edition.

If you use the SQL Server 2005 Standard Edition you can simply use the aspnet_regsql.exe tool's little wizard, point it to your SQL Server 2005 database, provide the credentials, and you're done. Alternatively you can run this tool from the command-line, using something like (with the -E using a “trusted connection,“ although user ID/password credentials can also be supplied):

aspnet_regsql.exe -S server -d database -E -A all

While this concept still applies for SQL Server 2005 Express Edition, it can be a little harder to get the server and database names right. What database server is SQL Server Express installed on? And what's the database name for a .MDF file in the App_Data folder?

Assuming you are working on an ASP.NET application locally, the server name will be: localhost\SQLExpress

The database name is (and here's it can get a bit tricky), is the path to the MDF file when it was created. So, say that you have an ASP.NET application created in the classroom lab at C:\Labs\Website\App_Data\MessageBoard.mdf. The name of the database is C:\Labs\Website\App_Data\MessageBoard.mdf, meaning you could install the membership services from the command-line using:

aspnet_regsql.exe -S localhost\SQLExpress -d “C:\Labs\Website\App_Data\MessageBoard.mdf” -E -A all

Now, imagine that you zip up your files onto a USB keychain drive, go home, and copy your project files to C:\Home\Website. Now, if you wanted to create the services, you'd think you'd just type in:

aspnet_regsql.exe -S localhost\SQLExpress -d “C:\Home\Website\App_Data\MessageBoard.mdf” -E -A all

Ah, but the database name is C:\Labs\Website\App_Data\MessageBoard.mdf. Eep. So when you run the above command the database can't be found and cryptic error messages abound. Essentially, it can't find the database C:\Home\Website\App_Data\MessageBoard.mdf so it tries to create a database file in the default directory (%PROGRAM FILES%\Microsoft SQL Server\MSSQL.1\DATA) with the filename C:\Home\Website\App_Data\MessageBoard.mdf. This, of course, causes problems since that's not a valid filename. Ick.

So how do we fix this? There are a couple optios. The easiest is probably to download the (free) SQL Server 2005 Management Studio Express program and attach the database file. Then, from the Properties pane you can see the database name. You can then use this with aspnet_regsql.exe. (You could also rename the database at this point...)

If you want to be 3l33t you can use sqlcmd, attach the database (sp_attach_db) giving it a friendly name, which you can then use to run the aspnet_regsql.exe command line program against. Something like:

sqlcmd -S localhost\SQLExpress -Q “EXEC sp_attach_db 'Foobar', N'pathToDBfile'”

And then:

aspnet_regsql.exe -S localhost\SQLExpress -d Foobar -E -A all

While the above may make help fulfill any geek quota you're trying to maintain, the easiest approach, by far, is to just download the SQL Server 2005 Management Studio Express product. With that you can work with SQL Server 2005 Express Edition databases much like you would with standard and up versions.

Hope this helps!

posted on Monday, March 27, 2006 6:35 PM

Feedback

# SQL Express Database Instance Names 3/28/2006 8:53 AM Keith Barrows - StarPilot

I want to thank Scott for a great explanation on the naming of SQL Express Database Instances. ...

# re: Working with SQL Server 2005 Express Databases 3/30/2006 4:07 PM Scott Mitchell

For a more in-depth look at adding the membership-related schema to an existing SQL Server 2005 Express Edition database in the App_Data folder, see:
http://aspnet.4guysfromrolla.com/articles/040506-1.aspx

# re: Working with SQL Server 2005 Express Databases 4/16/2006 8:14 PM Chris Mangiapane

I suspect this problem started back when I install VS Studio 2005 and it install SQL 2005 Express which upgraded (i.e. blew away) my SQL 2000 Server installation and I installed SQL 2005 Standard on top on the SQL 2005 Express in order to get the new SQL 2005 Server Management Studio installed.

I no longer have SQL 2005 Express installed on my client.

I am trying to use the MembershipSqlProvider and ASPNETDB for ASP.NET login, and am unable to connect to the ASPNETDB database. Using aspnet_regsql I have successfully created the ASPNETDB on both my client PC and on my server PC (which also has SQL 2005 Standard installed).

Specifically, here are my ‘issues’; any help would be most appreciated.

1) When I attempt to login to my ASP.NET 2.0 web app, I get the error; “The user instance login flag is not supported on this version of SQL Server. The connection will be closed.” Can’t I use the MembershipSqlProvider and ASPNETDB for forms authentication with ‘normal’ SQL 2005 Standard? If so, can you please help me out with a connection string? This connection string and other variations fail:

<add name="ASPNETDB" connectionString="Data Source=MANGIA-SERVER;Initial Catalog=aspnetdb;Integrated Security=True;User Instance=False;"/>

2) SQL 2005 Express will not install on my client PC apparently because I have SQL 2005 Standard already installed. Do I need to uninstall SQL2005 Standard?

# When aspnet_regsql.exe won't connect 4/24/2008 1:53 PM Nimble Coder

When aspnet_regsql.exe won't connect

# re: Working with SQL Server 2005 Express Databases 5/11/2008 2:20 AM viggy

where is the 'aspnet_regsql.exe' cuz i need it

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

Add To Your Reader

My Links

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<May 2008>
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Comment Stats

DayTotal% of Total
Sunday 1866.8%
Monday 37913.9%
Tuesday 45316.7%
Wednesday 50418.5%
Thursday 53519.7%
Friday 49418.2%
Saturday 1666.1%
Total 2717100.0%

Hour1Total% of Total
12:00 AM 652.4%
1:00 AM 682.5%
2:00 AM 622.3%
3:00 AM 742.7%
4:00 AM 572.1%
5:00 AM 1033.8%
6:00 AM 1084.0%
7:00 AM 1585.8%
8:00 AM 1716.3%
9:00 AM 1475.4%
10:00 AM 1716.3%
11:00 AM 1816.7%
12:00 PM 1886.9%
1:00 PM 1696.2%
2:00 PM 1605.9%
3:00 PM 1324.9%
4:00 PM 1073.9%
5:00 PM 923.4%
6:00 PM 913.3%
7:00 PM 963.5%
8:00 PM 833.1%
9:00 PM 782.9%
10:00 PM 792.9%
11:00 PM 772.8%
Total 2717100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.54144
Monday 5.22339
Tuesday 4.28419
Wednesday 7.67637
Thursday 6.90607
Friday 5.48411
Saturday 5.33160
Total 5.842717

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 7.0035
8:00 AM 5.35107
9:00 AM 6.32278
10:00 AM 6.47246
11:00 AM 4.41181
12:00 PM 6.88330
1:00 PM 3.00111
2:00 PM 5.41222
3:00 PM 8.64285
4:00 PM 4.0589
5:00 PM 5.92154
6:00 PM 4.52113
7:00 PM 9.67174
8:00 PM 9.80147
9:00 PM 5.05111
10:00 PM 5.4265
11:00 PM 4.5732
Total 5.842717

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles