• Tidak ada hasil yang ditemukan

Directory listing of http: uap.unnes.ac.id ebook biblebook Visual Basic 6 com & Library Books

N/A
N/A
Protected

Academic year: 2017

Membagikan "Directory listing of http: uap.unnes.ac.id ebook biblebook Visual Basic 6 com & Library Books"

Copied!
14
0
0

Teks penuh

(1)

Accessing

Databases from

Visual Basic

I

n this c hapter, I’m go ing to disc uss ho w Visual Basic talks to a database server. ODBC and OLE DB are the lo w-level tec hno lo gies that ac tually perfo rm the wo rk, while the DAO, RDO, and ADO o bjec t mo dels enc apsulate these tec hno lo gies to make them easy fo r the Visual Basic pro grammer to use.

M icrosoft Database

Programming APIs

Just bec ause yo ur database server is based o n SQL do esn’t mean that yo u c an easily ac c ess it fro m yo ur favo rite pro gram-ming language. Many database vendo rs pro vide a spec ial pre-c o mpiler that translates embedded database statements into database subro utine c alls that in turn c o mmunic ate with the database server. The pro blem with this appro ac h is that yo u need a different pre-c o mpiler fo r eac h pro gramming language the database vendo r suppo rts. Of c o urse, sinc e the pre-c o mpil-ers are spec ific fo r eac h database server, yo u’ll need a pre-c o m-piler fo r eac h database server fo r whic h yo u develo p pro grams. Rather than develo ping a large number o f pre-c o mpilers fo r eac h c o mbinatio n o f database server and pro gramming lan-guage, Mic ro so ft develo ped a standard c alled Ope n Database Co nne ctivity(ODBC), whic h later evo lved into OLE DB.

(2)

ODBC

The ODBC standard defines an Applicatio n Pro gramming Inte rface ( API) fo r database pro gramming. This allo ws yo u to write a pro gram using standard subro utine c alls fo r any database server that suppo rts ODBC, making it po ssible fo r the same o bjec t c o de to ac c ess any ODBC-c o mpatible database server.

ODBC architecture

ODBC is based o n the idea that the c alls to the API ro utines made by the applic a-tio n pro gram are translated to lo wer c alls that are passed o nto a database driver ( see Figure 6-1) . The database driver in turn perfo rms the nec essary wo rk to talk to the database server. Thus, the database vendo r need o nly pro vide an ODBC-c o m-patible driver fo r eac h c lient c o mputer system and no t fo r eac h c o mpiler o n eac h c lient o perating system.

Figure 6-1: The client side of the ODBC architecture is based on the driver m odel.

On the database server side, all the database vendo r needs to do is pro vide a single ODBC interfac e, whic h will be shared by all ODBC c lients. This means that database vendo rs c an preserve their native interfac e, plus any o ther spec ialized interfac es fo r o ther applic atio ns.

Application Program

ODBC API ODBC Database

Driver

Database Engine

Database Server

Client Computer

(3)

A utility pro gram c alled the ODBC Administrato r is inc luded as part o f the o perat-ing system to manage the set o f ODBC drivers and database servers that are avail-able to the ODBC API. This pro gram allo ws yo u to add and remo ve ODBC drivers, spec ify ho w to c o nnec t to the database server, and inc lude sec urity info rmatio n that will be used when the c o nnec tio n between the c lient c o mputer and the database server is o pened.

Drawbacks to ODBC

While the arc hitec ture o f ODBC allo ws a great deal o f flexibility o n the part o f the database vendo r, there are several drawbac ks to writing ODBC applic atio ns. First, the ODBC APIs are diffic ult to use, espec ially if yo u aren’t pro gramming in C. Sec o nd, the ODBC APIs are o ften slo wer than the native database interfac e. Third, the ODBC API o ften impo ses restric tio ns o n the SQL statements that c an be used.

While the first drawbac k applies to all database servers, the sec o nd two drawbac ks apply mainly to no n-Mic ro so ft database servers. Mic ro so ft uses ODBC as the native interfac e to bo th the Jet database and SQL Server. They spent a lo t o f time tuning the interfac e fo r o ptimal results. While o ther database vendo rs suppo rt ODBC, their native interfac es may o ffer impro ved perfo rmanc e and func tio nality, espec ially fo r no n-Mic ro so ft c o mpilers.

Database Access Objects (DAO)

Bec ause the ODBC API is diffic ult to use, Mic ro so ft c ho se to build an o bjec t-o riented interfac e to ODBC c alled Data Acce ss Obje cts(DAO). DAO was o riginally develo ped fo r Ac c ess and Visual Basic pro grammers who needed to ac c ess the Jet database. Mo st o f the func tio nality available in DAO mirro rs features fo und in the Jet database.

While DAO c an be used to ac c ess o ther databases, the pro c ess is o ften diffic ult to use and c lumsy in its implementatio n. To ac c ess an SQL Server database, yo u need to c reate a Jet database and then go thro ugh the Jet database to ac c ess the remo te database. While DAO allo ws yo u to ac c ess the ODBC database direc tly, eac h time yo u o pen the database, yo u must do wnlo ad a lo t o f info rmatio n abo ut the database struc tures that yo u might ac c ess. By using the Jet database, this info rmatio n is saved lo c ally and need no t be do wnlo aded eac h time yo u o pen the database.

Visual Basic 6 and Access 2000: While Jet 3.51 is shipped w ith Visual Basic 6, Jet 4.0 is shipped w ith Access 2000. While it’s possible to use Visual Basic 6 and DAO w ith Jet 4.0, m any of the new features found in Jet 4.0 can only be used if you’re using ADO.

(4)

Remote Database Objects (RDO)

The Re mo te Database Obje cts( RDO) o bjec t mo del was built to address the pro b-lems asso c iated with ac c essing SQL Server fro m Visual Basic . First available with the Visual Basic 5 Enterprise Editio n o f Visual Basic , RDO o ffered a streamlined o bjec t mo del whic h in turn o ffered muc h better perfo rmanc e than DAO when used with a database suc h as SQL Server o r Orac le. Ho wever, RDO do esn’t wo rk with the Jet database and o ther single-user database systems.

One feature lac king in RDO that is fo und in DAO is the ability to c reate a database by defining o bjec ts. Given that RDO is really targeted at large installatio ns, this isn’t nearly as limiting as it seems. These installatio ns generally have a gro up o f peo ple kno wn as database administrato rs( DBAs) , who se so le respo nsibility is the c reatio n and maintenanc e o f database struc tures. DBAs o ften use spec ialized to o ls to help them design and do c ument their databases.

OLE DB

After years o f wo rking with ODBC, Mic ro so ft rec o gnized that the database server to database c lient mo del used by ODBC c o uld be generalized as a data pro vide rto data co nsume rmo del ( see Figure 6-2) .

Figure 6-2: A data provider handles data requests from a data consum er.

Data providers

A data pro vide ris a pro gram that supplies data to ano ther pro gram. In the ODBC mo del, the data pro vider is the database server. Ho wever, in the OLE DB, nearly anything that c an pro duc e data c o uld be c o nsidered a data pro vider. This allo ws pro grammers to treat suc h things as an Exc el wo rkbo o k, a flat file, o r ano ther c us-to m-built pro gram like a database server.

Data Provider Data Customer

Data Request

(5)

Data consumers

A data co nsume ris simply a c lient pro gram that requests data fro m a data pro vider. Two types o f info rmatio n may be requested fro m the data pro duc er: dataand me ta-data.Data is the info rmatio n used by an applic atio n, while metadata is info rmatio n abo ut the struc tures used to ho ld the data. Metadata inc ludes suc h info rmatio n as the name o f eac h c o lumn returned, its size and data type, and o ther desc riptive info rmatio n.

ActiveX Data Objects (ADO)

The Active X Data Obje cts( ADO) o bjec t mo del was c reated to replac e bo th DAO and RDO o bjec t mo dels and was shipped fo r the first time in Visual Basic 6. It uses the newer OLE DB API rather than the o lder ODBC API. In additio n, ADO has an even simpler o bjec t mo del than fo und in the DAO and RDO, whic h makes it easier to use.

ADO is mo deled after RDO and inc ludes many o f the familiar o b jec ts fo und in DAO and RDO, suc h as the Re co rdSe to b jec t. Ho wever, where RDO maintains a relatively stric t o b jec t hierarc hy, ADO’s o b jec t struc ture is muc h lo o ser. This means that yo u generally need fewer o b jec ts in yo ur pro gram, and the resulting c o de is even simpler.

Like RDO, ADO do esn’t inc lude o bjec ts that yo u c an use to define yo ur database. Ho wever, with versio n 2.5 o f ADO, Mic ro so ft inc luded a new set o f o bjec ts kno wn as ADOX, whic h c an be used to define database struc tures and sec urity. These o bjec ts wo uld be useful if yo u want to write a general-purpo se to o l that allo ws yo u to c re-ate and manipulre-ate database struc tures fro m multiple database vendo rs. They c an help smo o th o ver differenc es in ho w the vario us vendo rs implement their o wn SQL extensio ns.

ADO MD is ano ther set o f extensio ns to the base ADO o bjec ts. These o bjec ts allo w yo u to wo rk with a multidimensio nal database suc h as the OLAP Servic es bundled with SQL Server. Yo u c an use these o bjec ts to build yo ur o wn pro grams fo r manipu-lating info rmatio n fro m yo ur o wn data wareho use.

OLE DB providers

(6)

Table 6-1

Common OLE DB Providers

Provider Name Description

Microsoft Jet 3.51 OLE DB Provider Supports Access 97/ Jet 3.51 databases Microsoft Jet 4.0 OLE DB Provider Supports Access 2000/ Jet 4.0 databases Microsoft OLE DB Provider for SQL Server Supports SQL Server 6.5 and 7.0 Microsoft OLE DB Provider for Oracle Supports Oracle 7 and 8

Microsoft OLE DB Provider for Microsoft Supports Microsoft Active Directory Service Active Directory Service found in Window s 2000 Server

Microsoft OLE DB Provider for Microsoft Supports Microsoft Index Server 2.0 and Index Service new er

Microsoft OLE DB Provider for OLAP Services Supports Microsoft OLAP Server Microsoft OLE DB Provider for ODBC Drivers Supports generic ODBC access

Even if yo ur datab ase server do esn’t suppo rt OLE DB, that do esn’t mean yo u c an’t ac c ess the datab ase fro m ADO. There is a spec ial pro vider kno wn OLE DB Pro vider many peo ple implement their o wn database management system? Ho wever, if yo u have a c usto m data sto rage applic atio n, yo u c an c reate yo ur o wn OLE DB pro vider fo r it. This will allo w pro grammers to ac c ess it using to o ls with whic h they are already familiar.

Visual Basic Database Tools

To suppo rt database pro gramming, Visual Basic inc ludes a nic e asso rtment o f to o ls that will assist yo u in c reating yo ur applic atio n, inc luding the fo llo wing:

✦Data Enviro nment Designer

(7)

✦Database Designer

✦SQL Edito r

✦T-SQL Debugger

✦Query Designer

✦Data Repo rt Designer

✦UserCo nnec tio n Designer

With the exc eptio n o f the UserCo nnec tio n Designer, these to o ls will wo rk o nly in an ADO pro gramming enviro nment. The UserCo nnec tio n Designer wo rks o nly with RDO o bjec ts.

Data Environment Designer

The Data Enviro nment Designer ( see Figure 6-3) is an interac tive design-time to o l that helps yo u c reate database o bjec ts fo r use at runtime. It is based o n the ADO o bjec t mo del and will no t wo rk with RDO and DAO o bjec ts.

Figure 6-3: Using the Data Environm ent Designer

Yo u c an use the Data Enviro nment Designer to perfo rm the fo llo wing tasks:

✦Define Conne ctiono bjec ts to yo ur database using either OLE DB data so urc es o r ODBC data so urc es.

✦Define Commando bjec ts using tables, queries, and sto red pro c edures fro m yo ur database.

(8)

✦Drag and dro p fields and tables fro m a Commando bjec t in the designer o nto a Formo bjec t o r the Data Re porte rdesigner that are auto matic ally bo und to the Commando bjec t.

✦Spec ify default the type o f c o ntro l to be used as part o f drag and dro p o peratio ns.

✦Bind data-aware c o ntro ls to Fie ldo bjec ts within a Command o bjec t.

✦Attac h c o de fo r Conne ctionand Re cordse to bjec ts in the Data Enviro nment Designer.

✦Trap all ADO events fo r the Conne ctionand Commando bjec ts.

Data View Window

The Data View Windo w allo ws yo u to ac c ess yo ur database system thro ugh Visual Basic instead o f a partic ular database vendo r’s utility ( see Figure 6-4) . Thro ugh the Data View Windo w, yo u c an ac c ess the Mic ro so ft Visual Database To o ls.

The Visual Database Tools are present only in the Enterprise Edition of Visual Basic. This not only includes the Data View Window, but the Database Designer, the Query Designer, the SQL Editor, and the T-SQL Debugger.

Figure 6-4: Using the Data View Window to brow se and edit the database structures in your database

Yo u c an use the Data View Windo w to perfo rm the fo llo wing tasks:

✦Create a Conne ctiono bjec t that c an be used to ac c ess yo ur database while in design mo de o r at runtime.

✦Design yo ur database graphic ally using the Database Designer.

(9)

✦View the c o ntents o f a table o r view using a wo rksheet-like display.

✦Create and edit sto red pro c edures using the SQL Edito r.

✦Debug sto red pro c edures using the T-SQL Debugger.

✦Create views using the Query Designer.

✦Generate repo rts using the Data Repo rter Designer.

Database Designer

The Database Designer is a graphic al to o l that presents a database using a graphi-c al diagram ( see Figure 6-5) . The Database Designer wo rks with bo th Migraphi-c ro so ft SQL Server and Orac le database systems.

Figure 6-5: Designing a database graphically using the Database Designer

Yo u c an use the Database Designer to perfo rm the fo llo wing tasks:

✦Create and mo dify tables.

✦Add and delete indexes.

✦Define relatio nships between tables.

(10)

SQL Editor

The SQL Edito r pro vides a simple edito r to help yo u write sto red pro c edures ( see Figure 6-6) .

Figure 6-6: Editing a sim ple stored procedure using the SQL Editor

Yo u c an use the SQL Edito r to perfo rm the fo llo wing tasks:

✦Create and edit SQL sto red pro c edures.

✦Exec ute sto red pro c edures.

✦Use the T-SQL Debugger to debug yo ur sto red pro c edures.

T-SQL Debugger

The T-SQL Debugger helps yo u test and debug yo ur sto red pro c edures ( see Figure 6-7) .

Yo u c an use the T-SQL Debugger to perfo rm the fo llo wing tasks:

✦Display the c o ntents o f lo c al variables and parameters.

✦Mo dify lo c al variables and parameters while exec uting the sto red pro c edure.

✦Co ntro l exec utio n by using breakpo ints.

✦Step thro ugh the sto red pro c edure.

✦View glo bal variables.

(11)

Figure 6-7: Testing a sim ple stored procedure using the T-SQL Debugger

Query Designer

The Query Designer is a to o l that wo rks with the Data View Windo w to drag and dro p tables and c o lumns to c reate a view o r query ( see Figure 6-8) .

Figure 6-8: Creating a view using the Query Designer

Yo u c an use the Query Designer to perfo rm the fo llo wing tasks:

✦Drag and dro p tables fro m the Data View Windo w o nto yo ur query.

(12)

✦Selec t the c o lumns to be o utput as a result o f the query.

✦Bro wse and edit live views o f yo ur data in yo ur view.

Data Reporter Designer

The Data Repo rter Designer helps yo u build repo rts interac tively ( see Figure 6-9) .

Figure 6-9: Building a report using the Data Reporter Designer

Yo u c an use the Data Repo rter Designer to perfo rm the fo llo wing tasks:

✦Create repo rts by dragging and dro pping fields fro m the Data View Windo w.

✦Add c o ntro ls to yo ur repo rt, just like yo u add c o ntro ls to a Fo rm in a Visual Basic pro gram.

✦Implement a Print Preview func tio n in yo ur pro gram to allo w users to preview the repo rt befo re sending it to the printer.

✦Allo w the user to resume wo rking while the repo rt is running ( i.e., asyn-c hro no us exeasyn-c utio n o f the repo rt) .

UserConnection Designer

The UserCo nnec tio n Designer is a to o l that assists the RDO pro grammer in building c o nnec tio n (rdoConnection) and query (rdoQuery) o bjec ts. These o bjec ts are made available at the pro jec t level and c an be used anywhere in yo ur pro gram. A key part o f these o bjec ts is that they have a simplified metho d fo r respo nding to database events. These o bjec ts also make it easier to c all sto red pro c edures at runtime.

RDO not ADO: The UserConnection Designer supports only the RDO object m odel. Do not try to use it w ith DAO or ADO object m odels. The Data Environm ent Designer is a m uch im proved version of the UserConnection Designer and is avail-able for use w ith ADO program s.

(13)

Thoughts on Visual Basic

Visual Basic program m ers can choose from three different object m odels to access their databases: DAO, RDO, and ADO. Deciding w hich m odel is best for you m ay seem difficult, but it really shouldn’t be. ADO is the m odel that Microsoft w ould prefer you to use. It is w here they are placing the m ajority of their efforts and it is w here you w ill see the m ost enhancem ents in the future.

Microsoft isn’t going to abandon the DAO object m odel anytim e soon. There are too m any Visual Basic program s that still use it. Dropping support for DAO in a future release of Visual Basic w ould sim ply m ean that m any Visual Basic program m ers w ouldn’t upgrade. (Consider the num ber of people still using Visual Basic 3 and 4 because Microsoft stopped supporting 16-bit versions w ith Visual Basic 5.) So if you’re currently using DAO, you can continue to use it in the foreseeable future. How ever, if you’re not currently using DAO, you probably shouldn’t start. Directly accessing SQL Server databases using DAO isn’t really practical for large applica-tions. As m ore and m ore people tried to do this, Microsoft developed RDO as a solution. Its low overhead offered m uch better perform ance in large-scale applications. How ever, RDO’s tim e is also past, and if you’re not using it now, don’t start.

ADO is the w ave of the future. You should be using it for new applications w herever practi-cal. While you don’t have to drop everything you’re currently doing to convert your existing applications to ADO now, you should have a plan in place that ensures that they w ill all be converted over tim e. This is especially true in applications w ith high transaction rates or applications that are offering services over the Internet.

In the form er case, m any of the things that Microsoft w ill do to im prove perform ance w ill be tied to ADO. This includes facilities such as the COM+ Transaction Server and future ver-sions of SQL Server. In the latter case, Microsoft is already adding features, such as XM L sup-port to ADO, w hich w ill not be added to DAO and RDO. Also, all of the tools designed to m ake you m ore productive, such as the Data View Window and the Data Environm ent Designer, also require an ADO-based program .

Summary

In this c hapter yo u learned that:

✦ODBC was o riginally develo ped as an alternative to develo ping pre-c o mpilers fo r eac h pro gramming language to translate SQL statements into exec utable c o de.

✦DAO was c reated to pro vide an o bjec t-o riented API to ac c ess Mic ro so ft Jet as well as any ODBC database.

✦RDO is a lo w-o verhead, general-purpo se ODBC interfac e.

(14)

✦ADO is the rec o mmended way fo r Visual Basic pro grammers to ac c ess OLE DB.

✦Visual Basic inc ludes a number o f spec ialized to o ls fo r the database pro gram-mer, whic h inc lude the Data Enviro nment Designer, Data View Windo w, Data-base Designer, SQL Edito r, T-SQL Debugger, Query Designer, Data Repo rter Designer, and the UserCo nnec tio n Designer.

Gambar

Figure 6-1: The client side of the ODBC architecture is based on the driver model.
Figure 6-2: A data provider handles data requests from a data consumer.
Table 6-1Common OLE DB Providers
Figure 6-3: Using the Data
+6

Referensi

Dokumen terkait

Adapun tujuan diadakannya indeks saham syariah sebagaiman Jakarta Islamic Index yang melibatkan 30 saham terpilih, yaitu sebagai tolak ukur untuk mengukur kinerja

You can download and install the soft file of this incredible book Time Mends (Timber Wolves, Book 2) By Tammy Blackwell currently and in the link offered.. Yeah, different with

Indonesia merupakan negara dengan orang muslim terbesar didunia, akan tetapi setiap ormas muslim sendiri tentunya berbeda dalam memutuskan suatu masalah baru yang muncul

54 Tahun 2010 tentang Pengadaan Barang/Jasa Pemerintah serta menindaklanjuti proses seleksi untuk Paket Pekerjaan Pengadaan Meubelair (985 Unit) , bersama ini kami

n salinannya sebagaimana tertuang dalam lam proses pembuktian kualifikasi

Uang merupakan uang milik masyarakat atau uang beredar di masyarakat (di luar Bank Sentral seperti Bank Indonesia dan perbankan atau semua bank), yang terdiri dari :.. Uang Kertas

Puji syukur kami panjatkan kepada Tuhan Yang Maha Esa atas limpahan rahmat dan hidayah-Nya sehingga Prosiding Seminar Nasional MIPA Universitas Negeri Yogyakarta

ebagaimana tertuang dalam formulir isian buktian kualifikasi dapat