How to Create an ADO Connection
In a previous feature, we discussed the basics of Microsoft ActiveX Data Objects (ADO). In this article, we'll begin a series that looks at the details of using ADO in a real-world environment. The sample code in these tutorials will assume that you're a VBScript programmer developing Active Server Pages (ASP) for use with Microsoft Internet Information Server and Microsoft SQL Server. However, keep in mind that ADO is a standard interface and you'll only find minor differences in syntax between languages.
The general principles you learn here will easily carry over to your development environment.
set cn = Server.CreateObject("ADODB.Connection")
Note that the syntax to create the Connection object will vary from language to language. For example, the equivalent command in Visual Basic would be:
cn = new ADODB.Connection
strCS = "Driver={SQL Server}; Server=myserver; Database=mydb; UID=sa; PWD=password"
The Driver argument tells ADO which type of data source you are accessing.
The remainder of the arguments are data source-specific. In this case, you're specifying the SQL Server instance and default database as well as the user credentials you'd like to present to the server.
cn.ConnectionString = strCS
At this point, you've provided the Connection object with everything it needs to know to connect to the data source but you haven't actually connected until you issue the following command:
cn.Open
That's it! You've now successfully opened a connection to a SQL Server database and you're ready to begin issuing commands and interacting with the server!
The general principles you learn here will easily carry over to your development environment.
Creating the Connection Object
The basis of all ADO applications is the Connection object. If you've properly configured your web server, you can create an instance of this object by simply issuing the VBScript command:set cn = Server.CreateObject("ADODB.Connection")
Note that the syntax to create the Connection object will vary from language to language. For example, the equivalent command in Visual Basic would be:
cn = new ADODB.Connection
Setting the Data Source
Once you've created the Connection, you need to tell it which data source it should reference through the use of a ConnectionString. In our example, we're attempting to access a Microsoft SQL Server instance. The ConnectionString contains a list of arguments that define how the data source is accessed. We'll use the following string:strCS = "Driver={SQL Server}; Server=myserver; Database=mydb; UID=sa; PWD=password"
The Driver argument tells ADO which type of data source you are accessing.
The remainder of the arguments are data source-specific. In this case, you're specifying the SQL Server instance and default database as well as the user credentials you'd like to present to the server.
Setting the Connection String
Once you've built this string, you associate it with the Connection object using the following syntax:cn.ConnectionString = strCS
At this point, you've provided the Connection object with everything it needs to know to connect to the data source but you haven't actually connected until you issue the following command:
cn.Open
That's it! You've now successfully opened a connection to a SQL Server database and you're ready to begin issuing commands and interacting with the server!
Source...