'================================================
'说明:连接数据库
'参数:curSession,数据库连接的指针,用于后面具体访问的操作
'connection_string,连接字符串,如
'connection_string="Provider=MSDAORA.1;Password=password;User ID=user;Data Source=orcl;Persist Security Info=True"
'返回值:0表示连接数据库成功,其它的为出错信息。
'================================================
Function db_connect( byRef curSession ,connection_string)
dim connection
on error Resume next
' 打开连接
set connection = CreateObject("ADODB.Connection")
If Err.Number <> 0 then
db_connect= "Error # " & CStr(Err.Number) & " " & Err.Descrīption
err.clear
Exit Function
End If
connection.Open connection_string
If Err.Number <> 0 then
db_connect= "Error # " & CStr(Err.Number) & " " & Err.Descrīption
err.clear
Exit Function
End If
set curSession=connection
db_connect=0
End Function
'==============================================
'说明:断开数据库连接
'参数:curSession,数据库连接的指针
'返回值:无
'==============================================
Function db_disconnect( byRef curSession )
curSession.close
set curSession = Nothing
End Function
'===============================================
'说明:通过查询语句,得到一个记录集
'参数:curSession,数据库连接的指针,SQL,相关SQL语句
'返回值:函数返回一个记录集'
'==============================================
Function db_execute_query ( byRef curSession , SQL)
set rs = curSession.Execute( SQL )
set db_execute_query = rs
End Function
'=============================================
'说明:获取记录集的行数
'参数:curRS,记录集的指针
'返回值:返回行数
'===============================================
Function db_get_rows_count( byRef curRS )
On error resume next
dim rows
rows = 0
curRS.MoveFirst
Do Until curRS.EOF
rows = rows+1
curRS.MoveNext
Loop
db_get_rows_count = rows
End Function
'=============================================
'说明:得到记录集当中某一行某一列的值
'参数:curRecordSet,记录集的指针,rowIndex,行的标识,从0开始,
'colIndex,列的标识,从0开始,可以是列名
'============================================
Function db_get_field_value( curRecordSet , rowIndex , colIndex )
dim curRow
curRecordSet.MoveFirst
count_fields = curRecordSet.fields.count-1
If ( TypeName(colIndex)<> "String" ) and ( count_fields < colIndex ) then
db_get_field_value = -1 'requested field index more than exists in recordset
Else
curRecordSet.Move rowIndex
db_get_field_value = curRecordSet.fields(colIndex).Value
End If
End Function
补充说明:数据库连接字符串(连接数据库的时候用到的),可以通过新建一个后缀名为udl的文件,通过该文件完成数据库的连接之后,用文本的方式打开该文件,就可以得到数据库连接的字符串了。
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/