오늘 회사에 출근하면서 당황스러웠다.
한번도 해보지 못한 Visual Basic를 이용해 Pocket PC에서 데이터베이스를 생성하고 이용하고 제거해야한다.
게다가 컴퓨터에서 EXCEL 파일로 읽을 수 있도록 추출 프로그램을 만들어야 한다.

일단 Pocket PC에서 Pocket Excel은 Automation이 지원안된다. 따라서 TEXT 파일로 C에서 흔히 사용하는 "\t"를 각 항목에 붙여주면 된다.
일단 기본 사용을 위해, deVBuzz.com에 제공하는 소스를 이용해 다음과 같이 함수들을 만들었다.
소스를 첨부한다.

--MORE--

' 사용전 주의 사항
' Project->References 에서 다음 2가지 항목을 체크해야 한다.
'  항목 1 : Microsoft CE ADO Control 3.1
'  항목 2 : Microsoft CE File System Control 3.0
' Project->Components 에서 다음 1가지 항목을 체크해야 한다.
'  항목 1 : Microsoft CE File System Control 3.0

Option Explicit
Public objConnection As ADOCE.Connection    ' 데이터베이스 연결을 위한 전역 변수
Public objUsingFileSystem As FileSystem     ' 파일 시스템을 위한 전역 변수


' 설  명 : 파일 시스템 객체 열기 서브 함수
' 만든이 : 이준희(flyingmt@nate.com)
' 날  짜 : 2004년 03월 04일
Sub OpenFileSystem()
    Set objUsingFileSystem = CreateObject("FILECTL.FileSystem.1")
End Sub


' 설  명 : 파일 시스템 객체 닫기 서브 함수
' 만든이 : 이준희(flyingmt@nate.com)
' 날  짜 : 2004년 03월 04일
Sub CloseFileSystem()
    Set objUsingFileSystem = Nothing
End Sub


' 설  명 : DB 연결 함수
' 만든이 : 이준희(flyingmt@nate.com)
' 날  짜 : 2004년 03월 04일
Function ConnectionOpen(strDBFileName As String, boolMsgShow As Boolean) As Boolean
    On Error Resume Next
    
    ConnectionOpen = True
    
    If objConnection Is Nothing Then
        Set objConnection = CreateObject("ADOCE.Connection.3.1")
        objConnection.Open strDBFileName
        If objConnection.Errors.Count > 0 Then
            If boolMsgShow = True Then
                MsgBox strDBFileName & " DB 연결 실패!", vbOKOnly
                DisplayDBErrors
            End If
            ConnectionOpen = False
        End If
    End If
    
    On Error GoTo 0
End Function


' 설  명 : DB 연결끊기 서브함수
' 만든이 : 이준희(flyingmt@nate.com)
' 날  짜 : 2004년 03월 04일
Sub ConnectionClose()
    On Error Resume Next
    
    objConnection.Close
    Set objConnection = Nothing
    ConnectionClose = True
    
    On Error GoTo 0
End Sub


' 설  명 : DB 오류 출력 서브함수
' 만든이 : 이준희(flyingmt@nate.com)
' 날  짜 : 2004년 03월 04일
Sub DisplayDBErrors()
    Dim dispErr As String
    Dim arb1 As Integer
    Dim arb2 As Integer
    Dim ADOErr As ADOCE.Error

  'show connections errors
  For arb1 = 0 To objConnection.Errors.Count - 1
    Set ADOErr = objConnection.Errors(arb1)
    dispErr = "desc = " & ADOErr.Description & vbCrLf
    dispErr = dispErr & "number = " & Hex(ADOErr.Number) & vbCrLf
    dispErr = dispErr & "nativeerror = " & ADOErr.NativeError & vbCrLf
    dispErr = dispErr & "source = " & ADOErr.Source
    MsgBox dispErr, vbCritical, strTitleBar
  Next arb1
End Sub

' 설  명 : DB 존재 여부 확인 함수
' 만든이 : 이준희(flyingmt@nate.com)
' 날  짜 : 2004년 03월 04일
Function DBExists(strDBFileName As String, boolMsgShow As Boolean) As Boolean
  Dim MsgBoxReturnValue
  
  OpenFileSystem

  If objUsingFileSystem.Dir(strDBFileName) <> "" Then

      DBExists = True
  
      If boolMsgShow = True Then
          MsgBoxReturnValue = MsgBox(strDBFileName & "가 존재함", vbOKOnly, "데이터베이스 존재")
      End If
  
  Else

    DBExists = False
    
    If boolMsgShow = True Then
        MsgBoxReturnValue = MsgBox(strDBFileName & "가 존재하지 않음", vbOKOnly, "데이터베이스 존재")
    End If
    
  End If
  
  CloseFileSystem
  
End Function


' 설  명 : DB 생성 함수
' 만든이 : 이준희(flyingmt@nate.com)
' 날  짜 : 2004년 03월 04일
Function DBCreate(strDBFileName As String, boolOverWrite As Boolean, boolMsgShow As Boolean) As Boolean
    Dim MsgBoxReturnValue   ' 대화상자 반환값
    Dim DBRecordSet         ' DB 레코드셋
    
    ' DB 존재 여부 관련 루틴
    If DBExists(strDBFileName, boolMsgShow) = True Then
    
        If boolMsgShow = True Then
            MsgBoxReturnValue = MsgBox(strDBFileName & " DB를 삭제하고 새로 생성할까요?", vbYesNo, "데이터베이스 존재")
            
            If MsgBoxReturnValue = vbYes Then
                boolOverWrite = True
            Else
                boolOverWrite = False
            End If
        End If
        
        If boolOverWrite = True Then                ' DB 파일 삭제
            ConnectionClose                         ' 현재 DB 연결 청소
            
            OpenFileSystem
            objUsingFileSystem.Kill strDBFileName   ' DB 파일 삭제
            CloseFileSystem
        Else                                        ' DB 파일 삭제를 원하지 않을때는 함수 종료
            CreateDB = False
            Exit Function
        End If
        
    End If
        
    
    ' DB 생성
    On Error Resume Next
    Set DBRecordSet = CreateObject("ADOCE.Recordset.3.1")
    DBRecordSet.Open "CREATE DATABASE '" & strDBFileName & "'"
    DBRecordSet.Close
    Set DBRecordSet = Nothing
    On Error GoTo 0
    
    ' DB 생성 여부 확인
    If DBExists(strDBFileName, boolMsgShow) = True Then
        DBCreate = True
    Else
        DBCreate = False
    End If
End Function


' 설  명 : DB 삭제 함수
' 만든이 : 이준희(flyingmt@nate.com)
' 날  짜 : 2004년 03월 04일
Function DBDelete(strDBFileName As String, boolDelete As Boolean, boolMsgShow As Boolean) As Boolean
    Dim MsgBoxReturnValue   ' 대화상자 반환값
    Dim DBRecordSet         ' DB 레코드셋
    
    If DBExists(strDBFileName, boolMsgShow) = True Then
        If boolMsgShow = True Then
            MsgBoxReturnValue = MsgBox(strDBFileName & " DB를 삭제할까요?", vbYesNo, "데이터베이스 존재")
            
            If MsgBoxReturnValue = vbYes Then
                boolDelete = True
            Else
                boolDelete = False
            End If
        End If
            
        If boolDelete = True Then
            ConnectionClose                 ' 현재 DB 연결 청소
            
            ' DB 삭제 시작
            On Error Resume Next
            Set DBRecordSet = CreateObject("ADOCE.Recordset.3.1")
            DBRecordSet.Open "DROP DATABASE '" & strDBFileName & "'"
            Set DBRecordSet = Nothing ' DBRcordSet.Close 할필요 없다 (반환값이 없기 때문)
            On Error GoTo 0
        Else
            DBDelete = False
            Exit Function
        End If
    End If
    
    If DBExists(strDBFileName, boolMsgShow) = False Then
        DBDelete = True
    Else
        DBDelete = False
    End If
End Function


' 설  명 : DB에 SQL 실행 추가 함수
' 만든이 : 이준희(flyingmt@nate.com)
' 날  짜 : 2004년 03월 04일
Function DBExecuteSQL(strDBFileName As String, strSQL As String, strSuccess As String, strError As String, boolMsgShow As Boolean) As Boolean
    Dim boolConnctionOpen As Boolean ' 연결 반환 변수
    
    If DBExists(strDBFileName, boolMsgShow) = True Then
        boolConnctionOpen = ConnectionOpen(strDBFileName, boolMsgShow)
        
        If boolConnctionOpen = False Then
            DBExecuteSQL = False
            
            If boolMsgShow = True Then
                MsgBox strError, vbOKOnly, "SQL 실행"
            End If
            
            Exit Function
        Else
            On Error Resume Next
            objConnection.Execute (strSQL)
            On Error GoTo 0
            
            DBExecuteSQL = True
            ConnectionClose
            
            If boolMsgShow = True Then
                MsgBox strSuccess, vbOKOnly, "SQL 실행"
            End If
        End If
        
    End If

End Function


' 설  명 : DB에 TABLE 추가 함수
' 만든이 : 이준희(flyingmt@nate.com)
' 날  짜 : 2004년 03월 04일
' 인  수 : strTableField에서 구분자는 콤마이다. 규칙은 SQL 구문에 따른다.
Function DBTableCreate(strDBFileName As String, strTableName As String, strTableField As String, boolMsgShow As Boolean) As Boolean
    Dim strFullSQL As String
    strFullSQL = "CREATE table " & strTableName & " (" & strTableField & ")"
    
    DBTableCreate = DBExecuteSQL(strDBFileName, strFullSQL, strTableName & " 테이블 생성됨", strTableName & " 테이블 생성 실패", boolMsgShow)
End Function


' 설  명 : DB에 TABLE 삭제 함수
' 만든이 : 이준희(flyingmt@nate.com)
' 날  짜 : 2004년 03월 04일
' 인  수 : strTableField에서 구분자는 콤마이다. 규칙은 SQL 구문에 따른다.
Function DBTableDelete(strDBFileName As String, strTableName As String, boolMsgShow As Boolean) As Boolean
    Dim strFullSQL As String
    strFullSQL = "DROP table " & strTableName
    
    DBTableDelete = DBExecuteSQL(strDBFileName, strFullSQL, strTableName & " 테이블 삭제됨", strTableName & " 테이블 삭제 실패", boolMsgShow)
End Function



' 설  명 : DB에 RECORD 삽입 함수
' 만든이 : 이준희(flyingmt@nate.com)
' 날  짜 : 2004년 03월 04일
' 인  수 : strTableField에서 구분자는 콤마이다. 규칙은 SQL 구문에 따른다.
' 인  수 : strTableValue에서 구분자는 콤마이다. 규칙은 SQL 구문에 따른다.
Function DBRecordInsert(strDBFileName As String, strTableName As String, strTableField As String, strTableValue As String, boolMsgShow As Boolean) As Boolean
    Dim strFullSQL As String
    strFullSQL = "INSERT INTO " & strTableName & " (" & strTableField & ") VALUES (" & strTableValue & ")"

    DBRecordInsert = DBExecuteSQL(strDBFileName, strFullSQL, strTableName & " 테이블에서 " & strTableValue & "추가됨", strTableName & " 테이블에서 필드 추가 실패", boolMsgShow)
End Function


' 설  명 : DB에 RECORD 삭제 함수
' 만든이 : 이준희(flyingmt@nate.com)
' 날  짜 : 2004년 03월 04일
' 인  수 : strWhere는 조건 문자열이다. 규칙은 SQL 구문에 따른다.
Function DBRecordDelete(strDBFileName As String, strTableName As String, strWhere As String, boolMsgShow As Boolean) As Boolean
    Dim strFullSQL As String
    strFullSQL = "DELETE " & strTableName
    
    If strWhere <> "" Then
        strFullSQL = strFullSQL & " WHERE " & strWhere
    End If

    DBRecordDelete = DBExecuteSQL(strDBFileName, strFullSQL, strTableName & " 테이블에서 " & strWhere & "조건으로 삭제됨", strTableName & " 테이블에서 필드 삭제 실패", boolMsgShow)
End Function


' 설  명 : DB의 테이블을 Delimited 텍스트 파일로 저장하기
' 이  유 : Pocket Excel Automation이 안되기 때문이다.
' 만든이 : 이준희(flyingmt@nate.com)
' 날  짜 : 2004년 03월 04일
Function ExportDBTable(strExportFileName As String, strDBFileName As String, strTableName As String, boolOverWrite As Boolean, boolMsgShow As Boolean) As Boolean
    Dim MsgBoxReturnValue               ' 대화상자 반환값
    Dim DBRecordSet As ADOCE.Recordset  ' DB 레코드셋
    Dim DBRecordNum As Integer          ' 총 레이코드 개수
    Dim strBuffer As String             ' 임시 문자열 변수
    Dim IndexForRecord As Integer       ' 레코드 항목 추출시 사용되는 변수 (For문)
    Dim objUsingFile As File            ' 파일쓰기를 위한 객체
    
    ' DB 존재 여부 확인
    If DBExists(strDBFileName, boolMsgShow) = False Then
        If boolMsgShow = True Then
            MsgBox strDBFileName & " DB가 존재하지 않음", vbOKOnly, "데이터베이스"
        End If
    
        ExportDBTable = False
        Exit Function
    End If
    
        
    ' 저장 파일 존재 여부 확인
    If DBExists(strExportFileName, boolMsgShow) = True Then
        If boolMsgShow = True Then
            MsgBoxReturnValue = MsgBox(strExportFileName & " 파일을 삭제하고 새로 생성할까요?", vbYesNo, "파일 존재")
            
            If MsgBoxReturnValue = vbYes Then
                boolOverWrite = True
            Else
                boolOverWrite = False
            End If
        End If
        
        If boolOverWrite = True Then                    ' DB 파일 삭제
            OpenFileSystem
            objUsingFileSystem.Kill strExportFileName   ' DB 파일 삭제
            CloseFileSystem
        Else                                            ' DB 파일 삭제를 원하지 않을때는 함수 종료
            If boolMsgShow = True Then
                MsgBox strExportFileName & " 파일이 존재함", vbOKOnly
            End If
            
            ExportDBTable = False
            Exit Function
        End If
    End If
    
    ' DB 연결하고 지정 테이블 가져오기
    If ConnectionOpen(strDBFileName, boolMsgShow) = True Then
        
        ' 파일 초기화 & 열기
        Set objUsingFile = CreateObject("FILECTL.File.1")
        objUsingFile.Open strExportFileName, fsModeOutput
        
        Set DBRecordSet = CreateObject("ADOCE.Recordset.3.1")
        On Error Resume Next
        
        DBRecordSet.Open "SELECT * FROM " & strTableName, objConnection, adOpenForwardOnly, adLockReadOnly
        
        Do While Not DBRecordSet.EOF
            strBuffer = ""
            For IndexForRecord = 0 To DBRecordSet.Fields.Count
                If IndexForRecord = DBRecordSet.Fields.Count - 1 Then
                    strBuffer = strBuffer & DBRecordSet.Fields(IndexForRecord).Value
                Else
                    strBuffer = strBuffer & DBRecordSet.Fields(IndexForRecord).Value & Chr(9)
                End If
            Next
        
            objUsingFile.LinePrint strBuffer
            
            DBRecordSet.MoveNext
        Loop
        DBRecordNum = DBRecordSet.RecordCount
        DBRecordSet.Close
        Set DBRecordSet = Nothing
            If boolMsgShow = True Then
            MsgBox "총 " & DBRecordNum & " 항목을 기록했습니다.", vbOKOnly, "추출 결과"
        End If
        
        On Error GoTo 0
        
        ' 파일 닫기
        objUsingFile.Close
        Set objUsingFile = Nothing
    End If
    
    ConnectionClose
    
End Function

크리에이티브 커먼즈 라이센스
Creative Commons License
이올린에 북마크하기
2004/03/04 17:15 2004/03/04 17:15
Posted by 나는산.

Leave your greetings here.

[로그인][오픈아이디란?]
출처: http://www.zog.co.kr/?no=8

자세하게 설명해서 올렸습니다..

http://www.zog.co.kr/lab/?no=37 에서 보세요.1

트랙백(TRACKBACK)이 이런건지 몰랐네요.
쉽게 설명해주셔서 감사합니다.
크리에이티브 커먼즈 라이센스
Creative Commons License
이올린에 북마크하기(0) 이올린에 추천하기(0)
2004/03/03 17:05 2004/03/03 17:05
Posted by 나는산.

Leave your greetings here.

[로그인][오픈아이디란?]

바쁜 생활 속에서도 난 계속 살아간다.


해야할것이 너무나도 많은 세상인거같다. 살도 빼야하고, 회사 일도 해야하고, 공부도 해야하고, 청춘사업도 해야하고, 시대도 따라가야 한다.

크리에이티브 커먼즈 라이센스
Creative Commons License
이올린에 북마크하기(0) 이올린에 추천하기(0)
2004/03/03 16:45 2004/03/03 16:45
Posted by 나는산.

Leave your greetings here.

  1. Comment RSS : http://lovehana.com/rss/comment/62
  2. 동훈형이 2005/10/05 09:26  Modify/Delete  Reply  Address

    정말 바쁘구나 방법은 하나하나 마무리해 나가는 것 그렇지 않으면 늘 일은 남아있게 마련이다. 결혼부터 해라. 많은 것이 정리된단다

[로그인][오픈아이디란?]
« Previous : 1 : ... 180 : 181 : 182 : 183 : 184 : Next »