SoFunction
Updated on 2025-03-04

ASP method to connect to SQL Server 2008 database through ODBC

Create a database connection file【dsnFile] method

 

Create an ODBC DSN file

Before creating a database script, you must provide a way to locate, identify and communicate with the database. The database driver uses Data Source Name (DSN) to locate and identify a specific ODBC-compatible database to pass information from the web application to the database. Typically, DSN contains database configuration, user security, and location information, and can obtain tables in Windows NT registry keys or text files.

ODBC allows you to select the type of DSN you want to create: user, system, or file. User and system DSNs are stored in the Windows NT registry. The system DSN allows all users to log in to a specific server to access the database, while the user DSN uses appropriate security proofs to restrict the connections from the database to a specific user. File DSN is used to obtain tables from text files, provides access to multiple users, and by copying the DSN file, it is easy to transfer from one server to another. For the above reasons, the examples in this topic will use the file DSN.

By "start"Menu Open"control Panel”, you can create a DSN-based file. Double-click the ODBC icon and selectFile DSN"Properties page, click "Add to”, select the database driver, and clickNext step”. Follow the instructions below to configure the DSN for your database software.

Configure the DSN file for Microsoft Access database

  1. exist"Create a new data sourceIn the dialog box, select " from the list box"Microsoft Access Driver”, and then clickNext step”。
  2. Type your DSN file name and clickNext step”。
  3. Click "Finish”Create a data source.
  4. exist"ODBC Microsoft Access 97 InstallerIn the dialog box, clickchoose”. Select the Microsoft Access database file (*.mdb), and clickSure”。

NoticeDue to performance and reliability reasons, we highly recommend you to use "customer"-Server Database Engine "configures data driven by such a web application that must satisfy simultaneous access to more than 10 users. Although ASP can use any ODBC-compatible database, it is designed for use of customer-server databases and has been rigorously tested, including Microsoft ® SQL ServerOracle wait.

ASP supports shared file databases (such as Microsoft ® Access or Microsoft ® FoxPro) as a valid data source. Although in ASP Some examples in the documentation use shared file databases, but we recommend that such database engines be used only for development or limited configuration scenarios. Shared file databases may not be well suited for high-quality, high-quality Web Application Customer-Server database.

 

Configure SQL Server Database Files DSN

NoticeIf the database resides on a remote server, contact the server administrator for additional configuration information; the following procedure uses SQL Server's ODBC default settings, which may not apply to your hardware configuration.

  1. exist"Create a new data sourceIn the dialog box, select " from the list box"SQL Server”, and then clickNext step”。
  2. Type a name for the DSN file and clickNext step”。
  3. Click "Finish”Create a data source.
  4. Type the name, login ID, and password of the server running the SQL Service program.
  1. exist"Create a new data source for SQL ServerIn the dialog box, inserverIn the list box, type the name of the server that contains the SQL Server database, and then clickNext step”。
  1. Select how to verify login ID.
  1. If you want to select SQL Server Verification, enter a login ID and password, and clickNext step”。
  2. exist"Create a new data source for SQL ServerIn the dialog box, set the default database, driver and ANSI ID for stored procedure settings, and then clickNext step”. (To get more information, click ”help”。)
  3. In the dialog box (also named "Create a new data source for SQL Server”), select a character conversion method, and click “Next step”. (For more information, please click “help”。)
  4. In the next dialog box (also named "Create a new data source for SQL Server”), select Login settings.
    NoticeTypically, You can only use logs to debug database access issues.
  5. exist"ODBC Microsoft SQL Server InstallerIn the dialog box, clickTest data source”. If DSN is created correctly,Test resultsThe ” dialog box will indicate that the test is completed successfully.
    SQL server connection and security information
    If you are developing an ASP database application for connecting to a remote SQL Server database, consider the following issues:
    • Connection scheme-   You can choose TCP/IP Sockets and named pipes methods to access remote SQL Server database. When using named pipes, the database user must be Windows NT Confirm, so only the appropriate ones SQL Server Users who access the identity without a Windows NT user account on that computer may be denied access to the named pipe. As an alternative, connections using TCP/IP sockets can be directly connected to the database server without having to pass through intermediate computers using named pipes. Because TCP/IP socket connections can be directly connected to the database server, users can gain access through the confirmation of SQL Server without having to confirm through Windows NT.
      NoticeUsed when connecting to a remote database TCP/IP Sockets improve performance.
    • Security-   If you use SQL Server of Integrate or mix security features, and SQL Server The database is located on a remote server and cannot be used Windows NT ask/Acknowledgement of response. That is, you cannot Windows NT ask/Responsive ID card forwarding to a remote computer, and only basic authentication can be used, which is based on the username and password information provided by the user.
      For more information on this topic, see
      /sqlsupport/
      on the Microsoft SQL Server Technical Support Home page.

 

Configure Oracle Database Files DSN

First, make sure that the Oracle user software is correctly installed on the computer where you want to create the DSN. For more information, please contact the server administrator or refer to the database software documentation.

  1. exist"Create a new data sourceIn the dialog box, select " from the list box"Microsoft ODBC for Oracle”, and then clickNext step”。
  1. Type a name for the DSN file and clickNext step”。
  1. Click "Finish”Create a data source.
  1. Enter the username, password, and server name, and clickSure”。

Notice   DSN For files .dsn Extension, located in \Programs\Common Files\ODBC\Data Sources in the directory.

For more information on creating a DSN file, visit the Microsoft ODBC Web site:/odbc/

====================================================

The first step to accessing database information is to establish a connection with the database source. Provided by ADOConnectionObject, which can be used to establish and manage connections between applications and ODBC databases.ConnectionObjects have various properties and methods that can be used to open and close database connections and issue query requests to update information.

To establish a database connection, first createConnectionAn instance of an object. For example, the following script createsConnectionObject, then open the database connection:

<%
 'Create a connection object
 Set cn = ("")
 'Open a connection; the string refers to the DSN
  "FILEDSN="
%>

Note The DSN string cannot contain spaces whether before or after the equal sign (=).

In this case, the Open method of the Connection object references a DSN-based file that contains information about the location and configuration of the database. You can also explicitly reference the provider, data source, user ID, and password without reference to the DSN.

Connection process

&lt;%
 Set cn = ("") 'Create a database connection object
 Set rsCustomers = ("") 'Create a database query object
   "FILEDSN=" 'Open the database
 strSQL = "SELECT username,password FROM [myDatabase].[dbo].[user] where username='duguying'"
  strSQL, cn  'runSQLStatement
%&gt;

Processing database return data

Set username1= rsCustomers("username") 'GetusernameReturn result of field
 Set password1= rsCustomers("password") 'Get the return result of the password field
  Do Until
   username1 & " " & password1 & "<BR>"
 
  Loop '通过循环Get所有返回记录