Connecting to a Database in ASP.NET 2.0
If you are using ASP.NET 2.0, one of the things you will have almost do for sure is connect to a database. This article describes how to do this and also shows some code examples of connecting to your database.
First, you need to set up your connection string. One thing I would recommend is sticking your connection string in your web.config file. This will allow you to connect to your database more easily from any page in your project.
In your web.config file
In your web.config file, you need to set up your connection string in the configuration->appSettings->connectionStrings part the file. The connection string in the following example is for a MS SQL 2000 Database. To find out information on how to build your connection string, try this site. Here is an example:
<add name="DBConnectionStringName" connectionString="Data Source=DB.IP;Initial Catalog=DBName;User ID=UserID;Password=password;Provider=SQLOLEDB;" providerName="System.Data.SqlClient"/>
In your ASP.NET VB file
In your ASP.NET VB page, you then need to connect to the database using this connection you just created in the web.config file. Here is an example of how to do that:
Dim strConnection As String
strConnection = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("DBConnectionStringName").ConnectionString
objConnection = New OleDbConnection(strConnection)
Make sure you close the connection after you are done using it for database efficiency reasons. You can do that like this:
Web site and all contents © Copyright DominicAcito.com 2007, All rights reserved.
Free website templates