asp中COM组件中如何连接数据库的代码
程序员文章站
2023-11-24 19:27:52
vb- 工程 -引用 - ado 2.0 ,建一个类db,工程名设为 sqlconn 以下是偶程序中的...
vb- 工程 -引用 - ado 2.0 ,建一个类db,工程名设为 sqlconn
以下是偶程序中的部分代码,大家有空可以试试
option explicit
public strerror as string
public conn as new adodb.connection
public rs as new adodb.recordset
public property get errorstring() as string
errorstring = strerror
end property
public function conn_open(strsqlservername, strsqldbusername, strsqldbpassword, strsqldbname)
dim strcon as string
set conn = new adodb.connection
on error goto errhandler:
strcon = "provider=sqloledb;server=" & strsqlservername & ";user id=" & strsqldbusername & ";password=" & strsqldbpassword & ";database=" & strsqldbname & ";"
conn.open strcon
errhandler:
strerror = "错误源:" & err.source & vbcrlf & "描述:" & err.description
exit function
end function
function getrootid(byval classid)
dim query
query = "select class_id,rootid from tblcategory where class_id='" & classid & "'"
set rs = conn.execute(query)
if not (rs.eof and rs.bof) then
getrootid = rs("rootid")
else
getrootid = classid
end if
rs.close
set rs = nothing
end function
编译后,在asp中这样用
function getrootid(byval id) ' 获取根类id号
set s_db = server.createobject( "sqlconn.db")
s_db.conn_open strsqlservername,strsqldbusername,strsqldbpassword,strsqldbname
getrootid = s_db.getrootid(id)
set s_db=nothing
end function
以下是偶程序中的部分代码,大家有空可以试试
option explicit
public strerror as string
public conn as new adodb.connection
public rs as new adodb.recordset
public property get errorstring() as string
errorstring = strerror
end property
public function conn_open(strsqlservername, strsqldbusername, strsqldbpassword, strsqldbname)
dim strcon as string
set conn = new adodb.connection
on error goto errhandler:
strcon = "provider=sqloledb;server=" & strsqlservername & ";user id=" & strsqldbusername & ";password=" & strsqldbpassword & ";database=" & strsqldbname & ";"
conn.open strcon
errhandler:
strerror = "错误源:" & err.source & vbcrlf & "描述:" & err.description
exit function
end function
function getrootid(byval classid)
dim query
query = "select class_id,rootid from tblcategory where class_id='" & classid & "'"
set rs = conn.execute(query)
if not (rs.eof and rs.bof) then
getrootid = rs("rootid")
else
getrootid = classid
end if
rs.close
set rs = nothing
end function
编译后,在asp中这样用
function getrootid(byval id) ' 获取根类id号
set s_db = server.createobject( "sqlconn.db")
s_db.conn_open strsqlservername,strsqldbusername,strsqldbpassword,strsqldbname
getrootid = s_db.getrootid(id)
set s_db=nothing
end function
上一篇: sqlserver 数据类型转换小实验