How to manipulate the database in ASP

Source: Internet
Author: User
Tags open dbf file mdb database connect odbc ole table name
In ASP, objects used to access databases are collectively called ADO Objects (Activedataobjects), which contain three objects: Connection, Recordset, and command, where Connection is responsible for opening or connecting to the database. The recordset is responsible for accessing the data table, which executes the action query (actionquery) command for the database and executes the storedprocedure of SQL Server. Depending on the three objects or the database, you must also have a database-accessible driver: An OLE DB driver and an ODBC driver. An ADO object can access a database for any database that must have a corresponding OLE DB driver and ODBC driver.

Connect the database and open the datasheet

The methods of connecting different databases (that is, to establish connection instances are different), but the method of accessing data using the Recordset object is much the same when the connection instance is established. The following for different data types, write the corresponding connection function, in the program directly referenced.

The program is written in VB Script scripting language.

1. Establish the Mdbrecordset object. The MDB database is a complete database that may contain several data tables, in which the connection function is to connect the database, and the recordset is to open the datasheet.

Function createmdbrecordset (database file name, data table name, or SELECT statement)

Dim Conn,provider,dbpath

Create a Connection object

Set conn = Server.CreateObject (ADODB. Connection)

provider=provider=microsoft.jet.oledb.4.0;

DBPath = Data source= & Server.MapPath (database file name)

Open Database

Set Createmdbrecordset=server.createobject (ADODB. Recordset)

Open Data table

End Function

2. Create a Recordset object with a password MDB database. It is built like a Recordset object that creates an MDB database without a password, but one more password parameter, that is, the password information must be given when connecting to the database.

Function createsecuredmdbrecordset (database file name, data table name or SELECT statement, password)

Dim Conn,provider,dbpath

Create a Connection object

Set conn = Server.CreateObject (ADODB. Connection)

Provider = provider=microsof.jet.oledb.4.0;

DBPath = Data source=& server.mappath (database file name)

Connect the database, note that you want the password parameter

Set Createsecuredmdbrecordset=server.createobject (ADODB. Recordset)

Open Data table

End Function

3.DBF file is not a standard database file, only equivalent to a data table in the standard database file, so in order to use DBF file, all DBF files can be placed in a directory, so that the directory name as a standard database, each DBF file corresponds to the standard database data table. The directory in the following function is the name of the catalog in which DBF resides.

Function createdbfrecordset (directory name, DBF filename, or SELECT statement)

Dim Conn,driver,sourcetype,dbpath

Create a Connection object

Set conn = Server.CreateObject (ADODB. Connection)

Driver=driver=microsoft Visual Foxprodriver; SOURCETYPE=SOURCETYPE=DBF;

Dbpath=sourcedb= & Server.MapPath (directory name)

To open a database by calling the Open method

Set Createdbfrecordset=server.createobject (ADODB. Recordset)

Open DBF File

Endfunction 4. The DBC database generated by FoxPro is similar to the MDB database, which is a database containing several data tables, so the access method for the DBC database is similar to the MDB database.

Function createdbcrecordset (DBC database file name, data table name, or SELECT statement)

Dim Conn,driver,sourcetype,dbpath

Create a Connection object

Set conn = Server.CreateObject (ADODB. Connection)

Driver=driver=microsoft Visual FoxPro Driver;

SourceType = SOURCETYPE=DBC;

DBPath = sourcedb= & Server.MapPath (DBC database file name)

Connecting to a database

Set Createdbcrecordset=server.createobject (ADODB. Recordset)

Open Data table

End Function

5. The xls file (book) generated by Excel is viewed as a database in which each worksheet (sheet) is treated as a database table.

Function createexcelrecordset (xls filename, sheet name)

Dim Conn. Driver,dbpath

Create a Connection object

Set conn = Server.CreateObject (ADODB. Connection)

Driver=driver=microsoft Excel Driver (*.xls);

DBPath = dbq= & Server.MapPath (xls filename)

To open a database by calling the Open method

Set Createexcelrecordset=server.createobject (ADODB. Recordset)

Open sheet

End Function

6.SQL server is a server-level database that is strictly required and must be entered with a username and password to be used.

Function createsqlserverrecordset (computer name, user ID, user password, database name data table or view table or select instruction)

Dim Params, Conn

Set creatsqlserverconnection = Nothing

Set conn = Server.CreateObject (ADODB. Connection)

Params = Provider=SQLOLEDB.1

Params = Params &;D ata source= & Computer

Params = Params &; User id= & UserID

Params = Params &; Password= & Password

Params = Params &. Initial catalog=& Database name

Conn Open Paras

Set Createsqlserverrecordset = Server.

CreateObject (ADODB. Recordset)

End Function



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.