自己整理的ASP连接SQL数据库函数及其相关用法

<%
'//==========================================
' = Name: [ Quick Connection ]
' = Author: []
' = StartTime: []
' = LastModify: []
'
' Content
' 1 con_str ' config connection string
' 2 oCon() ' open connection
' 3 oRd(sql_str) ' open RecordSet
' 4 cRd(rdset) ' close RecordSet
'
' =========================================//

'//-------get con_str ---------//
con_str=open_con_str("localhost","sa","sa","db")


'//------- con_str maker ---------//
function open_con_str(ConServe,ConUser,ConPassWord,ConDatabase)
open_con_str = "Driver={SQL Server};Server=" & ConServe & ";UID=" & ConUser & ";PWD=" & ConPassWord & ";Database=" & ConDatabase & ""
end function

'//-------open connection ---------//
function oConn()

set oConn=server.CreateObject("ADODB.connection")
oConn.ConnectionString=con_str
oConn.CursorLocation=3
oConn.Open

end function

'//-------open RecordSet LockType 1---------//
Function oRd(sql_str)

Set oRd = Server.CreateObject("ADODB.Recordset")
oRd.ActiveConnection = con_str
oRd.Source = sql_str
oRd.CursorType = 0
oRd.CursorLocation = 2
oRd.LockType = 3
oRd.Open()
oRd_numRows = 0

End Function

'//-------open RecordSet LockType 3---------//(可返回多条记录集,用于判断记录集的大小)
Function oRd3(sql_str)

Set oRd3 = Server.CreateObject("ADODB.Recordset")
oRd3.ActiveConnection = con_str
oRd3.Source = sql_str
oRd3.CursorType = 3
oRd3.CursorLocation = 2
oRd3.LockType = 3
oRd3.Open()
oRd3_numRows = 0

End Function

'//-------close RecordSet ---------//
function cRd(rdset)

rdset.close
set rdset=nothing

end function

'//-------open Command ---------//
function oCmd(sql_str)
set CmdConn=oConn()
Set oCmd=Server.CreateObject("ADODB.Command")

Set oCmd.ActiveConnection = CmdConn
oCmd.commandText=sql_str
end function

'//-------open Command to Rs---------//
function oCmdRd(sql_str)
set CmdConn=oConn()
Set oCommand=Server.CreateObject("ADODB.Command")

Set oCommand.ActiveConnection = CmdConn
oCommand.commandText=sql_str
Set oCmdRd=oCommand.Execute()
set CmdConn=nothing
end function

'//-------add Command Para---------//
function addCmdPara(Pcmd,pNmae,pType,pIO,pLength,pValue)
set CommandPara1=Pcmd.CreateParameter(pNmae,pType,pIO,pLength)
Pcmd.Parameters.Append CommandPara1
Pcmd(pNmae)=pValue
end function

%>

'//----------------------以上代码可以写在一个文件里,比如qCon.asp中;在需要用到的页面文件中include就可以了,比如(<!--#include file="./qCon.asp"-->)------------------//


用法:
1:建立记录集
tb_str="select * from tb"
set tb=oRd(tb_str)
set tb3=oRd3(tb_str)
2: 修改记录
set oCon=oConn()
添加:insert_str="insert into tb (a1,b1) values ("a1","b2")"
oCon.execute(insert_str)
oCon.close
修改:update_str="update tb set a1='a1',b1='b1' where ...."
oCon.execute(update_str)
oCon.close
删除:del_str="delete tb where ...."
oCon.execute(del_str)
oCon.close

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值