ASP programming introduction (twenty): display data records of ADO components

xiaoxiao2021-03-06  38

The Long March of learning ASP is coming to the end: Wait for the ASP's ADO components. Of course, this is also the most important step, almost all of the learning is for this final purpose. OK, let's take a detailed system to master, step by step, breaking. First, it can be said to have a database: The difference between static pages and dynamic pages is to use the database. Regarding a Web program uses the database and the superiority comparison, it will not be detailed. Main energy is still in how to use the database, how to skillfully use the database, how to use the database more flexibly ... Among them uses the operational database Mainly for database content: display, insert, modify, update, query, and delete. These methods are of course not to eat, and they will slowly feel chewing and understand digestion. Of course, it is very important to have a database. Otherwise, everything is empty talk, and the wife is difficult to have no rice. In general, what we said is actually a database file, which is generated by some database management system (DBMS). At present, the general DBMS is also commonly used for Access, SQLServer, MySQL, Oracle. Of course, the personal site, small businesses are fully sufficient; slightly, the same is the same as SQL Server or MySQL, it is worth noting that mysql is generally combined with another network programming language PHP. Of course, it is ORACLE. Oh, I used to use the Sybase database when I learned PB. The Unix system's Informix database ... DBMS is simply like the cattle -_-!, The current use, we currently use Access: 1, easy to use; 2, the entry master is easy; 3, there is no more suitable for beginners than this. Everything has to start from the actual operation. 1. Open the Access database, select the new database, name it cnbruce.mdb, and save it to a special folder Database [Shielded Advertise] 2, double-click "Use Designer Creative Table" in the newly created database container, pop-up Table 1 In the field name Enter "CN_ID", the data type selects "Auto Number" and selects the key button in the top 20 toolbar to set the field to the primary key. Continue to enter the field "cn_title", the data type selection "text"; NEXT input field "cn_content", the data type selection "Notes"; PS: Note and General Type Text The biggest difference is that the note allows the inserted field value to be relatively large. This is especially important when inserting some longer articles. Still enter the field "cn_author", the data type selection "text"; and switch to "Allow empty string" in "General" below. Yes. PS: This surface allows the value of the CN_AUTHOR field to be empty, which is important when submitting a form, some information does not fill in, but when inserting the database correctly. Finally, enter the field "cn_time", the data type selection "Date / Time", continue to switch to the "default" input "Now () function, save" Table 1 "as" cnarticle "[shielded" Advertising] 3, double-click to open the CNARTILE table, fill in the first line: "cn_title" Enter "Test", "CN_Content" Enter "this is a test", "cn_author" input "CNBRUCE", and time has been added automatically. carry out! Close the table, turn off the database.

[Shielded Advertise] Second, establish a database connection OK, the database has been established, and a line of information is filled out. Then it is now required to display the line information with the ASP. To display, first of all, ASP is still required to establish a connection with the database file, how is it established? Look down. 1, conn.asp: Mainly the function of connecting and opening a database file. This file is recommended to exist, and the location is the folder Database Database Database, which is stored, and 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", is not to say, it is to assign the relative path of the database to a variable to facilitate continuing calls. Set conn = server.createObject ("AdoDb.Connection"), like other components, established an ADO connection, and accepts with an object conn. Connstr = "provider = microsoft.jet.Oledb.4.0; data source =" & server.mappath (db_path) The path of the database). It is necessary to remind it again whether it is FSO to file, folder operation or ADO's operation of the database, the acquisition of the active file is the absolute physical address, under normal circumstances, using the server.mappath method relatively more Great. Conn.open connStr Last Object CONN Opens the database connection through the connection string Connstr. Third, the display database content establishes a database, established and database connections, the following water to the stream is to display the contents of the database through the ASP. 2, showit.asp

<% set = server.createObject ("adoDb.recordset") SQL = "SELECT * from CNArticle" rsopen sql, conn, 1, 1% > <% IF r .e a r ("The article title is:") Else Do Until Rs.EOF response.write ("CN_TITLE") Response.write ("
Article author is:" & RS ("cn_author")) response.write ("
):" & rs ("cn_time") Response.write ("
article content Yes: "& RS (" cn_content ")) Response.write ("


") rs.movenext loopend IF%> <% rs.close set rs = nothingconn.close set conn = Nothing%> Simple debugging page, No accident, I believe you will definitely display the information in the database. (PS: I have done two lines in my database) [Shielded Advertise] Let's take a specific one to understand the meaning of no line: 1, There is no controversy, mainly to call Conn.asp, which has been understood when explaining the conn.asp file. 2, SET RS = Server.createObject ("AdoDb.Recordset") ADO component In addition to the connection connection, there is a Recordset binding record set (I believe that people who have used DW to do ASP now have some feelings in the hometown) Of course RS You can imagine a line in a database table. 3. SQL = "Select * from cnarticle" standard SQL structure query language. Very simple: establishing a database connection, also binding a recordset, what information does it require? That is to filter some records, but the current use is no condition, you can extract all. 4, RS.Open SQL, CONN, 1, 1 Really open the gate to record the set in the database, and the specific parameters will be obtained from the following URL. 5. IF RS.eof and il. The state involves RS.eof and RS.BOF and both logical operations AND. Rs.eof represents the last line in the database table, and rs.bof represents the first line in the database table. The entire statement can be understood that if the last line in the current database is the first line in the database table, it can be sure: there is no data in the current database table.

6, Do Until Rs.eof ... rs.movenext loop is mainly a Do Loop loop statement, where the end condition of the loop is: until RS.eof, that is, the last line worth the database table. Then there is a license to meet these conditions, it is to show the specific information. You can only display a row in the database table each time, and if you want to continue reading down, then rsmovenext features. 7, RS ("cn_title") and the like are mainly information values ​​for which particular field in the record set. Very simple. 8, don't forget to release the resource space to close the record set connection, turn off the database connection. Fourth, some special conditions 1, have it noticed that the display of the database table information is generally arranged in time, and is often arranged in ascending order. Need to note: Ascending in time, not necessarily there is a field / date type field in each database table, as long as there is an automatic number of fields. Because this field is never repeated, it is increasing. Therefore, in order of time, it is actually sorted by an increase in the number value in the automatic number. Of course, the key to the problem is here, that is, according to the descending order of time, that is, always start from the latest content. What kind of surgery does it specifically need? Very simple, make the connection string make a modification. Add modified by SQL = "Select * from cnarticle" to sql = "select * from cnarticle order by cn_id desc" where the ORDER BY CN_ID is descended by the CN_ID field, DESC is descended. 2. It is time, just need to extract the first or latest information, how do this do it? Similarly, the modification of the connection string is added to SQL = "SELECT * from cnarticle order by cn_id" to add modifications to SQL = "SELECT TOP 3 * from cnarticle order by cn_id desc" where TOP 3 indicates the latest three information content. That's all. Now an article system, news system or message display part of the exquisite content you have not charged. Leave you is: The format of the article may show it wrong, such as entering the car, space is not displayed, then you have to learn the value of the accepted database table (mentioned in several exercises in ASP)

); Article cannot always open the database input information, then you will continue to learn how to insert data into the library table with ASP; then there is more articles, a page is not very trouble? Then you have to learn to page technology ...

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

New Post(0)