Active|activex|ado|object| data Talk about the components of ASP Processing data ADO (ActiveX Data Object)
Connect an Access library in a string form
<%
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 to create a connection
The object of the property conn code is very simple! ^_^
Path=server. MapPath ("Info.mdb") returns the Mappatch method of the server object with ASP
The absolute path of the Info.mdb.
Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data source= "&path
Create a connection to an Access database by using the Conn object's Open method
Provider=Microsoft.Jet.OLEDB.4.0 on behalf of the connected is an access connection and the
The data source= "&path specifies the absolute path of the database to which you want to connect. :·)
Two, string form to connect 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 the code above.
Set Conn=server. CreateObject ("Adodb.connection") said above is to create a
The object of the connections:-)
Now we use this object to open a database connection
Conn. Open "Driver={sql
Server};server=localhost; Uid=username; Pwd=password;database=dataname "
The Conn Open method opens a connection with the back root of a string Driver={sql Server}
It means that the connection is a library of SQL Server, and the localhost in server=localhost represents
The IP of this machine of course your database server IP is how much here is changed to the corresponding
Value Uid=username; Pwd=password; These two are the username and secret that you logged into your database server
Code. A database=dataname below is the name of the database you want to make:-} so far you have
Successfully created a connection to SQL Server.
The connection was established, but to do data processing using the object of the Recordset property we need a little more knowledge,
That's the T-SQL language. Select a recordset to pass to the recordset attribute I
To tell you a simple SQL statement to facilitate the use of everyone in practice. Look at the example below: Select *
From TableName where uid= ' Coolshow '
The select is followed by a *,* is a wildcard represents all the fields in a select table, or you can specify a
Or more than one field like this write the Select uid only opens a UID field, or select
Uid,pwd,sex,tel,address Open uid,pwd,sex,tel,address Five fields at the same time. Behind
The FROM TableName is open that table, there are several uses here, due to the relative complexity, and we
The point is not here so there is not much to say, if you have any questions you can write to the editor
Let him ask me to write an article about T-SQL:-) Look back, where uid= ' coolshow '
To select all of the values that match the UID field is equal to the string Coolshow Recordset, or you can use the
and to connect multiple conditions, not in detail here:)
OK, two major databases we are connected, T-SQL language you can also use the simple, the bottom of the
Tell me how to use the Recordset property to read the data.
Let's start by creating an object for the Recordset property
Set Rs=server. CreateObject ("ADODB.") Recordset ")
A very simple piece of code takes advantage of the CreateObject method of the ASP's own server object to create a
Object of the Recordset property, you will find the code deviation of the object that created the connection at the front edge of the code
No, that's right, ASP creates objects like this: Now let's use the recordset's object RS to
Open a recordset.
Rs.Open "SELECT * FROM tablename where uid= ' Coolshow '", conn,1,1 see
This is not easy to understand with the Recordset property we just created the object Rs open method opens a
Record set, followed by three parameters separated by commas, the first argument is not to say, is to select data
T-SQL statement The second one: _ is the object that we have created the connection property of the connection conn
There are more than two arguments, I'm here to tell you a little bit, if you only read the data set
"1,1" is enough, if you want to add data, set "1,2" if you want to overwrite the data best settings "
2,3 ". These two parameters must be remembered otherwise in the future programming will bring you a lot of trouble, looking at
SQL statements Yes, Object usage is also true, database table structure is also true, why OLE DB error? Ask
The problem is often out here, check you these two parameters is enough to give sufficient RS object permissions let him do what you want him to do
of things. Okay, two objects (object) We're all done ^^ now let's get into the data processing tool.
Body in practice.
One, display data
Don't look at me with hungry eyes, monkey hurry what ~:) Hey, adjust the atmosphere. I'm writing a code here.
You see if you can read it, if you read it, it means the whole of ADO. You've learned half of it:)
<%
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
%>
Well, if you understand, we'll take a look at the next step, the ADO 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 rs.eof and Rs.bof then ' if the data pointer points to the end of the head root of the recordset
Indicates that the recordset is empty.
Response.Write "No data to display" "Print" no data to display
Do Until Rs. EOF ' loops until end of recordset
Response.Write rs ("Field 1 name") ' shows a field of a row
Response.Write rs ("Field 2") ' Ibid.
Resp