DSN
'Data Source Name'
A Data Source Name (DSN) is the logical name that is used by Open Database Connectivity (ODBC) to refer to the drive and other information that is required to access data. The name is use by Internet Information Services for a connection to an ODBC data source, such as a Microsoft SQL Server database.
When you use an ODBC DSN entry to store the connection string values externally, you simplify the information that is needed in the connection string. This makes changes to the data source completely transparent to the code itself.
Open the 'ODBC Data Source Administrator'
( Start -> Control Panel -> Administrative Tools -> Data Sources )
(1) System DSN -- can be used by anyone who has access to the machine. DSN info is stored in the registry.
(2) User DSN -- created for a specific user. Also stored in the registry.
(3) File DSN-- DSN info is stored in a text file with .DSN extension.
DSN with ODBC
假設 database_name 有 TABLE_NAME, 而它有 column "TEST"
<% 'declare the variables Dim Connection Dim Recordset Dim SQL 'declare the SQL statement that will query the database SQL = "SELECT * FROM TABLE_NAME" 'create an instance of the ADO connection and recordset objects Set Connection = Server.CreateObject("ADODB.Connection") Set Recordset = Server.CreateObject("ADODB.Recordset") 'open the connection to the database Connection.Open "DSN=dsn_name;UID=user_name;PWD=password;Database=database_name" 'Open the recordset object executing the SQL statement and return records Recordset.Open SQL,Connection 'first of all determine whether there are any records If Recordset.EOF Then Response.Write("No records returned.") Else 'if there are records then loop through the fields Do While NOT Recordset.Eof Response.write Recordset("TEST") Response.write "<br>" Recordset.MoveNext Loop End If 'close the connection and recordset objects to free up resources Recordset.Close Set Recordset=nothing Connection.Close Set Connection=nothing %>
without DSN
<% 'declare the variables Dim Connection Dim ConnString Dim Recordset Dim SQL 'define the connection string, specify database driver ConnString="DRIVER={SQL Server};SERVER=yourServername;UID=yourUsername;" & _ "PWD=yourPassword;DATABASE=yourDatabasename" 'declare the SQL statement that will query the database SQL = "SELECT * FROM TABLE_NAME" 'create an instance of the ADO connection and recordset objects Set Connection = Server.CreateObject("ADODB.Connection") Set Recordset = Server.CreateObject("ADODB.Recordset") 'Open the connection to the database Connection.Open ConnString 'Open the recordset object executing the SQL statement and return records Recordset.Open SQL,Connection 'first of all determine whether there are any records If Recordset.EOF Then Response.Write("No records returned.") Else 'if there are records then loop through the fields Do While NOT Recordset.Eof Response.write Recordset("FIRST_FIELD_NAME") Response.write Recordset("SECOND_FIELD_NAME") Response.write Recordset("THIRD_FIELD_NAME") Response.write "<br>" Recordset.MoveNext Loop End If 'close the connection and recordset objects to free up resources Recordset.Close Set Recordset=nothing Connection.Close Set Connection=nothing %>