How do I use a connection string to connect to a database?

The following article discusses using a connection string to connect to a database. A connection string allows you to connect your website to a database using a section of code as well as a DSN. Although the DSN is not required, it is recommended. Information about creating a DSN can be found here. Once the connection is established, you can use further code to access the information stored in your database.

The exact code used in your connection string will vary based on the type of database you are connecting to (Access, MS SQL, MySQL) and the programming language you are using (ASP, ColdFusion, PHP). In general, connection strings contain information about the database you are connecting to, including the name of the database, the username and password (if applicable), the name of the database and the DSN name (if applicable).

The following are two examples of connection strings using the programming language ASP.

Microsoft Access

<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.open "<DSN>","<username>","<password>"
%>


Microsoft SQL

<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.open "DSN=<DSN>;UID=<username>;PWD=<password>;DATABASE=<database>"
%>


Further examples of connection strings can be found here.