ado| Programming | data | Show |ado Learning ASP's Long march is coming to an end: Wait for the ASP's ADO component to be done. Of course this is also the most important step, almost all of the previous learning is for this ultimate goal. OK, here is a detailed system to one by one master, step-by-step, broken.
One, owning a database
It can be said that static pages and dynamic pages The biggest difference is the use of the database. About a Web program using the database and not to use the superiority of comparison, not to analyze carefully, the main focus on how to use the database, how to skillfully use the database, how to use the database more flexibly, etc. ... The operation database is used mainly for the content of the database: display, insert, modify, update, query and delete.
These methods of course also is not a mouthful can eat out, to slowly feel chewing, understanding digestion. Of course, it is important to have a database first. Otherwise, all is empty talk, Qiao daughter-in-law difficult bricks without straw well.
Generally speaking, the database is actually a database file, which is built by some database management system (DBMS). At present the common DBMS is also commonly used to hear access,sqlserver,mysql,oracle. Of course, the general personal site, the small business to take access is completely enough; a little larger is the same Microsoft SQL Server or MySQL, and it's worth noting that MySQL is generally a perfect combination of PHP with another network programming language. Of course, the larger use of Oracle. Oh, once in the study of PB also applied to Sybase database, UNIX system Informix Database ... DBMS is a dime a-_-!.
In other words, we are now directly using Access: 1, easy to use, 2, easy to get started to master, 3, there is no more suitable for beginners.
Everything has to start with the actual operation.
1, open the Access database, select New database, name it Cnbruce.mdb, and save it to a specialized folder database
[Blocked ads]
2, in the New database container Double-click "CREATE table using designer", the pop-up Table 1 window in the field name entered "cn_id", the data type select AutoNumber, and select the Key button in the upper toolbar to set the field as the primary key.
Continue to enter field "Cn_title", Data type Select "Text", Next Enter Field "Cn_content", data type Select "Remark";
PS: The biggest difference between notes and generic text is that the notes allow more field values to be inserted, which is especially important when inserting some longer articles.
Still enter field "Cn_author", Data type Select "Text", and switch to "allow empty string" in "General" below to select "Yes".
PS: This surface allows the value of the Cn_author field to be empty, which is important when submitting a form where some information is not filled out but is inserted correctly into the database.
Finally enter field "Cn_time", Data type Select "Date/Time", continue to switch to "default value" in "General" enter "Now" function
Finally, save "Table 1" as "cnarticle"
[Blocked ads]
3, double hit open cnartile table, fill in the first line: "Cn_title" enter "Test", "cn_content" enter "This is a test", "Cn_author" enter "Cnbruce", time has been automatically added. Complete! Close the table and close the database.
[Blocked ads]
Second, establish a database connection
OK, the database has been established and a line of information has been filled in. So what is needed now is to display the line information with ASP.
To show, first or the need for ASP to establish a connection with the database file, how to build? Look down.
1,conn.asp: The main starting point is the ability to connect and open a database file. The file is recommended to exist separately, and the location is and is stored in the database folder at the same physical level.
<%
Db_path = "Database/cnbruce.mdb"
Set conn= Server.CreateObject ("ADODB.") Connection ")
ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" &server.mappath (Db_path)
Conn. Open ConnStr
%>
Db_path = "Database/cnbruce.mdb", needless to say, is to assign the relative path of the database to a variable so that it is convenient to continue the call below.
Set conn= Server.CreateObject ("ADODB.") Connection "), as well as other components, establishes an ADO connection and is accepted by an object conn.
ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" &server.mappath (Db_path), which is clearly the connection string, It includes the Drive method OLE DB to open the database, and the connection to which database is opened (that is, the path to the database).
Need to remind again: whether the FSO to file, folder operation or ADO to the database operation, to be manipulated files are obtained absolute physical address, under normal circumstances, the use of Server.MapPath method is relatively good.
Conn. The open connstr last object conn the connection to the database through the connection string connstr.
Third, display the contents of the database
Set up a database, and set up a connection to the database, the following is naturally the contents of the database through the ASP display.
2,showit.asp
<!--#include file= "conn.asp"-->
<%
Set rs = Server.CreateObject ("ADODB.") Recordset ")
sql = "SELECT * FROM Cnarticle"
Rs. Open sql,conn,1,1
%>
<%
If Rs. EOF and Rs. BOF Then
Response.Write ("no article Yet")
Else
Do Until Rs. Eof
Response.Write ("article title is:" & RS ("Cn_title"))
Response.Write ("<br> article author is:" & RS ("Cn_author"))
Response.Write ("<br> article Add Time is:" & RS ("Cn_time"))
Response.Write ("<br> article content is:" & RS ("Cn_content"))
Response.Write ("Rs. MoveNext
Loop
End If
%>
<%
Rs.close
Set rs = Nothing
Conn.close
Set conn=nothing
%>
Simple debugging this page, no accident, I believe that the database will be able to display the information. (PS: Two lines are lost in my database)
[Blocked ads]
Here is a specific explanation to understand the meaning of the No line:
1,<!--#include file= "conn.asp"--> there is no dispute, mainly the effect of invoking conn.asp, which is understood when interpreting conn.asp documents.
2,set rs = Server.CreateObject ("ADODB.") Recordset ") ADO components in addition to the connection connection, there are recordset binding recordset (I believe that using DW as an ASP is now beginning to feel a bit back to the hometown) of course, RS can be visualized as a row in a database table.
3,sql = "SELECT * from cnarticle" standard SQL Structured Query language. It's simple: A database connection is established, and a recordset is bound, what information is needed specifically? That is to filter some records collection, but currently using no conditions, that is, can extract all.
4,rs. Open sql,conn,1,1 really opens the door to the recordset in the database, and the following parameters can be obtained from the URL below.
5,if Rs. EOF and Rs. BOF then this statement involves Rs. EOF and Rs. BOF and the logical operation of both. Rs. EOF indicates that the last row in the database table is reached, Rs. BOF indicates that the first row in the database table is reached. The entire statement can be understood that if the last row in the current database is the first row in the database table, it is certain that there is no data in the current database table.
6,
Do Until Rs. Eof
...
Rs. MoveNext
Loop
The main thing is a Do Loop loop statement where the end condition of the loop is: until Rs. EOF, the last line of a database table that is worth it. So, within the license of these conditions, it is to show the specific information.
Each loop can only display a row in the database table, and if you want to continue reading the downlink, then Rs. The MoveNext function is true.
7,rs ("Cn_title") and so on is mainly to show exactly which specific field in the recordset the information value. It's very simple.
8, and finally don't forget to free the resource space to close the Recordset connection and close the database connection.
Four, some special conditions
1, has not noticed that the database table information display is generally according to the chronological order of time, also often mentioned in ascending order of time. Note: In ascending chronological order,