You are on page 1of 5

http://www.w3schools.

com/ado/ado_conn
ect.asp
ADO Database Connection
« Previous Next Chapter »

Before a database can be accessed from a web page, a database connection has to be
established.

Create a DSN-less Database Connection

The easiest way to connect to a database is to use a DSN-less connection. A DSN-less


connection can be used against any Microsoft Access database on your web site.

If you have a database called "northwind.mdb" located in a web directory like


"c:/webdata/", you can connect to the database with the following ASP code:

<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"
%>

Note, from the example above, that you have to specify the Microsoft Access database
driver (Provider) and the physical path to the database on your computer.

Create an ODBC Database Connection

If you have an ODBC database called "northwind" you can connect to the database with
the following ASP code:

<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Open "northwind"
%>

With an ODBC connection, you can connect to any database, on any computer in your
network, as long as an ODBC connection is available.
An ODBC Connection to an MS Access Database

Here is how to create a connection to a MS Access Database:

1. Open the ODBC icon in your Control Panel.


2. Choose the System DSN tab.
3. Click on Add in the System DSN tab.
4. Select the Microsoft Access Driver. Click Finish.
5. In the next screen, click Select to locate the database.
6. Give the database a Data Source Name (DSN).
7. Click OK.

Note that this configuration has to be done on the computer where your web site is
located. If you are running Personal Web Server (PWS) or Internet Information Server
(IIS) on your own computer, the instructions above will work, but if your web site is
located on a remote server, you have to have physical access to that server, or ask your
web host to do this for you.

The ADO Connection Object

The ADO Connection object is used to create an open connection to a data


source. Through this connection, you can access and manipulate a database.

View all methods and properties of the Connection object.

http://msdn.microsoft.com/en-us/library/cf131f6b%28VS.80%29.aspx

Data Access in Client and Middle-Tier Programming


How to: Connect to Data in an Access Database

You can establish communication between your application and an Access database by
creating a connection that points to the actual database file (.mdb).

You connect to data in Access files by running the Data Source Configuration Wizard
and selecting Database on the Choose a Data Source Type page.

Tip
Drag an .mdf or .mdb file from Windows Explorer into Solution Explorer to automatically
configure your connection and start the Data Source Configuration Wizard ready for
you to select the objects to use in your application.
You can start the wizard by selecting the Add New Data Source command from the
Data menu or from within the Data Sources window. Alternatively, you can select the
Add Existing Item command from the Project menu and browse to the desired database
file.

After completing the wizard, a copy of the database (.mdb file) and a strongly typed
dataset file (.xsd) are added to your project. The selected database objects are
immediately available in the Data Sources Window for dragging onto your form. For
more information, see Displaying Data Overview.

Note
When adding the .mdb file through the Data Source Configuration Wizard, you are
given the choice of adding the file to your project or leaving it in its original location. For
more information, see How to: Manage Local Data Files in Your Project.
Note
The dialog boxes and menu commands you see might differ from those described in Help
depending on your active settings or edition. To change your settings, choose Import and
Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To connect to data in an Access database with the Add New Data Source
command

1. On the Data menu, click Add New Data Source.


2. Select Database on the Choose a Data Source Type page.
3. Select New Connection to create a new data connection.
Note
If the Data source is not Microsoft Access Database File, then select Change to open
the Choose/Change Data Source dialog box, select Microsoft Access Database File,
and then click Continue.
4. The default provider is .NET Framework Provider for OLE DB. For more
information, see Choose/Change Data Source Dialog Box.
5. In the Database file name box, enter the path to the .mdb file, or click the
Browse button to locate the database file.
6. Enter login information if required by your database. For more information, see
Add/Modify Connection (Microsoft Access).
7. Click OK, and then click Next.
8. Select Yes to copy the .mdb file into your project, or No to connect to the
database file in its current location. For more information, see How to: Manage
Local Data Files in Your Project.
9. Click Next.
10. Expand the tree of objects, and select the database objects to use in your
application.
11. Replace the default DataSet name if desired.
12. Click Finish.
The dataset you just created is now available in the Data Sources window. Open
the Data Sources window by selecting Show Data Sources from the Data menu.

To connect to data in an Access database with the Add Existing Item


command

1. Select Add Existing Item from the Project menu, or drag the .mdb file from
Windows onto Solution Explorer.
2. Expand the tree of objects, and select the database objects to use in your
application.
3. Replace the default DataSet name if desired.
4. Click Finish.

The dataset you just created is now available in the Data Sources window. Open
the Data Sources window by selecting Show Data Sources from the Data menu.

Next Steps
To add functionality to your application

• Select items in the Data Sources window and drag them onto a form. For more
information, see Displaying Data Overview.

Note
If the data source does not appear in the Data Sources window, click the Refresh button
in the window.

Security
Storing sensitive information (such as the server name, user name, and password) can
affect the security of your application. Using Windows Authentication (also known as
integrated security) is a more secure way to control access to a database. For more
information, see Securing Connection Strings.

See Also
Tasks

How to: Manage Local Data Files in Your Project


Walkthrough: Displaying Data on a Form in a Windows Application
How to: Connect to Data in a Database
How to: Connect to Data in a Web Service
How to: Connect to Data in an Object
How to: Connect to Data in a SQL Server Express Database

Concepts

Local Data Overview


TableAdapter Overview
Dataset Designer
Data Sources Overview

You might also like