Author:
Favorite cabbage original:
ASP development with RecordCount only returns to -1!
Go to the Web version, often see someone, why is my recordcount always return -1?
Typical problem:
Can you tell me what is wrong with the code below? <%
Set objconn = server.createObject ("adoDb.connection") set objrst = server.createObject ("adoDb.recordset")
Objconn.open ("Driver = {Microsoft Access Driver (* .mdb)}; dbq =" & Server.mappath ("SeannewellDbsean.mdb))))
strsql = "SELECT * from people"
Objrst.open strsql, objconn? '<< ======== Here is the key
Response.write ("
") Response.write ("
If Objrst.RecordCount> 0 Thenobjrst.movefirstdo while not objrst.eofResponse.write ("name =" & objrst.fields (0)) Objrst.movenextLoopelseresponse.write ("IT's Empty!") Endiff
Objrst.closset Objrst = NothingObjconn.closeset Objconn = Nothing%>
answer:
The RecordCount property returns -1 in the low version of the MDAC. Update the MDAC file to the latest version on your server, the latest MDAC file can be found at www.microsoft.com/data. If you are managed by ISP, you have no right to configure it, you can't upgrade the MDAC file, you must modify the code.
Originally using the following code to check if there is a record: if Objrst.recordcount> 0 Then ...
Please use the following code: if objrst.bof and objrst.eof the 'record set is empty Elsedo While Not Objrst.eof' Process Record Objrst.MovenextLoopend IF
New Zealand Daryl Egarr said:
It can be seen that the code in the reader question is not wrong. The problem is "Return -1" in the low version of the MDAC ", this judgment itself does not have an error, but from the question content, it should not make this assumption, because there is no one line of code in the original question means used. Low version of MDAC.
The author considers the direction of the problem, the key points are not all the cursor types support all attributes and methods (no matter which database system adopted). The real reason for the error in the problem in the problem is that when using the default CursorLocation: RecordSet.cursorLocation = ADUSERVERRECORDCOUNT attribute is only available when the record set's CURSORTYPE is 1 or 3 (i.e., AdopenKeyset, AdopenStatic). The error code does not specify Cursortype, which is 0 type cursor (ie AdopenForwardonly, which is the fastest speed cursor type). At this time, the reference to RecordCount will return 0.
The method of solving the problem is very simple, just put the original code: objrst.open strsql, objconn
Change: Objrst.open Strsql, Objconn, 1
*********************************************************** ******
Finally, additionally:
The cursor type and lock type when connecting the database, is described below:
Cursor type const? Adopenforwardonly? =? 0 forward cursor, providing the fastest running performance for default cursors. Use it to open the Recordset, get all the results from the order of the to the tail. It does not support backward, only allows one-way moving between the results. Const? Adopenkeyset? =? 1 static cursor, reflect the status of data in the table in the table for the first time, the cursor cannot specify whether the data line in the underlying table is updated, delete or add new data. However, unlike the extensive container, the static cursor can be rolled before and after the result. Const? AdoPENDYNAMIC? =? 2 keyboard driven cursor, you can look at certain changes in the bottom of the table in the table, but not all. It can especially accurately reflect whether the data is updated. But it can't identify whether other users have deleted data rows (deleted data rows) in RecordSet). Keyboard driven cursor supports scrolling before and after the result. Const? AdopenStatic? =? 3 ?????????????? dynamic cursor is the most abundant cursor type. When the cursor is opened, you can check any changes to the table on the table and support scrolling. Unlock type const? AdlockReadOnly? =? 1 Default upper lock type, read-only mode Lock allows multiple users to simultaneously read the same data, but cannot change data. Const? AdlockPessimistic? =? 2 Opens the data object in a pessimistic upper lock. This method assumes that there will be other users to access data when you edit the record. Once you start editing records, other users cannot access the data. Const? AdlockOptimistic? =? 3 Open the data object in an optimistic uplock. This method assumes that there is no other user access data when you edit the record. Other users cannot access the record before completing the change. Const? AdlockBatchOptiMistic? =? 4 Use this type when performing multi-line batch updates
Finally, when opening a recordset, you can specify an Options parameter. The OPTIONS parameter indicates the type of command string used to open record sets. The information tells the ADO's string content helps efficiently execute the command string.
You can use the constants below as the options: