Global It Leader!!



 
 

VB 비주얼베이직에서 MySQL쓰기 초간단 예제

페이지 정보

작성자 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 댓글 0건 조회 4,661회 작성일 13-07-26 00:26

본문

비주얼베이직에서 MySQL쓰기 초간단 예제
이 소스를 돌리기위해서는 사전에 DSN을 설정해야된다.

DSN설정은 밑의 링크를 참조할 것
http://wwwi.tistory.com/74  MySQL ODBC설정하기

ADO를 쓰기위해서는 프로젝트에서 참조추가를 선택하여 COM 탭에서
Microsoft ActiveX Data Object 2.8 Library를 더블클릭으로 선택해야 된다.

밑의 소스는 테이블을 추가하고 데이터 추가 삭제 변경에 대한 예제이다.
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim AdoCn As ADODB.Connection
        Dim AdoRs As ADODB.Recordset
        Dim fldWork As ADODB.Field
        Dim sSql As String
        Dim sWork As String

        ' MySQL에 접속 [MySQL ODBC 3.51 Driver]
        AdoCn = New ADODB.Connection
        AdoCn.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};" _
                      & "SERVER = localhost;" _
                      & "DATABASE = memo;" _
                      & "UID = muser; PWD = pass; OPTION = 3"

        ' Database open
        AdoCn.Open()

        ' 테이블 만들기
        sSql = "create table tmemo ( "
        sSql = sSql & " memoID int8, "
        sSql = sSql & " fuser char(20), "
        sSql = sSql & " fmemo char(255) "
        sSql = sSql & " ) type = InnoDB; "

        AdoCn.Execute(sSql)

        ' 데이터 추가
        sSql = "INSERT INTO tmemo(memoID, fuser, fmemo) values(1,'김기사','5시까지와~')"
        AdoCn.Execute(sSql)
        sSql = "INSERT INTO tmemo(memoID, fuser, fmemo) values(2,'이사감','나 낙하산')"
        AdoCn.Execute(sSql)
        sSql = "INSERT INTO tmemo(memoID, fuser, fmemo) values(3,'박장난','말장난하냐')"
        AdoCn.Execute(sSql)

        AdoRs = New ADODB.Recordset
        AdoRs.CursorLocation = ADODB.CursorLocationEnum.adUseServer

        ' 레코드셋 설정
        AdoRs.Open("SELECT * FROM tmemo", AdoCn)

        Debug.Print("tmemo 테이블의 레코드수 :" & AdoRs.RecordCount)

        AdoRs.MoveFirst()

        sWork = ""

        ' tmemo 테이블의 필드명 출력
        For Each fldWork In AdoRs.Fields
            sWork = sWork & fldWork.Name & vbTab
        Next
        Debug.Print(sWork)

        ' tmemo 테이블의 필드 데이터 출력
        Do Until AdoRs.EOF

            sWork = ""
            For Each fldWork In AdoRs.Fields
                sWork = sWork & fldWork.Value & vbTab
            Next
            Debug.Print(sWork)

            AdoRs.MoveNext()
        Loop

        AdoRs.Close()

        ' 레코드셋 설정
        AdoRs.Open("select * from tmemo", AdoCn, _
                    ADODB.CursorTypeEnum.adOpenDynamic, _
                    ADODB.LockTypeEnum.adLockOptimistic)

        ' 데이터 추가
        AdoRs.AddNew()
        AdoRs.Fields(0).Value = 4
        AdoRs.Fields(1).Value = "최저가"
        AdoRs.Fields(2).Value = "어디가 제일싸?"
        AdoRs.Update()

        AdoRs.AddNew()
        AdoRs.Fields(0).Value = 5
        AdoRs.Fields(1).Value = "지천명"
        AdoRs.Fields(2).Value = "일신우일신"
        AdoRs.Update()

        AdoRs.Close()


        ' 레코드셋 설정
        AdoRs.Open("select * from tmemo where memoID = 3", AdoCn, _
                    ADODB.CursorTypeEnum.adOpenDynamic, _
                    ADODB.LockTypeEnum.adLockOptimistic)

        ' 데이터 변경
        AdoRs.Fields(1).Value = "박농담"
        AdoRs.Fields(2).Value = "농담만하니?"
        AdoRs.Update()
        AdoRs.Close()


        ' 레코드셋 설정
        AdoRs.Open("select * from tmemo where memoID = 5", AdoCn, _
                    ADODB.CursorTypeEnum.adOpenDynamic, _
                    ADODB.LockTypeEnum.adLockOptimistic)

        ' 데이터 삭제
        AdoRs.Delete()
        AdoRs.Close()


        ' 레코드셋 설정
        AdoRs.Open("SELECT * FROM tmemo", AdoCn)

        Debug.Print("tmemo 테이블의 레코드수 :" & AdoRs.RecordCount)

        AdoRs.MoveFirst()

        sWork = ""

        ' tmemo 테이블의 필드명 출력
        For Each fldWork In AdoRs.Fields
            sWork = sWork & fldWork.Name & vbTab
        Next
        Debug.Print(sWork)

        ' tmemo 테이블의 필드 데이터 출력
        Do Until AdoRs.EOF

            sWork = ""
            For Each fldWork In AdoRs.Fields
                sWork = sWork & fldWork.Value & vbTab
            Next
            Debug.Print(sWork)

            AdoRs.MoveNext()
        Loop

        AdoRs.Close()

        AdoCn.Close()

    End Sub



프로그램을 돌리면 아래와 같은 결과가 나온다.
레코드수를 제대로 못가져온다. -.-;; 왜그런거지?
tmemo 테이블의 레코드수 :-1
memoID fuser fmemo
1 김기사 5시까지와~
2 이사감 나 낙하산
3 박장난 말장난하냐
tmemo 테이블의 레코드수 :-1
memoID fuser fmemo
1 김기사 5시까지와~
2 이사감 나 낙하산
3 박농담 농담만하니?
4 최저가 어디가 제일싸?
 

댓글목록

등록된 댓글이 없습니다.

전체 95
게시물 검색
컴퓨터언어 목록
번호 제목 글쓴이 조회 날짜
55 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 6322 12-16
54 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4449 11-14
53 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 8855 11-05
52 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4623 10-24
51 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 7111 10-24
열람중 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4662 07-26
49 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4743 03-09
48 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4910 03-09
47 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 10842 06-30
46 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 6707 06-25
45 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 7468 06-25
44 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 8032 06-25
43 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 13003 06-24
42 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 8538 06-24
41 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 9230 06-17
40 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 24953 07-22
39 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 9921 07-21
38 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 8410 07-21
37 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 8724 07-21
36 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 8112 07-20