Recommended: ASP implements the end of the long title with an ellipsis Extracting long articles from the database always hinders the layout of the web page. So, I want to extract a part of the characters fixedly, and then there is... a replacement afterward. 1. Principle: determine whether the length of the article exceeds the specified length. If it exceeds the specified length, only the specified one will be displayed.
Many ASP programmers have had the experience of executing database queries and then displaying the query results in HTML tables. Usually we do this:
| The following is the quoted content: <% 'Create connection / recordset 'Populate data into recordset object %> <TABLE> <% Do While not rs.EOF %> <TR> <TD ><%=rs(Field1)% ></TD> <TD ><%=rs(Field2)% ></TD> . </TR> <% rs.MoveNext Loop %> </TABLE> |
If there are many query results, the server will take a lot of time to explain your ASP script, because there are many Response.Write statements to process. If you put all the output results in a long string (from <TABLE> to </TABLE>), the server will just need to explain the Response.Write statement once, and it will be much faster. Some capable guys at Microsoft have turned their ideas into reality. (Note, this is a feature that only ADO 2.0 or above. If you are still using the previous version, please upgrade to the latest version)
With the GetString method, we can use only one Response.Write to display all outputs, which is like a DO...LOOP loop that can determine whether the Recordset is EOF.
The usage of GetString is as follows (all parameters are optional):
String = recordset.GetString(StringFormat, NumRows, ColumnDelimiter, RowDelimiter, NullExpr)
To generate an HTML table from the Recordset result, we only need to care about 3 of the 5 parameters of GetString:
ColumnDelimiter (HTML code that separates columns of the record set), RowDelimiter (HTML code that separates rows of the record set), and NullExpr (HTML code that should be generated when the current record is empty). As you can see in the example of generating HTML table below, each column is separated by <TD >...</TD > and each row is separated by <TR >...</TR >. Let's take a look at the code for the example.
| The following is the quoted content: <%@ LANGUAGE=VBSCRIPT %> <% Option Explicit 'Good coding technique 'Establish connection to DB Dim conn Set conn = Server.CreateObject(ADODB.Connection) conn.Open DSN=Northwind; 'Create a recordset Dim rs Set rs = Server.CreateObject(ADODB.Recordset) rs.Open SELECT * FROM table1, conn 'Store our one big string Dim strTable strTable = rs.GetString(, , </td ><td >, </td ></tr ><tr ><td > , ) %> <HTML> <BODY> <TABLE> <TR ><TD> <% Response.Write(strTable) %> </TR ></TD> </TABLE> </BODY> </HTML> <% 'Cleanup! rs.Close Set rs = Nothing conn.Close Set conn = Nothing %> |
The strTable string is used to store the code that we generate from the HTML table result of SELECT * FROM table1.
There will be </td ><td > HTML code between each column of the HTML table, and the HTML code between each row is </td ></td ><tr ><td >. The GetString method outputs the correct HTML code and stores it in strTable, so that we only need one line of Response.Write to output all records in the dataset. Let's take a look at a simple example, assuming that our query results return the following rows and columns:
| The following is the quoted content: Col1 Col2 Col3 Row1 Bob Smith 40 Row1 Ed Frank 43 Row1 Sue Void 42 |
Then the string returned by the GetString statement will be:
| The following is the quoted content: Bob</td ><td >Smith</td ><td >40</td ><td ></td ></td ></tr ><tr >< td >Ed ... |
To be honest, this string looks lengthy and messy, but it's the HTML code we want. (Note that in the handwritten HTML code, we put <TABLE ><TR ><TD > in front of Response.Write and </TD ></TR ></TABLE > behind it. This is because our formatted string does not contain the strings required for the beginning and end of these tables.)
Share: Common errors and solutions in database calls in ASP The following are some errors in database calls in ASP programs that are often encountered during virtual machine maintenance. Now we collect and organize them as follows: Cannot open the registry keyword (8007000e) Microsoft OLE DB Provider for ODBC Drivers Error '8007000e' [Micr