컴퓨터
ASP나 VB에서 ADO,DAO,RDO를 위한 MySQL연결하기
홈피를 만들거나 프밍을 하는 사람이아면 기본적으로 알아야 할 내용이지요.
워낙 많이 쓰는 거라 필요할 때 찾기 귀찮죠.
MS SQL이나 Oracle DB나 사용법에는 차이가 없습니다. 단지 드라이버 선택이 다를 뿐입니다
VB 6 나 ASP 로 MySQL Database를 연결해서 사용할 때는 먼저 MyODBC 3.51.06 다운 받아야 한다. 주소는 http://www.mysql.com/downloads/api-myodbc-3.51.html
'**************************************
' ADO 사용하기
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim fld As ADODB.Field
Dim sql As String
'connect to MySQL server using MySQL ODBC 3.51 Driver
Set conn = New ADODB.Connection
conn.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost; DATABASE=test; UID=venu; PWD=venu; OPTION=3"
conn.Open
'table 생성하기
conn.Execute "DROP TABLE if EXISTS my_ado"
conn.Execute "CREATE TABLE my_ado(id int Not null primary key, name varchar(20)," _
& "txt text, dt date, tm time, ts timestamp)"
'direct insert
conn.Execute "INSERT INTO my_ado(id,name,txt) values(1,100,'venu')"
conn.Execute "INSERT INTO my_ado(id,name,txt) values(2,200,'MySQL')"
conn.Execute "INSERT INTO my_ado(id,name,txt) values(3,300,'Delete')"
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseServer
'fetch the initial table ..
rs.Open "SELECT * FROM my_ado", conn
Debug.Print rs.RecordCount
rs.MoveFirst
Debug.Print String(50, "-") & "Initial my_ado Result Set " & String(50, "-")
For Each fld In rs.Fields
Debug.Print fld.Name,
Next
Debug.Print
Do Until rs.EOF
For Each fld In rs.Fields
Debug.Print fld.Value,
Next
rs.MoveNext
Debug.Print
Loop
rs.Close
'rs insert(레코드삽입)
rs.Open "select * from my_ado", conn, adOpenDynamic, adLockOptimistic
rs.AddNew
rs!Name = "Monty"
rs!txt = "Insert row"
rs.Update
rs.Close
'rs update(레코드 업데이트)
rs.Open "SELECT * FROM my_ado"
rs!Name = "update"
rs!txt = "updated-row"
rs.Update
rs.Close
'rs update second time..
rs.Open "SELECT * FROM my_ado"
rs!Name = "update"
rs!txt = "updated-second-time"
rs.Update
rs.Close
'rs delete
rs.Open "SELECT * FROM my_ado"
rs.MoveNext
rs.MoveNext
rs.Delete
rs.Close
'업데이트 체이블 검사하기
rs.Open "SELECT * FROM my_ado", conn
Debug.Print rs.RecordCount
rs.MoveFirst
Debug.Print String(50, "-") & "Updated my_ado Result Set " & String(50, "-")
For Each fld In rs.Fields
Debug.Print fld.Name,
Next
Debug.Print
Do Until rs.EOF
For Each fld In rs.Fields
Debug.Print fld.Value,
Next
rs.MoveNext
Debug.Print
Loop
rs.Close
conn.Close
'*******************************************************
' DAO 사용하기
Dim ws As Workspace
Dim conn As Connection
Dim queryDef As queryDef
Dim str As String
'MySQL ODBC 3.51를 사용하여 MySQL에 연결하기
' Driver
Set ws = DBEngine.CreateWorkspace("", "venu", "venu", dbUseODBC)
str = "odbc;DRIVER={MySQL ODBC 3.51 Driver};"_
& "SERVER=localhost;"_
& " DATABASE=test;"_
& "UID=venu;PWD=venu; OPTION=3"
Set conn = ws.OpenConnection("test", dbDriverNoPrompt, False, str)
'Create table my_dao
Set queryDef = conn.CreateQueryDef("", "drop table if exists my_dao")
queryDef.Execute
Set queryDef = conn.CreateQueryDef("", "create table my_dao(Id INT AUTO_INCREMENT PRIMARY KEY, " _
& "Ts TIMESTAMP(14) Not NULL, Name varchar(20), Id2 INT)")
queryDef.Execute
'Insert new records using rs.addNew
Set rs = conn.OpenRecordset("my_dao")
Dim i As Integer
For i = 10 To 15
rs.AddNew
rs!Name = "insert record" & i
rs!Id2 = i
rs.Update
Next i
rs.Close
'rs update..
Set rs = conn.OpenRecordset("my_dao")
rs.Edit
rs!Name = "updated-string"
rs.Update
rs.Close
'fetch the table back...
Set rs = conn.OpenRecordset("my_dao", dbOpenDynamic)
str = "Results:"
rs.MoveFirst
While Not rs.EOF
str = " " & rs!Id & " , " & rs!Name & ", " & rs!Ts & ", " & rs!Id2
Debug.Print "DATA:" & str
rs.MoveNext
Wend
'rs Scrolling(레코드 이동)
rs.MoveFirst
str = " FIRST ROW: " & rs!Id & " , " & rs!Name & ", " & rs!Ts & ", " & rs!Id2
Debug.Print str
rs.MoveLast
str = " LAST ROW: " & rs!Id & " , " & rs!Name & ", " & rs!Ts & ", " & rs!Id2
Debug.Print str
rs.MovePrevious
str = " LAST-1 ROW: " & rs!Id & " , " & rs!Name & ", " & rs!Ts & ", " & rs!Id2
Debug.Print str
'free all resources
rs.Close
queryDef.Close
conn.Close
ws.Close
'*****************************************
' RDO사용하기
Dim rs As rdoResultset
Dim cn As New rdoConnection
Dim cl As rdoColumn
Dim SQL As String
'cn.Connect = "DSN=test;"
cn.Connect = "DRIVER={MySQL ODBC 3.51 Driver};"_
& "SERVER=localhost;"_
& " DATABASE=test;"_
& "UID=venu;PWD=venu; OPTION=3"
cn.CursorDriver = rdUseOdbc
cn.EstablishConnection rdDriverPrompt
'drop table my_rdo
SQL = "drop table if exists my_rdo"
cn.Execute SQL, rdExecDirect
'create table my_rdo
SQL = "create table my_rdo(id int, name varchar(20))"
cn.Execute SQL, rdExecDirect
'insert - direct
SQL = "insert into my_rdo values (100,'venu')"
cn.Execute SQL, rdExecDirect
SQL = "insert into my_rdo values (200,'MySQL')"
cn.Execute SQL, rdExecDirect
'rs insert
SQL = "select * from my_rdo"
Set rs = cn.OpenResultset(SQL, rdOpenStatic, rdConcurRowVer, rdExecDirect)
rs.AddNew
rs!id = 300
rs!Name = "Insert1"
rs.Update
rs.Close
'rs insert
SQL = "select * from my_rdo"
Set rs = cn.OpenResultset(SQL, rdOpenStatic, rdConcurRowVer, rdExecDirect)
rs.AddNew
rs!id = 400
rs!Name = "Insert 2"
rs.Update
rs.Close
'rs update
SQL = "select * from my_rdo"
Set rs = cn.OpenResultset(SQL, rdOpenStatic, rdConcurRowVer, rdExecDirect)
rs.Edit
rs!id = 999
rs!Name = "updated"
rs.Update
rs.Close
'fetch back...
SQL = "select * from my_rdo"
Set rs = cn.OpenResultset(SQL, rdOpenStatic, rdConcurRowVer, rdExecDirect)
Do Until rs.EOF
For Each cl In rs.rdoColumns
Debug.Print cl.Value,
Next
rs.MoveNext
Debug.Print
Loop
Debug.Print "Row count="; rs.RowCount
'close
rs.Close
cn.Close
번호 | 제목 | 글쓴이 | 조회 | 날짜 |
---|---|---|---|---|
>> | [컴퓨터] ASP나 VB에서 ADO,DAO,RDO를 위한 MySQL연결하기 | 8175 | 17564 | 2004.01.25 19:26 |
94 | [정보사회와 컴퓨터] 교실수업자료활용및제작 | 김종판 | 17399 | 2007.03.06 11:26 |
93 | [컴퓨터] 퀴즈제작 강좌[2강] | 김종판 | 16694 | 2007.11.09 11:10 |
92 | [정보사회와 컴퓨터] 파워포인트강좌,스위시맥스강좌 | 김종판 | 16640 | 2007.02.27 14:53 |
91 | [컴퓨터] 500개 템플릿-학습자료,평가문항 exe,html,cd로 만들기 | 교육자 | 16550 | 2008.08.23 00:04 |
90 | [정보사회와 컴퓨터] 플래시FLV동영상및소스 [다운로드] | 김종판 | 16498 | 2008.01.06 13:02 |
89 | [정보사회와 컴퓨터] 학습설계 구성강좌,제작사례 | 에듀맨 | 16378 | 2008.04.02 11:56 |
88 | [컴퓨터] 엑셀 3,000 문제 렉토라에서 사용하기 | 교육자 | 16350 | 2008.10.07 12:33 |
87 | [정보사회와 컴퓨터] 6개 swf파일 한번에 - 페이지에 첨부하는 팁 | 이상봉 | 16047 | 2008.02.28 11:24 |
86 | [컴퓨터] 하이퍼링크로 학급 앨범 완성하기 | 교컴지기 | 15978 | 2008.09.08 16:14 |
85 | [컴퓨터] VNC(원격제어 프로그램) 활용 자료 [3] | 정병희 | 15308 | 2007.11.13 09:18 |
84 | [컴퓨터] 렉토라를 이용한 교수-학습 자료 제작[경북]-사용법 강좌포함 | 이상봉 | 15213 | 2007.11.19 11:56 |
83 | [컴퓨터] Advocates promote computer-science educa | 교컴지기 | 15203 | 2010.07.17 08:06 |
82 | [정보사회와 컴퓨터] ucc 동영상 목록만들기,제어하기 | 이상봉 | 15137 | 2007.11.28 16:06 |
81 | [정보사회와 컴퓨터] 퀴즈제작 강좌,교재,사례,다운로드 | 김종판 | 15057 | 2007.11.09 11:15 |
80 | [컴퓨터] 립씽크 프로그램- 동기유발자료제작 사용법강좌 [1] | 교육자 | 14944 | 2008.05.08 14:53 |
79 | [정보사회와 컴퓨터] 통합멀티미디어 UCC 자료제작[첨부] [1] | 이상봉 | 14799 | 2007.12.03 12:33 |
78 | [컴퓨터] 퀴즈제작 강좌[1강] | 김종판 | 14207 | 2007.11.09 11:05 |
77 | [정보사회와 컴퓨터] 렉토라에서 만든 FLV 플래시 동영상,사운드 | 김종판 | 13563 | 2007.12.28 19:30 |
76 | [정보사회와 컴퓨터] 자유학기제 sw 관련 교육 [삼성전자 주니어 소프트웨어 아카데미] | 김수연 | 10831 | 2018.12.28 14:24 |