Click here to Skip to main content
Click here to Skip to main content

Tagged as

How to create Aspnetdb database

, 13 Nov 2010
Rate this:
Please Sign up or sign in to vote.
This tip describes a shorter way to create the aspnetdb schema on your database instead of using aspnet_regsql command

Microsoft has introduced its default database schema which is served as a role provider, membership provider and profile provider in .NET 2+.
In .NET 4, the database is created automatically, and is added to your App_Data folder and an appropriate connection string will be added to your web.config. But to be able to use this feature in previous versions of .NET, you have to create the database yourself and configure your web.config to point to that database.
For the web.config part, you have to add the following part to it:

 
<connectionStrings>
    <remove name="LocalSqlServer" />
    <add name="LocalSqlServer" connectionString="Data Source=SERVER_NAME;Initial Catalog=DATABASE_NAME;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
 
where SERVER_NAME and DATABASE_NAME should be replaced by your server/database name.
But how do you create the database with the default ASP.NET membership schema? The first solution is that you can use the aspnet_regsql command. A step by step tutorial can be found at http://programming.top54u.com/post/How-to-Create-Aspnetdb.aspx[^]
But as far as I can remember, this command tool was a bit tricky for me and didn't always work as expected especially if you have more than one website in your dev machine that uses Aspnetdb. So I searched for a better solution and here it is:
 
1- Create your database in SQL server.
2- Open up your ASP.NET website in which you'd like to use ASP.NET membership features.
3- Add the following using (or imports in case of VB) directive to your default.aspc.cs (or .vb)
using System.Web.Management;
4- In the load event of your website, add the following method call:
System.Web.Management.SqlServices.Install("YOUR_SERVER", "YOUR_DATABASE", SqlFeatures.All);
 
Just replace the SERVER_NAME and DATABASE with the appropriate values.
Also, if you're using SQL Express edition, there's an overload for that as well:
 
System.Web.Management.SqlServices.Install("YOUR_DATABASE", SqlFeatures.All, "YOUR_CONNECTION_STRING");
 
Now fire up your site and navigate to default.aspx. The code gets executed and if you open your database in SSMS, you'll see that default scripts have been executed on your database to create the default database for ASP.NET 2.0 web applications. You can now delete the command from your page's load event.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kamyar
Software Developer (Senior) Atitav
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
Microsoft Visual Studio .NET Application Programmer, Web Developer. I'm also part of YetAnotherForum's (YAF) Development Team.

Comments and Discussions

 
GeneralCan be customize tables name PinmemberVijay Tanwar12-Feb-14 22:23 
GeneralMy vote of 5 Pinmembermanumohan.ppn2-Mar-13 3:24 
GeneralWish I'd seen this earlier Pinmemberjaimepremy13-Jul-12 17:46 
Generalneeds to? PinmemberKamyar17-Nov-10 22:38 
GeneralReason for my vote of 3 this article needs to PinmemberScofield Puzzle17-Nov-10 22:35 
GeneralGlad this could help you joseph. And thanks for your vote. PinmemberKamyar16-Nov-10 3:18 
GeneralReason for my vote of 5 So much easier than digging for that... Pinmemberjoseph donley15-Nov-10 17:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 13 Nov 2010
Article Copyright 2010 by Kamyar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid