Telling an ASP processing component ADO (ActiveX Data Object)

xiaoxiao2021-03-06  73

Talking about the component ADO (ActiveX Data Object) for ASP processing data, connect an Access library in the form of a string

<%

Set conn = server.createObject ("adoDb.connection")

Path = server.mappath ("info.mdb")

Conn.open "provider = microsoft.jet.oledb.4.0; data source =" & path ""

%>

Set conn = server.createObject ("AdoDb.Connection") This is a CONNECTION

The code of the object of the attribute is very simple! ^ _ ^

Path = server.mappath ("info.mdb") Returns the mappatch method of the Server object with the ASP

Info.mdb absolute path.

Conn.open "provider = microsoft.jet.Oledb.4.0; data source =" & path

Create a connection to Access database with conn. Open method

Provider = microsoft.jet.OleDb.4.0 is a representative connection is an Access connection and behind.

Data Source = "& Path Specifies the absolute path to the database to connect.: ·)

Second, the string is connected to a SQL Server library

<%

Set conn = server.createObject ("adoDb.connection")

Conn.open "driver = {SQL

Server}; server = localhost; uid = username; pwd = password; database = dataname

%>

Look at this code.

SET CONN = Server.createObject ("AdoDb.Connection") is a creation of one

Objects of Connections :-)

Now we open a database connection with this object.

Conn.open "driver = {SQL

Server}; server = localhost; uid = username; pwd = password; database = dataname

Conn's open method is to open a string driver = {SQL Server}

It means that it means a library of SQL Server, and the localhost representing the server = localhost is

This machine's IP is of course your database server IP is changed here.

Value; uid = username; pwd = password; these two are the username you log in to your database server

code. A Database = Dataname next is the name of the database you want *: -} You have

Successfully created a SQL Server connection.

The connection is established, but the object to use the RecordSet property does need a little knowledge,

That is the T-SQL language. We must use T-SQL to select a recordset to send it to the Recordset property.

Talking to everyone a simple SQL statement makes it easy for everyone to practice. Look at this example: SELECT *

From tablename where uid = 'coolshow'

After SELECT is tight with a *, * is a wildcard representative selection table or you can specify one

Or more than one field such as this written SELECT UID only opens a UID field, or selectUID, PWD, SEX, TEL, and address simultaneously opens the UID, PWD, SEX, TEL, and Address five fields. behind

From Tablename is open that table, there are several usage, because of relatively complicated, and we

The focus is not here, so I don't have much here, if you have any questions, you can write your editor.

Let him ask me to write an article about T-SQL :-), WHERE UID = 'CoolShow'

The meaning is that all values ​​that match the UID field is equal to the record set of string coolshow, or

And to connect multiple conditions, this is not described in detail here :)

Ok, two mainstream databases we are connected, and you can use it simple to use, under the lower side.

Tell how to read data using the RecordSet property.

Let us first create an object of a RecordSet property.

SET RS = Server.createObject ("AdoDb.Recordset")

A simple section of code creates one with the CreateObject method of the SERVER object comes with the ASP.

The object of the RecordSet property, everyone will find the code difference between this code to create the object of the previous connection.

Not much, ASP creation object is this :) Now let us use the recordset object RS

Open a recordset.

Rs.open "Select * from tablename where uid = 'coolshow', conn, 1, 1

This is easy to use the OPEN method of the object RS of the Recordset property we just created.

A recordset, followed by three parameters to separate the comma, the first parameter is not used, it is to select the data

The second T-SQL statement second that: _) is the object we have created a good connection with the Connection property of the connection. CONN

There are more than two parameters, I will tell you simple here, if I read the data

Set "1,1" is enough, if you want to add data, set "1, 2" if you want to override the data is best set "

2, 3 ". These two parameters must be remembering to bring you a lot of trouble in the future programming, watching

The SQL statement is right, the object usage is not wrong, the database table structure is not wrong, why is OLEDB error? ask

The title is often here, check your two parameters is enough to give RS object privileges let him do it.

Best. Ok, two objects (Object) we all finished ^^ Now let us enter the processing of data.

Body practice.

First, display data

Don't use the hungry eye to look at me, monkey what ~ :) 嘿 ~ Adjust the atmosphere. Below I write a code

You can see if you can read it, if you read it, you will explain that the entire ADO uses you have learned half :)

<%

Set conn = server.createObject ("adoDb.connection")

Conn.open "driver = {SQL

Server}; server = localhost; uid = username; pwd = password; database = dataname

SET RS = Server.createObject ("AdoDb.Recordset")

Rs.open "Select * from tablename where uid = 'coolshow'", conn, 1, 1

%>

Ok, if you understand, let's look at the next step, the ADO's data shows <%

Set conn = server.createObject ("adoDb.connection")

Conn.open "driver = {SQL

Server}; server = localhost; uid = username; pwd = password; database = dataname

SET RS = Server.createObject ("AdoDb.Recordset")

Rs.open "Select * from tablename where uid = 'coolshow'", conn, 1, 1

if ife.eof and rs.bof then 'If the data pointer points to the head of the recorded head

Description Recording set is empty

Response.write "Data that can be displayed" "Print" No data can be displayed "

Do Until Rs.eof 'loops until the end of the rear end

Response.write RS ("1" ") 'Displays a field of a row

Response.write RS ("2" in the field ") '

Response.write RS ("3" fields ") '

Response.write "

"'A line of printing is completed

Rs.Movenext 'Move the data pointer to the next line

LOOP 'cycle end

%>

This code, I added a comment, I believe most of you have already understood :) Now we have newful things only

There are three, that is, rs.bof rs.eof rs.movenext These three methods are related to data pointers

, The first two RS.BOF and RS.EOF are used to determine if the data pointer starts or at the end of the recordset, Bof

And EOF is obvious B represents Begin, E represents End is also the meaning of the head, they will return one

Boolean value. Let's take a look at rs.movenext. He is very different from the top. He is used to control data pointers.

Let the pointer move to the next one, we can display a plurality of rows of a table to him. To this

The data of a table has been displayed on the ASP page.

Second, add data

<%

Set conn = server.createObject ("adoDb.connection")

Conn.open "driver = {SQL

Server}; server = localhost; uid = username; pwd = password; database = dataname

SET RS = Server.createObject ("AdoDb.Recordset")

Rs.open "Select * from tablename where uid = 'coolshow'", conn, 1, 2 '

Here we have to add data, so change the parameters to 1, 2

Rs.addnew 'tells RS We have to add a line

RS ("Field 1") = value 1 'assigns a field to the row to be added

RS ("2" ") = value 2 '

RS ("3" in the field ") = value 3 '

rs.Update 'Notification RS We want to write this line to the database

%>

This code can write a new line into the database, regardless of the Access library or SQL Server library

It is like this. Here we have two methods I have never seen before rs.addnew kettate.

Rs.addnew is necessary to add a row and rs.Update is written, modify the database,

use. Now you have successfully added a record to the database through the ASP page.

Third, modify the data

Modifying the data root adding data is almost just in the T-SQL statement, you must only choose one line.

<%

Set conn = server.createObject ("adoDb.connection")

Conn.open "driver = {SQL

Server}; server = localhost; uid = username; pwd = password; database = dataname

SET RS = Server.createObject ("AdoDb.Recordset")

Rs.open "Select * from tablename where uid = 'coolshow'", conn, 2, 3 '

Here we want to modify the data, so change the parameters to 2, 3

RS ("1") = value 1 'gives a new value for a field to be modified

RS ("2" ") = value 2 '

RS ("3" in the field ") = value 3 '

Rs.Update 'Notification RS We want to write this line to the database

%>

When you take this code root to add data, you will find that there are only two different points, just behind the conn.

Number, and not addnew, pair :) Modify the difference between the root added is only here. Oh, it is very relaxed.

Skills with ADO to modify data. You also learned. The lower side is deleted data, and the delete data is simpler.

Fourth, delete data

<%

Set conn = server.createObject ("adoDb.connection")

Conn.open "driver = {SQL

Server}; server = localhost; uid = username; pwd = password; database = dataname

SET RS = Server.createObject ("AdoDb.Recordset")

Rs.open "Select * from TableName", CONN, 2, 3

rs.delete 'Notification RS We have to delete the current record

%>

Here our new contact method is RS.Delete, as the name suggests, deleting the current record! No

What is good, J is worth mentioning, if we want to delete data in batches, such as clearing all quantities

= 0 The goods records for convenience not to delete one of us can also use this method.

<%

Set conn = server.createObject ("adoDb.connection")

Conn.open "driver = {SQL

Server}; server = localhost; uid = username; pwd = password; database = dataname

SET RS = Server.createObject ("AdoDb.Recordset")

Rs.open "Select * from TableName", CONN, 2, 3

rs.filter = "Quantity = 0" 'Here we specify a condition

rs.delete 'Notification RS We have to delete the current record

%>

Let's take a look at rs.filter = "quantity = 0" actually he is part of the SQL statement.

It is the name of the field and 0 is the required value. In this way, we speak the database in the database.

. It's all about the common functions of ADO here, this article only wants to explore everyone's ideas, help like

ASP's friends cross ADO threshold.

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

New Post(0)