Chức năng: Đếm số bản ghi có trong một table của DB
Ngôn ngữ: asp - Nhóm: database
CÚ PHÁP
long = RecordCount2(connstring, tableName)
Hàm RecordCount2 đếm số lượng bản ghi trong bảng và trả về ở dạng long.
Sử dụng được với các DB: , MSSQL và MySQL.
Các đối số:
constring: Đường dẫn đến DB tồn tại.
tableName: Tên bảng trong DB.
VÍ DỤ
<%
'--- Use RecordCount to determine the record count returned from a given Table Name.
Dim a, cn
cn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.Mappath("/database/mydata.mdb") & ";"
a = RecordCount2(cn, "YourTableName")
Response.Write a & " Records Found!"
%>
ASP Code
<%
Private Function RecordCount2(byVal connstring, byVal TableName)
'--- MSAccess, MSSQL and MySQL
Dim objCn, bErr1, bErr2, strErrDesc, objRs, strSQL
On Error Resume Next
Set objCn = Server.CreateObject("ADODB.Connection")
objCn.Open ConnString
If Err Then
bErr1 = True
Else
strSQL = "SELECT COUNT(*) AS Record_Count FROM " & TableName
Set objRs = objCn.Execute(strSQL)
RecordCount2 = CLng(objRs("Record_Count"))
objRs.Close
Set objRs = Nothing
If Err Then
bErr2 = True
strErrDesc = Err.Description
End If
End If
objCn.Close
Set objCn = Nothing
On Error GoTo 0
If bErr1 then
Err.Raise 5109, "RecordCount2 Function", "Bad connection string. Database cannot be accessed."
RecordCount2 = Null
ElseIf bErr2 then
Err.Raise 5109, "RecordCount2 Function", strErrDesc
RecordCount2 = Null
End If
End Function '--- RecordCount2
%>