Using XMLHTTP to implement remote management databases in ASP

zhaozj2021-02-16  37

Download source program - 143 KB

A Review of Remote Database Administer has been used as a remote administrative database several years ago, the CGI-BIN module is used as a remote management database. Now, CGI-BIN is getting less and less because it is very slow, and it is difficult to maintain.

In recent years, the component object model (COM) has been widely used, unfortunately, is a very troublesome thing to register a COM component on a virtual host.

On the .NET or J2EE platform, we can easily develop the N-layer web application. But for the website, we have more convenient way to remotely manage the database, such as using XMLHTTP and ADO / ADOX.

RDBA works RDBA's workflow as follows:

1 The client issues a query request to the server.

2 The server accepts and executes, and then returns the value to the client.

3 The client accepts the results from the server and displayed.

RDBA depends on two technologies: 1 client and server interaction (send command, return result) channel: XMLHTTP.2 server for obtaining data intermediate layer: ADO / ADOX.

The RDBA's workflow map is as follows:

Use XMLHTTPXMLHTTP to send or accept XML information via HTTP requests.

In fact, XMLHTTP has many other uses. You can use it to send commands to the server (available using XML, String, Stream, or Unsigned Array). The command can also be used as a parameter of the URL to send results information to the client (XML, String, Stream, or unsigned array) More Please refer to information:

It is very simple to use XMLHTTP in the client, just 5 steps: 1. Create an XMLHTTP object. 2. Specify methods, URLs, and verification, open XMLHTTP on the server. The open method can be "post" or "get". 3. Send Request information to the server. 4. Wait until the result is accepted from the server. 5. Release the XMLHTTP object.

XMLHTTP method: Open BSTRMETHOD, BSTRURL, Varasync, BStruser, BSTRPasswordbstrMethod: Used to open the connected HTTP method, such as GET or Postbstrull: URL on the request server, such as http: //myserver/mypath/myfile.asp. Varasync: logical type. Indicates whether it is synchronized. The default is true (call immediately returns). However, it is usually set to false to wait for the return value of the server. BSTRUSER: The authenticated username information. BSTRPASSWORD: Identified password information.

Send Varbodyvarbody: Types can be: BSTR, SafeArray Of UI1 (Unsigned Bytes), the iDispatch interface of the XML DOM object, or istream.

SetRequestHeader Bstrheader, BStrValuebstrHeader: The name of the HTTP header to be set. BSTRVALUE: The value of the http header. If you want to POST data, you can add an HTTP header to tell the reception side that is sending data: xmlhttp.setrEquestHeader "Content-Type", "Application / X-www-form-urlencoded"

XMLHTTP attribute: OnReadyStateChange: Specifies the event handler that is called when the ReadyState property is changed. ResponseBody: RESPONSERESEXSETEXT: RESPONSERESPONSETEXT: RESPONSEXSEXML in the form of a string: The following is the code snippet under XML Document Response:

Function GetResult (URLSTR)

DIM XMLHTTP

DIM RETSTR

SET XMLHTTP = CreateObject ("MSXML2.XMLHTTP") 'creates an object

ON Error ResMe next 'error handling

XMLHTTP.Open "Post", Urlstr, False 'use "post" method to open connection

'Send form data

XMLHTTP.SetRequestHeader "Content-Type", "Application / X-WWW-FORM-URLENCODED"

Xmlhttp.send 'Sends an HTTP request to the server

If Err.Number = 0 Then 'If successful

Retstr = xmlhttp.responsetext 'Waiting for data from the server

Else

Retstr = "Address No" 'error message

END IF

Set Xmlhttp = Nothing 'Release Object

GetResult = RETSTR 'Returns Response to the caller

END FUNCTION

The parameter of the getResult () function is the URL to be requested, you can add sub-parameters, such as: urlstr = "Server.asp? Cmd =" ​​& cmd & "&" & db = "& dB &" Table = "& Table CMD: command type Such as: query, modify, delete, etc. DB: Database name on the server Table: Table name

What data type is used? As mentioned above, data transmitted by XMLHTTP channels can have many types. The most used is XML DOM and String.

What data type is selected depends on the needs of the program.

In order to obtain data from the server, the client and server use XML data is a good method. Because this can handle large amounts of data, request the server to add / delete / modify / query records, or sort / filter data in client.

The purpose of this article is to remotely manage the database on a virtual host, so this requires two functions: 1. Remote management database: Add / delete / modify / query remote database / table / field. 2. Data in remote management database: Add / Delete / Modify / Query Record

So here we use XMLHTTP to send / accept strings

The benefits of doing this are obvious: there is no new page without flashing.

Since it is a string, you can use "Response.Write" to return the result. Once the server response is completed, the client uses the "XMLHTTP.ResponseText" method to get the entire result and return to the caller, used to refresh the page data.

If an asynchronous method is used, the client will have a certain delay after pressing the button, and the client must wait to return data. This can be avoided if you use an asynchronous or use XML DOM.

Using adox

ADO cannot be used to remotely manage the database, we can use Adox. Adox is an extension of ADO, which provides more functions for processing databases. Once authenticated by identity, and your role is a database administrator, then you can do anything through Adox.

This Sample Requests: Generates a dynamic SELECT menu to display the database name under the specified path. Generate a dynamic SELECT menu to display the table names in the selected database in the above menu. The field name is dynamically listed according to the selected database and table.

Depending on the change of the virtual directory path and the change of the SELECT, the displayed content is displayed and the refresh page does not flash.

Use scripting.filesystemObject to get the database name for a given path. Use the Adox to get the table name and field name. code show as below:

Get the table name in a database:

Sub gettables_server (dbname)

DIM I

DIM CATDB 'AdoDb.catalog object

Set catdb = server.createObject ("adox.catalog") 'Create an AdoDb.catalog object

ON Error ResMe next 'error handling

CATDB.ActiveConnection = "provider = microsoft.jet.Oledb.4.0;" & _

"Data Source =" & Server.mappath (dbname) 'opens the connection

'If you need to add your username and password

If Err.Number = 0 Then 'success

'Dynamic Production Select menu

Response.write "" & .Columns (i) .name & ""

'Display a field name

Else

Response.write " & .columns (i) .name &" "

'Display a field name

END IF

NEXT

End with

Else

'Error Message

Response.Write "error: You cannot open the database -" & dbname

END IF

Set catdb = nothing 'release adodb.catalog object

End Sub

Note: All tables in this example uses the self-adding field as the primary key. If your database is not used as a primary button or not a primary key, you need to override the above code. It is a good habit using the self-adding field as the primary key.

Use the samplea screenshot of gois rdba is showed asface. 2.

SYSTEM Required:

To Run this Sample, The Follows Should Be Installed On your PC:

Windows 98 / ME / XP / 2000, MSXML 4.0, MS Access 2000, IE 5.5 or Above.

Install and Execute: 1. Download The Sample Zip File - Supervisor.zip.2. Unzip The File Up in Your IIS, for Example: C: / inetpub / wwwroot / supervisor. 3. Input "localhost / supervisor / supervisor.asp" in the url address on your IE, then GO.4. By a little changing, such as change the path, authorize the user, you can upload this sample source to your virtual hosting to administer your own database.There are several sample databases and two source files in the downloadable ZIP file The sample databases will automatically unzip to a "database" path under the working path, for example:. / superviosr / database.

Two source files are: Server: Server.asp Client: Supervisor.asp

转载请注明原文地址:https://www.9cbs.com/read-26248.html

New Post(0)