SlideShare a Scribd company logo
1 of 8
Download to read offline
Connect to an Oracle Database from Visual Basic 6
Connect to an Oracle Database from
Visual Basic 6 (Part 2)
Preface
This is Part 2 in a 2 part series on using Visual Basic 6 to connect to an Oracle
database. In Part 1, I showed you how to use an ADO Data Control to make the
connection. In this article, I show you how to use ADO Objects to make the
connection. Not only will the article be useful for the Oracle programmers in the
audience, it will also show you how to use ADO objects in your program.
ADO Objects
What's an Object? Hopefully you've read by well-received book, Learn to
Program with Visual Basic 6 Objects. As much as I would love to spend a lot of
time discussing object variables in detail, I'm going to presume that you know
what they are and how, in a nutshell, they refer to an object instantiated from a
class or template.
Having said that, I will tell you that the ADO Object model can be confusing,
particularly because there are frequently many ways to achieve the same
functionality.
In this article, we will create an ADO Connection Object, then use an ADO
Command Object to return the results of a query to an ADO Recordset object.
Finally, as an added bonus, I'll show you something that your average
programmer doesn't know can be done--we'll set the Recordsource of an ADO
DataGrid to the recordset we produce in code. In other words, without the ADO
Data Control, we'll populate a DataGrid.
Similar to what we did when we added the ADO Data Control to our Visual Basic
Toolbox, in order to use ADO Objects we must first set a reference to the ADO
Object Library. The version of the ADO Object Library installed on your PC will
vary depending upon a number of factors-the PC I'm using as I write this article
contains ADO Version 2.x.
To add a reference, select Project-References from the Visual Basic Menu Bar,
then select Microsoft ActiveX Data Objects 2.6 Library. If the version number
doesn't match yours exactly, that's fine---just be sure to select the ActiveX Data
Objects Library, not the ActiveX Data Objects Recordset Library that follows it in
the list. Be sure that you select Project-References, NOT Project-Components.
Object libraries have no visible interface, and so they won't appear in the
http://www.johnsmiley.com/cis18.notfree/Smiley004/Smiley004.htm (1 of 8)3/28/2004 11:35:52 AM
Connect to an Oracle Database from Visual Basic 6
Components section.
When you click on the OK button, nothing obvious happens--but you now have
access to ADO Objects in your program.
Before we get too far, let's start the process of adding the ADO DataGrid Control
to our form. In order to do that, we need to select Project-Components (yes,
that's right, Components this time) from the Visual Basic Menu Bar, and select
Microsoft DataGrid Control 6.0.
http://www.johnsmiley.com/cis18.notfree/Smiley004/Smiley004.htm (2 of 8)3/28/2004 11:35:52 AM
Connect to an Oracle Database from Visual Basic 6
When you click on the OK button, the DataGrid Control (the ADO version) is
added to the Visual Basic Toolbox.
http://www.johnsmiley.com/cis18.notfree/Smiley004/Smiley004.htm (3 of 8)3/28/2004 11:35:52 AM
Connect to an Oracle Database from Visual Basic 6
With the DataGrid control in your Toolbox, now it's time to add it to your form...
http://www.johnsmiley.com/cis18.notfree/Smiley004/Smiley004.htm (4 of 8)3/28/2004 11:35:52 AM
Connect to an Oracle Database from Visual Basic 6
Working with ADO Objects to achieve your
Oracle Connection
The Connection Property
It was at this point in Part 1 of this article that we adjusted properties of the ADO
Data Control to achieve a Connection to an Oracle Database, and to build a
Recordset which was then used to populate the ADO DataGrid. Here, we'll use
an ADO Connection Object and a Recordset Object to achieve the same
functionality. For demonstration purposes, let's place this code in the Load Event
Procedure of the Form...
Dim oconn As New ADODB.Connection
http://www.johnsmiley.com/cis18.notfree/Smiley004/Smiley004.htm (5 of 8)3/28/2004 11:35:52 AM
Connect to an Oracle Database from Visual Basic 6
Dim rs As New ADODB.Recordset
Dim strSQL As String
strSQL = "SELECT * FROM EMPLOYEES"
Set oconn = New ADODB.Connection
oconn.Open "Provider=msdaora;Data Source=John.world;User Id=jsmiley;
Password=neveruseyourdogsnameasyourpassword;"
rs.CursorType = adOpenStatic
rs.CursorLocation = adUseClient
rs.LockType = adLockOptimistic
rs.Open strSQL, oconn, , , adCmdText
Set DataGrid1.DataSource = rs
What's going on?
Let me explain. These first two lines of code declare 2 object variables. The first
is an ADO Connection Object
Dim oconn As New ADODB.Connection
followed by an ADO Recordset Object
Dim rs As New ADODB.Recordset
Both of these declarations are necessary in order to build a Recordset--which is
a virtual (in memory) representation of an actual Oracle table in our Database.
The names for these two object variables are pretty standard, and you see them
all the time in code and in books and articles. In fact, if you search for oconn or
rs using a Google search, you'll gets lots of results for ADO.
The final variable declaration is a String variable that we will use to 'hold' the
SQL statement used to build our recordset.
Dim strSQL As String
Once we've declared the strSQL variable, it's time to assign a SQL statement to
it. You may remember this statement from Part 1 of this article--it's used to
retrieve every record from the Employees table in my Oracle database...
strSQL = "SELECT * FROM EMPLOYEES"
Our next step is to open our ADO Connection. We do that by executing the
http://www.johnsmiley.com/cis18.notfree/Smiley004/Smiley004.htm (6 of 8)3/28/2004 11:35:52 AM
Connect to an Oracle Database from Visual Basic 6
Open method of our Connection Object. The Open method is looking for four
parameters: the Provider name, the Data Source (or HostName), the User ID
and the Password of the database in question.
oconn.Open "Provider=msdaora;Data Source=John.world;User Id=jsmiley;
Password=neveruseyourdogsnameasyourpassword;"
Do the details of the Open Method look familiar? They should. In Part 1 I told
you that the Connection String that the ADO Data Control wizard built for us
could be used to open a Connection in code. In fact, some programmers use the
ADO Data Control wizard to build their Connection Strings for them, and then
copy and paste them into the code window.
Before we build the Recordset object (not the same as the Connection object)
we need to adjust three properties of the Recordset object, the CursorType,
CursorLocation and LockType. If you want to know more about the details of the
Recordset object, I highly recommend Wrox's ADO 2.6 Programmer's Guide.
rs.CursorType = adOpenStatic
rs.CursorLocation = adUseClient
rs.LockType = adLockOptimistic
Now it's time to open the Recordset. If you are confused about the difference
between a Connection and a Recordset, think of the Connection as the
Database window you see when you first start Access. The Database contains a
window with a list of tables that you can then select. When you select one, a
Data Window opens up with just the information from that table. This is the
equivalent of the Recordset.
rs.Open strSQL, oconn, , , adCmdText
In theory, we now have a Recordset object built containing all of the data in the
Employees table of my Oracle database. How do we see the Recordset?
We can use the Set statement to assign the Recordset object to the DataSource
property of our DataGrid.
Set DataGrid1.DataSource = rs
If we now run the program, the code in the Load Event procedure of the form
executes. A Connection object is created, initiating the Connection to my Oracle
Database. Then a Recordset object is created, retrieving Employee records.
Finally, the DataSource property of the DataGrid is set to point to the Recordset
object. Here's our form after the code executes--as you can see, the DataGrid
is now populated.
http://www.johnsmiley.com/cis18.notfree/Smiley004/Smiley004.htm (7 of 8)3/28/2004 11:35:52 AM
Connect to an Oracle Database from Visual Basic 6
Summary
I hope you've enjoyed this article.
http://www.johnsmiley.com/cis18.notfree/Smiley004/Smiley004.htm (8 of 8)3/28/2004 11:35:52 AM

More Related Content

What's hot

Ado.Net Architecture
Ado.Net ArchitectureAdo.Net Architecture
Ado.Net ArchitectureUmar Farooq
 
ADO .NET by Sonu Vishwakarma
ADO .NET by Sonu VishwakarmaADO .NET by Sonu Vishwakarma
ADO .NET by Sonu VishwakarmaSonu Vishwakarma
 
ASP.NET Session 11 12
ASP.NET Session 11 12ASP.NET Session 11 12
ASP.NET Session 11 12Sisir Ghosh
 
Ch06 ado.net fundamentals
Ch06 ado.net fundamentalsCh06 ado.net fundamentals
Ch06 ado.net fundamentalsMadhuri Kavade
 
Share point 2013 coding standards and best practices 1.0
Share point 2013 coding standards and best practices 1.0Share point 2013 coding standards and best practices 1.0
Share point 2013 coding standards and best practices 1.0LiquidHub
 
Data Binding and Data Grid View Classes
Data Binding and Data Grid View ClassesData Binding and Data Grid View Classes
Data Binding and Data Grid View ClassesArvind Krishnaa
 
ASP.NET 08 - Data Binding And Representation
ASP.NET 08 - Data Binding And RepresentationASP.NET 08 - Data Binding And Representation
ASP.NET 08 - Data Binding And RepresentationRandy Connolly
 
Slick: Bringing Scala’s Powerful Features to Your Database Access
Slick: Bringing Scala’s Powerful Features to Your Database Access Slick: Bringing Scala’s Powerful Features to Your Database Access
Slick: Bringing Scala’s Powerful Features to Your Database Access Rebecca Grenier
 
Accessing data with android cursors
Accessing data with android cursorsAccessing data with android cursors
Accessing data with android cursorsinfo_zybotech
 

What's hot (20)

ADO.NET
ADO.NETADO.NET
ADO.NET
 
Ado.net
Ado.netAdo.net
Ado.net
 
ADO .Net
ADO .Net ADO .Net
ADO .Net
 
Ado.net
Ado.netAdo.net
Ado.net
 
Ado object
Ado objectAdo object
Ado object
 
Ado.Net Architecture
Ado.Net ArchitectureAdo.Net Architecture
Ado.Net Architecture
 
ADO.NET
ADO.NETADO.NET
ADO.NET
 
ADO .NET by Sonu Vishwakarma
ADO .NET by Sonu VishwakarmaADO .NET by Sonu Vishwakarma
ADO .NET by Sonu Vishwakarma
 
ASP.NET Session 11 12
ASP.NET Session 11 12ASP.NET Session 11 12
ASP.NET Session 11 12
 
Ch06 ado.net fundamentals
Ch06 ado.net fundamentalsCh06 ado.net fundamentals
Ch06 ado.net fundamentals
 
Ado.Net Tutorial
Ado.Net TutorialAdo.Net Tutorial
Ado.Net Tutorial
 
5.C#
5.C#5.C#
5.C#
 
For Beginers - ADO.Net
For Beginers - ADO.NetFor Beginers - ADO.Net
For Beginers - ADO.Net
 
Share point 2013 coding standards and best practices 1.0
Share point 2013 coding standards and best practices 1.0Share point 2013 coding standards and best practices 1.0
Share point 2013 coding standards and best practices 1.0
 
Hibernate
HibernateHibernate
Hibernate
 
Data Binding and Data Grid View Classes
Data Binding and Data Grid View ClassesData Binding and Data Grid View Classes
Data Binding and Data Grid View Classes
 
Ado.net
Ado.netAdo.net
Ado.net
 
ASP.NET 08 - Data Binding And Representation
ASP.NET 08 - Data Binding And RepresentationASP.NET 08 - Data Binding And Representation
ASP.NET 08 - Data Binding And Representation
 
Slick: Bringing Scala’s Powerful Features to Your Database Access
Slick: Bringing Scala’s Powerful Features to Your Database Access Slick: Bringing Scala’s Powerful Features to Your Database Access
Slick: Bringing Scala’s Powerful Features to Your Database Access
 
Accessing data with android cursors
Accessing data with android cursorsAccessing data with android cursors
Accessing data with android cursors
 

Similar to Connect to Oracle DB from VB6 Using ADO Objects

PATTERNS07 - Data Representation in C#
PATTERNS07 - Data Representation in C#PATTERNS07 - Data Representation in C#
PATTERNS07 - Data Representation in C#Michael Heron
 
MCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISP
MCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISPMCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISP
MCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISPAli Shah
 
Visual Basic.Net & Ado.Net
Visual Basic.Net & Ado.NetVisual Basic.Net & Ado.Net
Visual Basic.Net & Ado.NetFaRid Adwa
 
Simple ado program by visual studio
Simple ado program by visual studioSimple ado program by visual studio
Simple ado program by visual studioAravindharamanan S
 
Introduction to ado
Introduction to adoIntroduction to ado
Introduction to adoHarman Bajwa
 
Oracle EBS 12.1.3 : Integrate OA Framework BC4J components within java concur...
Oracle EBS 12.1.3 : Integrate OA Framework BC4J components within java concur...Oracle EBS 12.1.3 : Integrate OA Framework BC4J components within java concur...
Oracle EBS 12.1.3 : Integrate OA Framework BC4J components within java concur...Amit Singh
 
Iphone programming: Core Data Tutorial for iOS
Iphone programming: Core Data Tutorial for iOSIphone programming: Core Data Tutorial for iOS
Iphone programming: Core Data Tutorial for iOSKenny Nguyen
 
Ado dot net complete meterial (1)
Ado dot net complete meterial (1)Ado dot net complete meterial (1)
Ado dot net complete meterial (1)Mubarak Hussain
 
Ob loading data_oracle
Ob loading data_oracleOb loading data_oracle
Ob loading data_oracleSteve Xu
 
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...Bill Buchan
 
jdbc_presentation.ppt
jdbc_presentation.pptjdbc_presentation.ppt
jdbc_presentation.pptDrMeenakshiS
 
Data base connectivity and flex grid in vb
Data base connectivity and flex grid in vbData base connectivity and flex grid in vb
Data base connectivity and flex grid in vbAmandeep Kaur
 
Oracle endeca information discovery v3.0 integration with the obiee 11g bi se...
Oracle endeca information discovery v3.0 integration with the obiee 11g bi se...Oracle endeca information discovery v3.0 integration with the obiee 11g bi se...
Oracle endeca information discovery v3.0 integration with the obiee 11g bi se...Ravi Kumar Lanke
 
Learning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFrameworkLearning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFrameworkAkhil Mittal
 
Dao pattern
Dao patternDao pattern
Dao patternciriako
 

Similar to Connect to Oracle DB from VB6 Using ADO Objects (20)

Ado.net
Ado.netAdo.net
Ado.net
 
unit 3.docx
unit 3.docxunit 3.docx
unit 3.docx
 
PATTERNS07 - Data Representation in C#
PATTERNS07 - Data Representation in C#PATTERNS07 - Data Representation in C#
PATTERNS07 - Data Representation in C#
 
Excel
ExcelExcel
Excel
 
MCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISP
MCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISPMCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISP
MCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISP
 
Visual Basic.Net & Ado.Net
Visual Basic.Net & Ado.NetVisual Basic.Net & Ado.Net
Visual Basic.Net & Ado.Net
 
Ado
AdoAdo
Ado
 
Simple ado program by visual studio
Simple ado program by visual studioSimple ado program by visual studio
Simple ado program by visual studio
 
PPT temp.pptx
PPT temp.pptxPPT temp.pptx
PPT temp.pptx
 
Introduction to ado
Introduction to adoIntroduction to ado
Introduction to ado
 
Oracle EBS 12.1.3 : Integrate OA Framework BC4J components within java concur...
Oracle EBS 12.1.3 : Integrate OA Framework BC4J components within java concur...Oracle EBS 12.1.3 : Integrate OA Framework BC4J components within java concur...
Oracle EBS 12.1.3 : Integrate OA Framework BC4J components within java concur...
 
Iphone programming: Core Data Tutorial for iOS
Iphone programming: Core Data Tutorial for iOSIphone programming: Core Data Tutorial for iOS
Iphone programming: Core Data Tutorial for iOS
 
Ado dot net complete meterial (1)
Ado dot net complete meterial (1)Ado dot net complete meterial (1)
Ado dot net complete meterial (1)
 
Ob loading data_oracle
Ob loading data_oracleOb loading data_oracle
Ob loading data_oracle
 
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
 
jdbc_presentation.ppt
jdbc_presentation.pptjdbc_presentation.ppt
jdbc_presentation.ppt
 
Data base connectivity and flex grid in vb
Data base connectivity and flex grid in vbData base connectivity and flex grid in vb
Data base connectivity and flex grid in vb
 
Oracle endeca information discovery v3.0 integration with the obiee 11g bi se...
Oracle endeca information discovery v3.0 integration with the obiee 11g bi se...Oracle endeca information discovery v3.0 integration with the obiee 11g bi se...
Oracle endeca information discovery v3.0 integration with the obiee 11g bi se...
 
Learning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFrameworkLearning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFramework
 
Dao pattern
Dao patternDao pattern
Dao pattern
 

Recently uploaded

Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxruthvilladarez
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...liera silvan
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 

Recently uploaded (20)

Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 

Connect to Oracle DB from VB6 Using ADO Objects

  • 1. Connect to an Oracle Database from Visual Basic 6 Connect to an Oracle Database from Visual Basic 6 (Part 2) Preface This is Part 2 in a 2 part series on using Visual Basic 6 to connect to an Oracle database. In Part 1, I showed you how to use an ADO Data Control to make the connection. In this article, I show you how to use ADO Objects to make the connection. Not only will the article be useful for the Oracle programmers in the audience, it will also show you how to use ADO objects in your program. ADO Objects What's an Object? Hopefully you've read by well-received book, Learn to Program with Visual Basic 6 Objects. As much as I would love to spend a lot of time discussing object variables in detail, I'm going to presume that you know what they are and how, in a nutshell, they refer to an object instantiated from a class or template. Having said that, I will tell you that the ADO Object model can be confusing, particularly because there are frequently many ways to achieve the same functionality. In this article, we will create an ADO Connection Object, then use an ADO Command Object to return the results of a query to an ADO Recordset object. Finally, as an added bonus, I'll show you something that your average programmer doesn't know can be done--we'll set the Recordsource of an ADO DataGrid to the recordset we produce in code. In other words, without the ADO Data Control, we'll populate a DataGrid. Similar to what we did when we added the ADO Data Control to our Visual Basic Toolbox, in order to use ADO Objects we must first set a reference to the ADO Object Library. The version of the ADO Object Library installed on your PC will vary depending upon a number of factors-the PC I'm using as I write this article contains ADO Version 2.x. To add a reference, select Project-References from the Visual Basic Menu Bar, then select Microsoft ActiveX Data Objects 2.6 Library. If the version number doesn't match yours exactly, that's fine---just be sure to select the ActiveX Data Objects Library, not the ActiveX Data Objects Recordset Library that follows it in the list. Be sure that you select Project-References, NOT Project-Components. Object libraries have no visible interface, and so they won't appear in the http://www.johnsmiley.com/cis18.notfree/Smiley004/Smiley004.htm (1 of 8)3/28/2004 11:35:52 AM
  • 2. Connect to an Oracle Database from Visual Basic 6 Components section. When you click on the OK button, nothing obvious happens--but you now have access to ADO Objects in your program. Before we get too far, let's start the process of adding the ADO DataGrid Control to our form. In order to do that, we need to select Project-Components (yes, that's right, Components this time) from the Visual Basic Menu Bar, and select Microsoft DataGrid Control 6.0. http://www.johnsmiley.com/cis18.notfree/Smiley004/Smiley004.htm (2 of 8)3/28/2004 11:35:52 AM
  • 3. Connect to an Oracle Database from Visual Basic 6 When you click on the OK button, the DataGrid Control (the ADO version) is added to the Visual Basic Toolbox. http://www.johnsmiley.com/cis18.notfree/Smiley004/Smiley004.htm (3 of 8)3/28/2004 11:35:52 AM
  • 4. Connect to an Oracle Database from Visual Basic 6 With the DataGrid control in your Toolbox, now it's time to add it to your form... http://www.johnsmiley.com/cis18.notfree/Smiley004/Smiley004.htm (4 of 8)3/28/2004 11:35:52 AM
  • 5. Connect to an Oracle Database from Visual Basic 6 Working with ADO Objects to achieve your Oracle Connection The Connection Property It was at this point in Part 1 of this article that we adjusted properties of the ADO Data Control to achieve a Connection to an Oracle Database, and to build a Recordset which was then used to populate the ADO DataGrid. Here, we'll use an ADO Connection Object and a Recordset Object to achieve the same functionality. For demonstration purposes, let's place this code in the Load Event Procedure of the Form... Dim oconn As New ADODB.Connection http://www.johnsmiley.com/cis18.notfree/Smiley004/Smiley004.htm (5 of 8)3/28/2004 11:35:52 AM
  • 6. Connect to an Oracle Database from Visual Basic 6 Dim rs As New ADODB.Recordset Dim strSQL As String strSQL = "SELECT * FROM EMPLOYEES" Set oconn = New ADODB.Connection oconn.Open "Provider=msdaora;Data Source=John.world;User Id=jsmiley; Password=neveruseyourdogsnameasyourpassword;" rs.CursorType = adOpenStatic rs.CursorLocation = adUseClient rs.LockType = adLockOptimistic rs.Open strSQL, oconn, , , adCmdText Set DataGrid1.DataSource = rs What's going on? Let me explain. These first two lines of code declare 2 object variables. The first is an ADO Connection Object Dim oconn As New ADODB.Connection followed by an ADO Recordset Object Dim rs As New ADODB.Recordset Both of these declarations are necessary in order to build a Recordset--which is a virtual (in memory) representation of an actual Oracle table in our Database. The names for these two object variables are pretty standard, and you see them all the time in code and in books and articles. In fact, if you search for oconn or rs using a Google search, you'll gets lots of results for ADO. The final variable declaration is a String variable that we will use to 'hold' the SQL statement used to build our recordset. Dim strSQL As String Once we've declared the strSQL variable, it's time to assign a SQL statement to it. You may remember this statement from Part 1 of this article--it's used to retrieve every record from the Employees table in my Oracle database... strSQL = "SELECT * FROM EMPLOYEES" Our next step is to open our ADO Connection. We do that by executing the http://www.johnsmiley.com/cis18.notfree/Smiley004/Smiley004.htm (6 of 8)3/28/2004 11:35:52 AM
  • 7. Connect to an Oracle Database from Visual Basic 6 Open method of our Connection Object. The Open method is looking for four parameters: the Provider name, the Data Source (or HostName), the User ID and the Password of the database in question. oconn.Open "Provider=msdaora;Data Source=John.world;User Id=jsmiley; Password=neveruseyourdogsnameasyourpassword;" Do the details of the Open Method look familiar? They should. In Part 1 I told you that the Connection String that the ADO Data Control wizard built for us could be used to open a Connection in code. In fact, some programmers use the ADO Data Control wizard to build their Connection Strings for them, and then copy and paste them into the code window. Before we build the Recordset object (not the same as the Connection object) we need to adjust three properties of the Recordset object, the CursorType, CursorLocation and LockType. If you want to know more about the details of the Recordset object, I highly recommend Wrox's ADO 2.6 Programmer's Guide. rs.CursorType = adOpenStatic rs.CursorLocation = adUseClient rs.LockType = adLockOptimistic Now it's time to open the Recordset. If you are confused about the difference between a Connection and a Recordset, think of the Connection as the Database window you see when you first start Access. The Database contains a window with a list of tables that you can then select. When you select one, a Data Window opens up with just the information from that table. This is the equivalent of the Recordset. rs.Open strSQL, oconn, , , adCmdText In theory, we now have a Recordset object built containing all of the data in the Employees table of my Oracle database. How do we see the Recordset? We can use the Set statement to assign the Recordset object to the DataSource property of our DataGrid. Set DataGrid1.DataSource = rs If we now run the program, the code in the Load Event procedure of the form executes. A Connection object is created, initiating the Connection to my Oracle Database. Then a Recordset object is created, retrieving Employee records. Finally, the DataSource property of the DataGrid is set to point to the Recordset object. Here's our form after the code executes--as you can see, the DataGrid is now populated. http://www.johnsmiley.com/cis18.notfree/Smiley004/Smiley004.htm (7 of 8)3/28/2004 11:35:52 AM
  • 8. Connect to an Oracle Database from Visual Basic 6 Summary I hope you've enjoyed this article. http://www.johnsmiley.com/cis18.notfree/Smiley004/Smiley004.htm (8 of 8)3/28/2004 11:35:52 AM