ASP. Net network database: connect to the database

Source: Internet
Author: User

1. Use the database MyFirstAccessDB

The database name used in this instance is MyFirstAccessDB and the version is Microsoft Access 2003 (Chinese version. This database has been created in section 1.4.2.

2. Create a new ASP. NET application

Create an ASP. NET Web application in the integrated development environment of Visual Studio. NET 2003 and name it Example_1_1.

3. Design page ConnectedAcDB. aspx

Rename the default WebForm1.aspx page of the application Example_1_1 to ConnectedAcDB. aspx. You do not need to add any ASP. NET Server-side control or client-side control to this page. Therefore, you do not need to introduce its design interface. The HTML Design Code of ConnectedAcDB. aspx on the page is as follows:

<% @ Page language = "c #" Codebehind = "ConnectedAcDB. aspx. cs"

AutoEventWireup = "false" Inherits = "Example_1_1. ConnectedAcDB" %>

<HEAD> <title> Example_1_1: connect to the Access database </title> </HEAD>

<Body MS_POSITIONING = "GridLayout">

<Form id = "Form1" method = "post" runat = "server">

</Form>

</Body>

</HTML>

4. design the connection string for accessing the database

Store the connection string of the page ConnectedAcDB. aspx accessing the database in the web. config configuration file of the application. The program code is as follows:

<Deleetask>

<Add key = "ACCESSCONNECTIONSTRING"

Value = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =">

</Add>

</AppSettings>

The page ConnectedAcDB. aspx needs to use the class ConfigurationSettings when getting the connection string. It is included in the namespace System. Configuration, so you need to introduce this namespace. The code for obtaining the connection string on the page is as follows:

// Introduce the namespace

Using System. Configuration;

// Obtain the connection string

Private readonly string ACCESSCONNECTIONSTRING

= ConfigurationSettings. deleettings [

"ACCESSCONNECTIONSTRING"]. ToString ();

5. design page ConnectedAcDB. aspx events and functions

Page ConnectedAcDB. aspx calls the Page_Load (object sender, System. EventArgs e) function for initialization. This function calls ConnectAccessDB () to create a connection string for MyFirstAccessDB and display the connection information on the page. The program code for the functions Page_Load (object sender, System. EventArgs e) and ConnectAccessDB () is as follows:
 

Private void Page_Load (object sender, System. EventArgs e)
{
If (! Page. IsPostBack)
{
ConnectAccessDB (); // connect to the MyFirstAccessDb Database
}
}
Private void ConnectAccessDB ()
{// Set the connection string for accessing the database
String accessString = ACCESSCONNECTIONSTRING
+ Server. MapPath ("MyFirstAccessDB. mdb ");
OleDbConnection accessConn = new OleDbConnection (accessString );
Try
{
AccessConn. Open (); // Open the connection to the OLEDB Database
Response. Write ("connection to MyFirstAccessDB data source successful !!! ");
// Display the connection success Information
Response. Write ("<br> connection string of the OLEDB database connection :"
+ AccessConn. ConnectionString );
Response. Write ("<br> name of the database connected to the OLEDB database :"
+ AccessConn. Database );
Response. Write ("<br> the data source connected to the OLEDB Database :"
+ AccessConn. DataSource );
Response. Write ("<br> database driver connected to the OLEDB Database :"
+ AccessConn. Provider );
Response. Write ("<br> server version of The OLEDB database connection :"
+ AccessConn. ServerVersion );
Response. Write ("<br> the opening status of the oledb database connection :"
+ AccessConn. State. ToString ());
// Perform database operations, such as selection, insertion, deletion, and update.
AccessConn. Close (); // Close the connection to the OLEDB Database
Response. Write ("<br> the ole db database connection is closed :"
+ AccessConn. State. ToString ());
}
Catch (Exception ex)
{
Response. Write (ex. Message); // throw a database connection exception
}
}

Set ConnectedAcDB. aspx to the start page of the application. After you press F5, the initial interface shown in 1-8 is displayed.

Figure 1-8 initial page of ConnectedAcDB. aspx

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.