For ADO (ActiveX Data Object), everyone has already heard, but for ADO's practical application, it may not be so clear. Today, I will talk about the Asp's component ADO. After reading it, you will know that it is so simple.
Let us first look at how to create a database connection with the ASP combined with the Connection property.
String Connect a SQL Server library <% set conn = server.createObject ("adoDb.connection") 'Create a connection attribute of object conn conn.open "driver = {SQL Server}; server = localhost; uid = usrname; PWD = password; database = dataname "'Create a connection% of the SQL Server database with CONN. Objects>
The OPEN method in the code is to open a connection, followed by a string. Driver = {SQL Server} means that the connection is a library of SQL Server. And PWD two is the username and password of the login database server, and the last option is the name of the database you want to operate.
Ok, the database we connect, and you should talk about how to read data.
Let us first create an object set RS = Server.createObject ("AdoDb.Recordset") of a RecordSet property and open a recordset with the object RS. Rs.open "Select * from tablename where uid = 'coolshow'", conn, 1, 1
This is the Open method of the object RS opens a recordset, and the three parameters are separated by a comma. The first parameter is the T-SQL statement for selecting the data. The second is the object conn that we have created a good connection with the Connection property. In the last side, the two parameter usage is more, if only data is read, set "1, 1", if you want to add data, set "1, 2", if you want to rewrite the data, you should set "2, 3", These two parameters must remember. Ok, let us have some processing on the data.
First, display 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, 1 if r.eof and 1. If the data pointer points to the head and tail of the recordset, Explanation This time the record set is empty response.write "Data" "Data" "DO Until Rs.eof" can be displayed until the recording end of the end of the replacement end so far to record the end of the replacement end of Response.Write RS ("Field 1") ' Display a line of a line Response.write RS ("2" in the field ") 'Same as the response.write RS (" 3 "in the field")' The same RSPONSE.WRITE "", the line is printed, the wrap RS.MOVENEXT 'will data Move to the next line loop 'cycle end%> rs.bof, rs.eof and rs.movenext These three methods are related to the data pointer, the first two RS.BOF and RS.EOF used to determine the data pointer Whether in the beginning or end of the record set, they will return a Boolean value. rs.Movenext is very different from the first two, it is used to control the data pointer, so that the pointer moves to the next bit, we can display multiple rows of a table to it.
Second, add data <% ... 'code with the top, r o "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 row RS ("1" field ") = value 1' assigning a field assignment RS (" 2 "in field") = value 2 '同 上 r (" Field 3 ") = value 3 'r 同 同 d d d r r r r,, we must write this line to database%>
This code can write a new line into the database. Here is the two methods we have never seen before rs.addnew and rsupdate, where RS.AddNew is necessary to add a row, and rs.Update is written, modifying the database.
Third, modify the data
Modifying the data is similar to adding data, just in the T-SQL statement, you must only choose one line. <% '... The code is the top, slight rs.open "Select * from tablename where uid =' coolshow '", conn, 2, 3' here we have to modify the data, so the parameters are changed to 2,3 rs. ( "Field 1") = Value 1 'gives a new value RS to the row to be modified ("2" in the field ") = value 2' 同 同 上 上 = = = = 上 上 上 上. Update 'Notification RS, we have to write a modified line to database%>
Take this code to compare the code to add data, you will find that there are only two different points, which is the parameters behind the conn change and no addnew, the difference between the modification is only here. Skills that use ADO to modify data. You also learned. Lower is deleted data, and delete data is simpler.
Fourth, delete data <% ... 'code with the top, slight rsopen "select * from tablename", conn, 2, 3 rs.delete' notify RS, we have to delete the current record%> Here we have new contact The method is RS.Delete, as the name suggests, it is to delete the current record! It's all about the common functions of ADO here. I would like to hope that this article can explore everyone's ideas, help friends who like ASP easily cross ADO threshold.