Introduction to ASP Programming (20): ADO component Display Data recording _asp Foundation

Source: Internet
Author: User
Tags add time microsoft sql server sybase database first row access database
Learn the ASP Long March is almost to the end: And so the ASP's ADO components. 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 database content: 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



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"



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.



Two, 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, an ADO connection is established and accepted by an object conn.

connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" &server.mappath (Db_path), which is clearly the connection string, which 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. Open ConnStrThe 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)



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 in the interpretation of conn.asp documents.

2,set rs = Server.CreateObject ("ADODB.") Recordset ")In addition to the connection connection, the ADO component has a Recordset-bound recordset (it is believed that people who have used DW as an ASP are beginning to feel the same way back home) of course RS can be visualized as a row in a database table.

3,sql = "SELECT * from Cnarticle"A 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,1Really open the door to the recordset in the database, the following parameters can be obtained from the URL below. http://www.cnbruce.com/blog/showlog.asp?cat_id=26&log_id=283

5,if Rs. EOF and Rs. BOF thenThis statement involves Rs. EOF and Rs. BOF and the logical operation of both. Rs. EOFIndicates that the last row in the database table is reached. Rs. BOFIndicates 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 down, Rs. MoveNextThe function is so.

7,rs ("Cn_title") and so onThe main thing is to display the information value of which specific field in the recordset. It's very simple.

8, don't forget to release the resource space in the endClose 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, there is not necessarily a time/date Type field in each database table, as long as there is an AutoNumber field available. Because the field is never repeated and is enlarged in turn. Therefore, in chronological order, the number of numbers in the automatic numbering is sorted.

Of course, the key to the problem is to sort out the time in descending order, that is, always starting with the latest content. What kind of surgery does it take?
Quite simply, the connection string is slightly modified.

Add modified by sql = "SELECT * from Cnarticle" to
sql = "SELECT * FROM Cnarticle ORDER by cn_id desc"
Where order by cn_id is through the cn_id field, desc is descending.

2, there is a time, only need to extract the first or the latest information, then how to operate the specific? Same as the connection string modification
Add modified by sql = "SELECT * from Cnarticle ORDER BY cn_id Desc" to
sql = "Select" Top 3* FROM Cnarticle ORDER BY cn_id Desc "
Top 3 indicates that the latest three-piece information is extracted.

That ' s all. Now the essence of an article system, a news system, or a message is that you've done your best.

What's left of you is: The format of the article may show something wrong, for example, carriage returns, spaces are not displayed, then you have to learn to handle the values of the accepted database table (mentioned in several of the ASP exercises); The article can not always open the database input information Ah, then you have to continue to learn how to use ASP to insert data into the library table And then there is more articles, a page to see is not very troublesome? Then you have to learn the paging technology ...

Too much to do, I will detail the following:
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.