Global It Leader!!



 
 

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

페이지 정보

작성자 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 댓글 0건 조회 4,662회 작성일 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 최저가 어디가 제일싸?
 

댓글목록

등록된 댓글이 없습니다.

전체 440
게시물 검색
컴퓨터언어 목록
번호 제목 글쓴이 조회 날짜
240 그누보드스킨 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4495 08-20
239 CSS no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4839 08-19
238 Mysql no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4997 07-29
열람중 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4663 07-26
236 그누보드스킨 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5018 06-24
235 Mysql no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4606 06-20
234 Javasript no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4656 05-20
233 PHP no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4841 04-11
232 Javasript no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4399 04-11
231 Mysql no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5145 03-28
230 그누보드스킨 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4961 03-27
229 그누보드스킨 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4550 03-26
228 Javasript no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4537 03-20
227 그누보드스킨 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4930 03-20
226 그누보드스킨 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4472 03-13
225 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4744 03-09
224 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4911 03-09
223 CSS no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4619 03-08
222 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 10843 06-30
221 VB no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 6708 06-25