自己整理的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

你可能感兴趣的:(sql,SQL Server,asp)