日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

VBScript 教程之数据库篇

發(fā)布時間:2024/4/11 数据库 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 VBScript 教程之数据库篇 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
VBScript 教程之?dāng)?shù)據(jù)庫篇,以 vbscript DBHelper 類的方式,封裝數(shù)據(jù)庫連接、查詢、基本的存儲過程訪問方法。 option Explicit
' 數(shù)據(jù)庫讀取選項
Public Const adOpenStatic = 3
Public Const adLockReadOnly = 1
Public Const adLockOptimistic = 3 Public Const adCmdStoredProc = 4
Public Const adInteger=3
Public Const adChar=129
Public Const adVarchar = 200
Public Const adDate=7
Public Const adParamInput=1
Public Const adParamReturnValue=4 Public Const LogTypeInfo = 0
Public Const LogTypeError = 1
Public Const LogTypeWarning = 2 Class DBHelper
??? Private oConn
??? Private Sub Class_Initialize
??????? Set oConn = Nothing
??? End Sub ' ***************************************************************************
??? ' 創(chuàng)建 ADODB.Connection 對象,連接數(shù)據(jù)庫
??? ' ***************************************************************************
??? Public Function Connect(server, database, uid, password)
??????? Dim sDSNRef
??????? Dim sMsg ' 創(chuàng)建 ADO 數(shù)據(jù)庫連接對象
??????? On Error Resume Next
??????? Set oConn = CreateObject("ADODB.Connection")
??????? If Err then
??????????? ShowMessage "錯誤 - 無法創(chuàng)建 ADODB.Connection 對象, 不能查詢 SQL Server: " & Err.Description & " (" & Err.Number & ")", LogTypeError
??????????? Set Connect = Nothing
??????????? Exit Function
??????? End If
??????? On Error Goto 0 ' 構(gòu)建連接字符串
??????? sDSNRef = "Provider=SQLOLEDB;OLE DB Services=0;Data Source=" & server & ";Initial Catalog=" & database
??????? sDSNRef = sDSNRef & ";User ID=" & uid & ";Password=" & password ' 正在連接數(shù)據(jù)庫
??????? ShowMessage "Connecting to SQL Server using connect string: " & sDSNref, LogTypeInfo
??????? On Error Resume Next
??????? oConn.Open sDSNref
??????? If Err then
??????????? sMsg = Err.Description & " (" & Err.Number & ")" ShowMessage "error opening SQL connection: " & sMsg, LogTypeError iRetVal = Failure
??????????? ShowMessage "error opening SQL Connection: " & sMsg, LogTypeError
??????????? For each objErr in oConn.Errors
??????????????? ShowMessage "? ADO error: " & objErr.Description & " (Error #" & objErr.Number & "; Source: " & objErr.Source & "; SQL State: " & objErr.SQLState & "; NativeError: " & objErr.NativeError & ")", LogTypeError
??????????? Next
??????????? Err.Clear
??????????? Set Connect = Nothing
??????????? Exit Function
??????? End If
??????? On Error Goto 0 ShowMessage "Successfully opened connection to database.", LogTypeInfo ' Return the connection to the caller
??????? Set Connect = oConn
??? End Function
??? ' ***************************************************************************
??? ' 創(chuàng)建 ADODB.Recordset 對象,執(zhí)行數(shù)據(jù)庫查詢,返回 Recordset 對象。
??? ' ***************************************************************************
??? Public Function Query(strSQL)
??????? Dim oRS
??????? ' Create ADO recordset object
??????? On Error Resume Next
??????? Set oRS = CreateObject("ADODB.Recordset")
??????? If Err then
??????????? Set Query = Nothing
??????????? ShowMessage "ERROR - Unable to create ADODB.Recordset object, impossible to query SQL Server: " & Err.Description & " (" & Err.Number & ")", LogTypeError
??????????? Exit Function
??????? End If
??????? On Error Goto 0 ' Issue the SQL statement
??????? ShowMessage "About to issue SQL statement: " & strSQL, LogTypeInfo
??????? On Error Resume Next
??????? oRS.Open strSQL, oConn, adOpenStatic, adLockReadOnly
??????? If Err then
??????????? Set Query = Nothing
??????????? ShowMessage "ERROR - Opening Record Set (Error Number = " & Err.Number & ") (Error Description: " & Err.Description & ").", LogTypeError
??????????? For each objErr in oConn.Errors
??????????????? ShowMessage "? ADO error: " & objErr.Description & " (Error #" & objErr.Number & "; Source: " & objErr.Source & "; SQL State: " & objErr.SQLState & "; NativeError: " & objErr.NativeError & ")", LogTypeError
??????????? Next
??????????? oRS.Close
??????????? Err.Clear
??????????? Exit Function
??????? End If
??????? On Error Goto 0 ShowMessage "Successfully queried the database.", LogTypeInfo
??????? Set Query = oRS
??? End Function
??? ' ***************************************************************************
??? ' 創(chuàng)建 ADODB.Command 對象,執(zhí)行數(shù)據(jù)庫存儲過程,返回 Command 對象。
??? ' 注意:存儲過程要求,沒有返回值對象,返回值以Select方式返回,存儲過程需要使用
??? ' set nocount on,禁用影響行數(shù)消息。
??? ' ***************************************************************************
??? Public Function ExecuteProc(strSQL)
??????? Dim oComm,oRS
??????? ' Create ADO recordset object
??????? On Error Resume Next
??????? Set oComm = CreateObject("ADODB.Command")
??????? If Err then
??????????? Set ExecuteProc = Nothing
??????????? ShowMessage "ERROR - Unable to create ADODB.Command object, impossible to query SQL Server: " & Err.Description & " (" & Err.Number & ")", LogTypeError
??????????? Exit Function
??????? End If
??????? On Error Goto 0
??????? ' 分解參數(shù)
??????? Dim sql,cmd,parm,parms,p,index
??????? sql = Split(strSQL," ")
??????? If UBound(sql) = 1 Then
??????????? cmd = sql(0)
??????????? parm = Right(strSQL,Len(strSQL)-Len(cmd))
??????????? parms = Split(parm,",")
??????????? index = 0
??????????? For Each p In parms
??????????????? p = LTrim(Replace(p,"'",""))
??????????????? Dim para
??????????????? Set para = CreateObject("ADODB.Parameter")
??????????????? para.Name = index
??????????????? para.Type = adVarchar
??????????????? para.Size = 1000
??????????????? para.Direction = adParamInput
??????????????? para.Value = p
??????????????? oComm.Parameters.Append para
??????????????? index = index + 1
??????????? Next
??????? Else
??????????? cmd = strSQL
??????? End If ' Issue the SQL statement
??????? ShowMessage "About to issue SQL statement: " & strSQL, LogTypeInfo
??????? On Error Resume Next
??????? oComm.CommandType = adCmdStoredProc
??????? oComm.ActiveConnection = oConn
??????? oComm.CommandText = cmd
??????? Set oRS = oComm.Execute
??????? If Err then
??????????? Set ExecuteProc = Nothing
??????????? ShowMessage "ERROR - Opening Command (Error Number = " & Err.Number & ") (Error Description: " & Err.Description & ").", LogTypeError
??????????? For each objErr in oConn.Errors
??????????????? ShowMessage "? ADO error: " & objErr.Description & " (Error #" & objErr.Number & "; Source: " & objErr.Source & "; SQL State: " & objErr.SQLState & "; NativeError: " & objErr.NativeError & ")", LogTypeError
??????????? Next
??????????? oComm.Close
??????????? Err.Clear
??????????? Set ExecuteProc = Nothing
??????????? Exit Function
??????? End If
??????? On Error Goto 0 ShowMessage "Successfully queried the database.", LogTypeInfo
??????? Set ExecuteProc = oRS
??? End Function
End Class

轉(zhuǎn)載于:https://blog.51cto.com/qijinchao/263270

超強(qiáng)干貨來襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生

總結(jié)

以上是生活随笔為你收集整理的VBScript 教程之数据库篇的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。