컴퓨터
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
번호 | 제목 | 글쓴이 | 조회 | 날짜 |
---|---|---|---|---|
55 | [컴퓨터] [vb60]쇼핑몰지원 원격 툴소스 | 8175 | 5465 | 2004.08.19 17:13 |
54 | [컴퓨터] [ASP]새 교컴 위지윅게시판과 모양이 똑같은 ASP용소스 | 8175 | 7070 | 2004.08.05 23:05 |
53 | [컴퓨터] WEB 3D입니다 구경오세요 | 김정식 | 5474 | 2004.02.11 19:46 |
52 | [컴퓨터] 안녕하세요 컴퓨터교사모임이에요. | 김미영 | 7129 | 2004.02.10 11:48 |
>> | [컴퓨터] ASP나 VB에서 ADO,DAO,RDO를 위한 MySQL연결하기 | 8175 | 17519 | 2004.01.25 19:26 |
50 | [컴퓨터] [html help workshop]물 간 건데 올려도 되나? | 8175 | 5648 | 2003.12.30 22:46 |
49 | [컴퓨터] 2002 전세계IP할당표 | 8175 | 5906 | 2003.12.02 14:49 |
48 | [컴퓨터] 여러종류의 플래시을 CD및 웹으로 제작방법[강좌] | 김용희 | 5350 | 2003.11.27 12:58 |
47 | [컴퓨터] [vb60]작은 메신저 소스 | 8175 | 5548 | 2003.11.24 20:39 |
46 | [컴퓨터] 오덕초등학교-동영상편집(10) | 김용희 | 5419 | 2003.10.21 01:41 |
45 | [컴퓨터] 오덕초등학교-프로듀셔 강좌(9) | 김용희 | 4886 | 2003.10.18 09:58 |
44 | [컴퓨터] 오덕초등학교 - 광고제작방법(8) | 김용희 | 5456 | 2003.10.17 01:35 |
43 | [컴퓨터] 오덕초등학교 - 동영상강좌(7) | 김용희 | 5403 | 2003.10.15 09:42 |
42 | [컴퓨터] 컴퓨터 청소 방법입니당 | 주영일 | 7996 | 2003.10.10 12:37 |
41 | [컴퓨터] 오덕초등학교 - 동영상강좌(4) | 김용희 | 5253 | 2003.10.09 23:58 |
40 | [컴퓨터] 오덕초등학교-동영상강좌(3) | 김용희 | 5212 | 2003.10.08 02:03 |
39 | [컴퓨터] 오덕초등학교 - 동영상강좌(2) | 김용희 | 4900 | 2003.10.07 02:14 |
38 | [컴퓨터] 오덕초등학교 - 동영상강좌(1) | 김용희 | 5422 | 2003.10.05 16:57 |
37 | [컴퓨터] SnagIt을 이용한 영화 스틸 사진 만들기 | 김용희 | 5677 | 2003.10.01 19:29 |
36 | [컴퓨터] Windows media player 9 [최신버젼으로 patch] | 양석윤 | 5539 | 2003.09.07 00:43 |