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   

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<February 2010>
SMTWTFS
31123456
78910111213
14151617181920
21222324252627
28123456
78910111213

Comment Stats

DayTotal% of Total
Sunday 2046.8%
Monday 42514.1%
Tuesday 51617.1%
Wednesday 55318.4%
Thursday 58019.2%
Friday 54718.2%
Saturday 1886.2%
Total 3013100.0%

Hour1Total% of Total
12:00 AM 772.6%
1:00 AM 812.7%
2:00 AM 682.3%
3:00 AM 822.7%
4:00 AM 692.3%
5:00 AM 1264.2%
6:00 AM 1183.9%
7:00 AM 1806.0%
8:00 AM 1926.4%
9:00 AM 1585.2%
10:00 AM 1876.2%
11:00 AM 1936.4%
12:00 PM 2016.7%
1:00 PM 1846.1%
2:00 PM 1695.6%
3:00 PM 1354.5%
4:00 PM 1153.8%
5:00 PM 1063.5%
6:00 PM 1013.4%
7:00 PM 1073.6%
8:00 PM 923.1%
9:00 PM 882.9%
10:00 PM 893.0%
11:00 PM 953.2%
Total 3013100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 4.94158
Monday 4.80384
Tuesday 4.08477
Wednesday 7.47680
Thursday 6.25675
Friday 5.02462
Saturday 4.78177
Total 5.413013

Hour1 Entry MadeAvg.Total
12:00 AM 5.2937
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 3.8550
8:00 AM 3.72134
9:00 AM 6.02295
10:00 AM 5.63276
11:00 AM 4.20193
12:00 PM 6.14350
1:00 PM 3.17133
2:00 PM 5.00230
3:00 PM 7.62320
4:00 PM 4.00108
5:00 PM 6.04169
6:00 PM 4.64116
7:00 PM 8.95188
8:00 PM 8.63164
9:00 PM 5.00115
10:00 PM 6.31101
11:00 PM 4.5732
Total 5.413013

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


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles