<% Class clsDBHelper Private DefaultConnString Private DefaultConnection Private m_ServerIP Private m_DBName Private m_UserID Private m_UserPW Dim cmd Dim rs Dim inum Private sub Class_Initialize() '---------------------------------------------------- 'SQL OLE DB ConnString '---------------------------------------------------- 'DefaultConnString = "Provider=SQLOLEDB;Data Source=" & m_ServerIP & ";Initial Catalog=" & m_DBName & ";User Id=" & m_UserID & ";Password=" & m_UserPW & ";" '---------------------------------------------------- 'MS SQL DB ConnString '---------------------------------------------------- DefaultConnString = "Driver=SQL Server; Server=" & m_ServerIP & "; Database=" & m_DBName & "; UID=" & m_UserID & "; PWD=" & m_UserPW Set DefaultConnection = Nothing End Sub '소멸자 Private Sub Class_Terminate() Set DefaultConnection = Nothing End Sub '--------------------------------------------------- ' 서버 설정 '--------------------------------------------------- Public Property Set Server(mServer) m_ServerIP = mServer End Property Public Property Set DBName(mDBName) m_DBName = mDBName End Property Public Property Set UserID(mUserID) m_UserID = mUserID End Property Public Property Set UserPW(mUserPW) m_UserPW = mUserPW End Property '--------------------------------------------------- ' SP를 실행하고, RecordSet을 반환한다. '--------------------------------------------------- Public Function ExecSPReturnRS(spName, params, connectionString) If IsObject(connectionString) Then If connectionString is Nothing Then If DefaultConnection is Nothing Then Set DefaultConnection = CreateObject("ADODB.Connection") DefaultConnection.Open DefaultConnString End If Set connectionString = DefaultConnection End If End If Set rs = CreateObject("ADODB.RecordSet") Set cmd = CreateObject("ADODB.Command") cmd.ActiveConnection = connectionString cmd.CommandText = spName cmd.CommandType = adCmdStoredProc Set cmd = collectParams(cmd, params) 'cmd.Parameters.Refresh rs.CursorLocation = adUseClient rs.Open cmd, ,adOpenStatic, adLockReadOnly For i = 0 To cmd.Parameters.Count - 1 If cmd.Parameters(i).Direction = adParamOutput OR cmd.Parameters(i).Direction = adParamInputOutput OR cmd.Parameters(i).Direction = adParamReturnValue Then If IsObject(params) Then If params is Nothing Then Exit For End If Else params(i)(4) = cmd.Parameters(i).Value End If End If Next Set cmd.ActiveConnection = Nothing Set cmd = Nothing If rs.State = adStateClosed Then Set rs.Source = Nothing End If Set rs.ActiveConnection = Nothing Set ExecSPReturnRS = rs End Function '--------------------------------------------------- ' SQL Query를 실행하고, RecordSet을 반환한다. '--------------------------------------------------- Public Function ExecSQLReturnRS(strSQL, params, connectionString) If IsObject(connectionString) Then If connectionString is Nothing Then If DefaultConnection is Nothing Then Set DefaultConnection = CreateObject("ADODB.Connection") DefaultConnection.Open DefaultConnString End If Set connectionString = DefaultConnection End If End If Set rs = CreateObject("ADODB.RecordSet") Set cmd = CreateObject("ADODB.Command") cmd.ActiveConnection = connectionString cmd.Prepared = true cmd.CommandText = strSQL cmd.CommandType = adCmdText Set cmd = collectParams(cmd, params) ' On Error Resume Next ' Response.Write strSQL & "
" rs.CursorLocation = adUseClient rs.Open cmd, , adOpenStatic, adLockReadOnly If Err.Number<>0 Then ErrorLog DefaultConnection.Number, DefaultConnection.Description End IF Set cmd.ActiveConnection = Nothing Set cmd = Nothing If rs.State = adStateClosed Then Set rs.Source = Nothing End If Set rs.ActiveConnection = Nothing Set ExecSQLReturnRS = rs End Function '--------------------------------------------------- ' SQL Query를 실행하고, RecordSet Array을 반환한다. '--------------------------------------------------- Public Function ExecSQLReturnAR(strSQL, params, connectionString) If IsObject(connectionString) Then If connectionString is Nothing Then If DefaultConnection is Nothing Then Set DefaultConnection = CreateObject("ADODB.Connection") DefaultConnection.Open DefaultConnString End If Set connectionString = DefaultConnection End If End If Set rs = CreateObject("ADODB.RecordSet") Set cmd = CreateObject("ADODB.Command") cmd.ActiveConnection = connectionString cmd.Prepared = true cmd.CommandText = strSQL cmd.CommandType = adCmdText Set cmd = collectParams(cmd, params) ' On Error Resume Next 'Response.Write strSQL & "
"& "
"& "
"& "
" rs.CursorLocation = adUseClient rs.Open cmd, , adOpenStatic, adLockReadOnly If Err.Number<>0 Then ErrorLog DefaultConnection.Number, DefaultConnection.Description End IF Set cmd.ActiveConnection = Nothing Set cmd = Nothing If rs.State = adStateClosed Then Set rs.Source = Nothing End If Set rs.ActiveConnection = Nothing Set ExecSQLReturnAR = rs.getRows End Function '--------------------------------------------------- ' SQL Query를 실행하고, RecordSet String을 반환한다. '--------------------------------------------------- Public Function ExecSQLReturnST(strSQL, params, connectionString) If IsObject(connectionString) Then If connectionString is Nothing Then If DefaultConnection is Nothing Then Set DefaultConnection = CreateObject("ADODB.Connection") DefaultConnection.Open DefaultConnString End If Set connectionString = DefaultConnection End If End If Set rs = CreateObject("ADODB.RecordSet") Set cmd = CreateObject("ADODB.Command") cmd.ActiveConnection = connectionString cmd.Prepared = true cmd.CommandText = strSQL cmd.CommandType = adCmdText Set cmd = collectParams(cmd, params) ' On Error Resume Next 'Response.Write strSQL & "
"& "
"& "
"& "
" rs.CursorLocation = adUseClient rs.Open cmd, , adOpenStatic, adLockReadOnly If Err.Number<>0 Then ErrorLog DefaultConnection.Number, DefaultConnection.Description End IF Set cmd.ActiveConnection = Nothing Set cmd = Nothing If rs.State = adStateClosed Then Set rs.Source = Nothing End If Set rs.ActiveConnection = Nothing Set ExecSQLReturnAR = rs.getString(2) End Function '--------------------------------------------------- ' SP를 실행한다.(RecordSet 반환없음) '--------------------------------------------------- Public Sub ExecSP(strSP,params,connectionString) If IsObject(connectionString) Then If connectionString is Nothing Then If DefaultConnection is Nothing Then Set DefaultConnection = CreateObject("ADODB.Connection") DefaultConnection.Open DefaultConnString End If Set connectionString = DefaultConnection End If End If Set cmd = CreateObject("ADODB.Command") cmd.ActiveConnection = connectionString cmd.CommandText = strSP cmd.CommandType = adCmdStoredProc Set cmd = collectParams(cmd, params) cmd.Execute , , adExecuteNoRecords For inum = 0 To cmd.Parameters.Count - 1 If cmd.Parameters(inum).Direction = adParamOutput OR cmd.Parameters(inum).Direction = adParamInputOutput OR cmd.Parameters(inum).Direction = adParamReturnValue Then If IsObject(params) Then If params is Nothing Then Exit For End If Else params(inum)(4) = cmd.Parameters(inum).Value End If End If Next Set cmd.ActiveConnection = Nothing Set cmd = Nothing End Sub '--------------------------------------------------- ' SQL Query를 실행한다.(RecordSet 반환없음) '--------------------------------------------------- Public Sub ExecSQL(strSQL,params,connectionString) If IsObject(connectionString) Then If connectionString is Nothing Then If DefaultConnection is Nothing Then Set DefaultConnection = CreateObject("ADODB.Connection") DefaultConnection.Open DefaultConnString End If Set connectionString = DefaultConnection End If End If On Error Resume Next Set cmd = CreateObject("ADODB.Command") cmd.ActiveConnection = connectionString cmd.CommandText = strSQL cmd.CommandType = adCmdText Set cmd = collectParams(cmd, params) cmd.Execute , , adExecuteNoRecords ' If DefaultConnection.Errors.Count <> 0 Then ' For each objErr in DefaultConnection.Errors ' response.write("

") ' response.write("Description: ") ' response.write(objErr.Description & "
") ' response.write("Help context: ") ' response.write(objErr.HelpContext & "
") ' response.write("Help file: ") ' response.write(objErr.HelpFile & "
") ' response.write("Native error: ") ' response.write(objErr.NativeError & "
") ' response.write("Error number: ") ' response.write(objErr.Number & "
") ' response.write("Error source: ") ' response.write(objErr.Source & "
") ' response.write("SQL state: ") ' response.write(objErr.SQLState & "
") ' response.write("

") ' Next ' Response.End ' End IF if err.number <> 0 then response.Write("SQL :"&strSQL&"
") response.Write("err.description :"&err.Description&"
") response.Write("err.number : "&err.Number&"
") response.Write("err.Source : "&err.Source&"
") Response.End End IF Set cmd.ActiveConnection = Nothing Set cmd = Nothing End Sub '--------------------------------------------------- ' 트랜잭션을 시작하고, Connetion 개체를 반환한다. '--------------------------------------------------- Public Function BeginTrans(connectionString) If IsObject(connectionString) Then If connectionString is Nothing Then connectionString = DefaultConnString End If End If Set conn = CreateObject("ADODB.Connection") conn.Open connectionString conn.BeginTrans Set BeginTrans = conn End Function '--------------------------------------------------- ' 활성화된 트랜잭션을 커밋한다. '--------------------------------------------------- Public Sub CommitTrans(connectionObj) If Not connectionObj Is Nothing Then connectionObj.CommitTrans connectionObj.Close Set ConnectionObj = Nothing End If End Sub '--------------------------------------------------- ' 활성화된 트랜잭션을 롤백한다. '--------------------------------------------------- Public Sub RollbackTrans(connectionObj) If Not connectionObj Is Nothing Then connectionObj.RollbackTrans connectionObj.Close Set ConnectionObj = Nothing End If End Sub '--------------------------------------------------- ' 배열로 매개변수를 만든다. '--------------------------------------------------- Public Function MakeParam(PName,PType,PDirection,PSize,PValue) MakeParam = Array(PName, PType, PDirection, PSize, PValue) End Function '--------------------------------------------------- ' 매개변수 배열 내에서 지정된 이름의 매개변수 값을 반환한다. '--------------------------------------------------- Public Function GetValue(params, paramName) For Each param in params If param(0) = paramName Then GetValue = param(4) Exit Function End If Next End Function Public Sub Dispose if (Not DefaultConnection is Nothing) Then if (DefaultConnection.State = adStateOpen) Then DefaultConnection.Close Set DefaultConnection = Nothing End if End Sub '--------------------------------------------------------------------------- 'Array로 넘겨오는 파라메터를 Parsing 하여 Parameter 객체를 '생성하여 Command 객체에 추가한다. '--------------------------------------------------------------------------- Private Function collectParams(cmd,argparams) Dim params Dim i,l,u,v If VarType(argparams) = 8192 or VarType(argparams) = 8204 or VarType(argparams) = 8209 then params = argparams For i = LBound(params) To UBound(params) l = LBound(params(i)) u = UBound(params(i)) ' Check for nulls. If u - l = 4 Then If VarType(params(i)(4)) = vbString Then If params(i)(4) = "" Then v = Null Else v = Trim(params(i)(4)) End If Else v = params(i)(4) End If ' On Error Resume Next ' If err.number <> 0 then ' Response.Write params(i)(0) & ":" & params(i)(1) & ":" & params(i)(2) & ":" & params(i)(3)& ":" &v&"
" ' End If If VarType(params(i)(4)) = vbString And params(i)(4) = "" Then Set cnParam = cmd.CreateParameter(params(i)(0)) cnParam.attributes=adParamNullable cnParam.type = params(i)(1) cnParam.Direction = params(i)(2) cnParam.Size=1 cnParam.value=Null cmd.Parameters.Append cnParam 'cmd.CreateParameter(params(i)(0), params(i)(1), params(i)(2), params(i)(3), Null) Else cmd.Parameters.Append cmd.CreateParameter(params(i)(0), params(i)(1), params(i)(2), params(i)(3), v) End If End If Next Set collectParams = cmd Exit Function Else Set collectParams = cmd End If End Function End Class %>