Telling an ASP processing component ADO (ActiveX Data Object)

xiaoxiao2021-03-06  60

Telling an ASP processing component ADO (ActiveX Data Object)

Source: Unknown Author: Unknown Recommended rating: Visits: 9

Telling an ASP handling data for the component ADO (ActiveX Data Object). Connect an Access Library <% 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 the code of object conn creating a Connection property. Let's! ^ _ ^ path = server.mappath ("info.mdb") Returns the absolute path of Info.mdb with the mappatch method of the Server object with the ASP. CONN.OPEN "provider = microsoft.jet.oledb.4.0; data source =" & pats Create a connection for Access database using Conn. Open method of an object = microsoft.jet.OleDb.4.0 represents an Access connection. The Data Source = "& Path behind is specified for the absolute path to the database to connect.: ·) Second, strings Connect a SQL Server library <% set conn = server.createObject (" adoDb.connection "conn.open" Driver = {sql server}; server = localhost; uid = username; pwd = password; database = dataname "%> Looks on this code. SET CONN = Server.createObject (" AdoDb.Connection ") is created A Connections object :-) Now we use this object to open a database connection conn.open "driver = {SQL Server}; server = localhost; uid = username; pwd = password; database = dataname" CONEEN Open method is open After connecting, the root of the root driver = {SQL Server} means that the library of SQL Server is connected, and the localhost in Server = localhost represents this machine's IP of course, your database server IP is here. Being a corresponding value; uid = username; pwd = password; these two are the username of your database server. A Database = DataName is the name of the database you want *: -} To this Successfully created a SQL Server connection. Connection is established, but the object to use the RecordSet property Do you need a little knowledge, that is, the T-SQL language we must use T-SQL to select a recordset to transfer to Recordset properties. Life, I will tell you a simple SQL statement to make it easy for everyone to practice.

Look at this example: Select * from tablename where uid = 'coolshow' SELECT is followed by a *, * is a wildcard representing all the fields in the selection table can also specify one or more fields such as this to write SELECT UID only open one The UID field, or SELECT UID, PWD, SEX, TEL, and Address simultaneously opens the UID, PWD, SEX, TEL, and Address five fields. Rear, from Tablename is open that table, there are several usage, because of relatively complicated, and our focus is not here, so don't say more, if you have any questions, you can write a letter to let him ask me. Write an article about T-SQL :-), WHERE UID = 'CoolShow' The meaning of this sentence means that all values ​​that match the UID field is equal to the record set of the string coolshow, or use and and more to connect more Conditions, this is not detailed here :) Ok, two mainstream databases we are connected, and you can simply use it, and you should use the RecordSet property to read data. Let us first create an object of a RecordSet property set RS = Server.createObject ("AdoDb.Recordset") Very simple piece of code with the CreateObject method of the ASP's own Server object creates an object of a RecordSet property, everyone will find This code is almost right, and the ASP creates objects is like this :) Now let's open a recordset with the Recordset object RS. Rs.open "Select * from tablename where uid = 'coolshow'", conn, 1, 1 to see this sentence is easy to use the Open method of the object RS of the RS attribute we just created, followed by the Open method of RS, follow The three parameters were separated by a comma. The first parameter is not used, it is the second T-SQL statement that is selected: _) is the object we have created the object of the connected Connection property. More, I will tell you here. If I read only data settings "1, 1" is enough, if you want to add data, set "1, 2" if you want to override the data is best to 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? OLEDB error? The question is often here. Check that you are enough for your two parameters. Ok, two objects (Object) we all finished ^^ Now let us enter the specific practice of processing data.

First, the display data doesn't look at me, Monkey is anxious ~ :) 嘿 ~ Adjust the atmosphere. Below I write a code, you can see if you can read it, if you read, you have explained the use of the entire ADO. I 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 take a look at the next step. ADO's data shows <% set conn = server.createObject ("adoDb.connection") conn.open "driver = {SQL Server}; server = localhost; uid = username; pwd = password; database = dataname" set = server .CreateObject ("AdoDb.Recordset") rs.open "Select * from tablename where uid = 'coolshow'", conn, 1 ,1 if xif and 1. b If the data pointer points to the header of the recordset The tail of the tail of the tail of the section is empty, and the response.write "Data" "Data" "DO" "Do Until Rs.eof" can be displayed until the recording end of the end of the end of the RESPONSE.WRITE RS ("1 in the field ") 'Show a field of a line Response.Write RS (" 2 "fields")' The same RESPONSE.WRITE RS ("3" in the field ") 'The same RESPONSE.WRITE" "A row print RS.MOVENEXT' Move the data pointer to the next row LOOP 'cycle end%> This code I added a note to believe that most of you have already understood :) Now that our new things are only three, that is, rs.bof rs.eof Rs.Movenex t these three methods are related to the data pointer, the first two RS.BOF and RS.EOF are used to determine if the data pointer starts or at the end of the record set, and Bof and EOF are obvious B represents Begin, e represents End is also head The meaning of root tail, both of them return a Boolean value. Let's take a look at rs.movenext, it is very different from the top two. He is used to control the data pointer, so that the pointer moves to the next one, we can display a number of rows of a table to him. To this, you have displayed a table's data to 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 = server .CreateObject ("adoDb.recordset") rs.open "Select * from tablename where uid = 'coolshow'", conn, 1, 2 'Here we have to add data, change the parameters to 1, 2 rs.addnew' tell RS We want to add a row RS ("Field 1") = value 1 'assigns a field assignment RS ("2" field ") = value 2' 同 上 r (" field 3 ") = value 3) = value 3 'Notify RS on the RS.Update' We want to write this line to the database%> This code can write a new line into the database, whether the Access's library is still the library of SQL Server, is like this. Here we have two methods I haven't seen before rs.addnew ketdate where RS.Addnew is necessary to add a row and RS.Update is written, and it needs to be used when modifying the database. Now you have successfully added a record to the database through the ASP page. Third, modifying data modification 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, change the parameters to 2,3 rs (" 1 ") = Value 1 'gives a new value RS to the row to be modified ("2" field ") = value 2' 同 r (" field 3 ") = value 3 '同 同 上 d = = r r r r 把Modify this line Write Database%> Take this code root to add data to the code comparison, you will find that only two different points are the parameters behind the conn, and there is no addnew, pair :) Modify the difference between the root added Only here. Oh, it is very easy to use ADO to modify data. You have learned.

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

New Post(0)