Chapter 22 - Accessing SQL Server Databases Through Front-End Products![]()
![]() With SQL Server 6, Microsoft introduced a new client-side interface through Open database connectivity (ODBC). Rather than using the old DB-Library approach that was created by Sybase, Microsoft decided to create a new access path through ODBC. As custodian of the ODBC specification, it was easy for Microsoft to tailor the ODBC interface, making it no longer truly "generic" and, in fact, optimizing it for SQL Server access. With this new interface, there is no longer the stigma of slow performance associated with ODBC access. Microsoft still supports the DB-Library interface for backwards compatibility, but it seems that their primary focus is on making ODBC the standard for database access.
Configuring ODBC for SQL Server 6.5ODBC is a way of connecting various different data services to different front-end applications in a consistent manner. ODBC has undergone several revisions since its inception in 1991.ODBC is configured through a Control Panel Applet: ODBC or ODBC32. In this chapter we will show you how to configure ODBC under Windows NT Workstation. In Windows 95, the only difference is the shading and etching of the dialog boxes; functionally, the methods detailed later in the chapter are identical. To configure ODBC for use with Microsoft SQL Server 6.5, follow these steps:
Using Gupta's SQLWindowsGupta's SQLWindows is a classic front-end tool that has been around since the late 1980s. Since that time, it has acquired various drivers written natively to provide communications to different DBMSs. Most recently, ODBC was added with SQLWindows 5.0. However, because Microsoft revised ODBC with the release of SQL Server 6, Gupta was required to address the new functionality provided in the ODBC driver and put out a special release. Consequently, in order to connect to SQL Server 6.5 from SQLWindows, version 5.0.2 must be used.All the source code that follows for the SQLWindows application can be found in SWINDEMO.APP on the enclosed CD-ROM.
Establishing a ConnectionConnecting or preparing SQLWindows for use with SQL Server 6, involves a somewhat arcane method of using Gupta's query and report writing tool, Quest, to set up a private INI file with connection information about the ODBC data source. The INI file in question is the GUPTA.INI file and can be found in the directory that is indicated in the WIN.INI file by the keyword SqlWinDir.
Listing 22.1 SWINDEMO.APPConnecting to SQL Server using SQLWindows' SqlConnect( ) Function
Preparing and Executing SQL StatementsSQLWindows provides a simple interface for executing statements. The same interface is used to prepare and execute queries on all database server types. Listing 22.2 shows the execution of a simple SELECT to count the number of objects and place the results in the data field on the screen dfCount.Listing 22.2 SWINDEMO.APPExecuting a SQL statement on SQL Server Using SQLWindows' SqlPrepareAndExecute( ) Function
Using Stored Procedures and Command BatchesIn SQLWindows there are some extension functions for the use of executing stored procedures on the server. These functions are prefixed with the letters Odr. To add this functionality to an application, include the Gupta supplied include file: ODBSAL.APL, which should be located in the root Gupta directory.A simple stored procedure is used to test stored procedure execution. Listing 22.3 shows the code for the stored procedure. Listing 22.3 SWINDEMO.APPSimple Stored Procedure Used to Test Stored Procedure Execution in SQLWindows
Listing 22.4 SWINDEMO.APPExecuting Stored Procedure on SQL Server Using SQLWindows' OdrExecuteProc( ) Function
Sneak Preview of CenturaGupta Corporation is changing its name to Centura Software Corporation in March of 1996. Along with the name change comes a whole new development tool: Centura. We had the opportunity to review a late beta release of Centura just prior to the printing of this book, and we have to say we are impressed.As a new product, Centura was remarkably stable and feature-rich. It is fully code-compatible with SQLWindows and yet adds full 32-bit engineering to its arsenal. The new user interface embraces Windows 95's Explorer metaphor (see fig. 22.10). Fig. 22.10 - The Explorer view of Centura allows you to view the source code in style! However, for old-timers, the Outline is always availablecheck out the Outline tab on the lower-left corner of the right pane. In addition to beefing up its development tool, Centura sports new class wizards that support three-tier programming through three of the most popular architectures: Novell's QuickTuxedo, Open Environment Corporation's DCE-based QuickRPC, and QuickCICS (see fig. 22.11). Fig. 22.11 - The Three-Tier Wizard supports the most popular architectures available. Centura even comes in a special edition for Tuxedo that includes all of the Tuxedo development environment. Finally, Centura adds a new tool for the developerthe Database Explorer. The Database Explorer is similar to Delphi 2's and allows easy browsing of all the key attributes. It even includes editable tables that allow you to directly manipulate the data in the database. Fig. 22.12 shows the Database Explorer editing the authors table. Fig. 22.12 - The Database Explorer allows you to browse the data in the databases of all the different types to which Centura supports native connections: SQLBase, Oracle, Microsoft SQL Server, DB2, Sybase, Informix and ODBC.
More Information and ExamplesGupta has provided two instructive sample applicationsODBSAL1.APP and ODBSAL2.APPthat can be found in the \SAMPLES directory below the Gupta directory. These examples indicate in more detail how to connect to the database and perform various operations on them.
Using Borland's Delphi 2Borland's Delphi 2 is at the cutting edge of client/server application development. It is a new object oriented programming language based on Pascal. Delphi 2 is the new version of Delphi that was released in 1996 to much fanfare. Delphi 2 is a fully 32-bit development environment and has received many awards.All the source code that follows for the Delphi 2 application can be found in DPHI20.PRJ and DPHIDEMO.* on the CD-ROM.
Establishing a ConnectionDelphi 2 has native drivers for SQL Server 6.5 that are shells over Microsoft's ODBC. The simplest way to prepare Delphi 2 for use with a database is to use the Database Explorer Applet that ships with Delphi 2.To use the Database Explorer to prepare the ODBC interface, perform the following steps:
Understanding the Delphi 2 Database ModelIn its most common use, Delphi 2 has a layered approach to interfacing with databases. This layering provides several levels of abstraction from the database itself, allowing a very generic application programming interface through common objects.Delphi 2 has a number of classes that can be used to actually manipulate the data. These classes are responsible for executing the appropriate queries to perform any DML required. The classes that can be used vary from TTable, which is used for representing a table, to TQuery, which enables a custom query to be presented to the visual objects. These physical data interface classes are typically non-visual. These physical data sources are then mapped to a class that is responsible for interfacing with user interface objects/controls, such as data fields and lists. This interface is performed through a non-visual class, TDataSource, which transfers data from the physical data class to the visual objects that you place on an edit form. Manipulating the data in TQuery or TTable to fetch records, update them, etc., can be performed by invoking the methods that they have or by adding a Navigation control to the edit form. TDBNavigator is a class that interacts with TDataSource and provides the standard Next, Previous, Insert, and Update buttons to manipulate the data on the form. By placing one of these controls on an edit form and then hookingeither at design time by setting its property or at runtime by adjusting its property to the required data source on the formyou will have all the necessary components to build an edit window to a data structure (either a query or database table). Finally, in Delphi 2 it is necessary to add controls to view/edit the actual data. Delphi 2 provides all the standard edit controls (including list boxes and combo boxes) that have properties that enable them to be hooked to a particular TDataSource. Placing the control on the form and setting its DataSource and DataField properties is all that is required.
About the Sample Application DPHIDEMOThe sample application provided here demonstrates a simple edit form to the authors table in the pubs database. This application took less than ten minutes to write and shows how easy it is to use Delphi 2.The application demonstrates the use of basic TTable.Table_Authors to read from the database table directly. On top of this class is TDataSource.MyDataSource that performs the data source manipulation. There are data fields on the form that enable editing of the basic name and address information in the table; they are all of class TDBEdit. Finally, to control the interaction with the database, TDBNavigator.MyNavigator is hooked to TDataSource.MyDataSource. To "beef up" the application and to learn a tiny part of Delphi 2, some code executes whenever the form is resized so that the Navigator control stays "docked" to the bottom of the window. The code in Listing 22.5 is so simple that it's amazing to anyone who has ever had to write this in a language such as C. Listing 22.5 DPHI20.PRJDynamic Form Resizing with Objects That Paint Inside the Form's Boundaries
Preparing and Executing SQL StatementsDelphi 2 represents ad hoc queries through the class TQuery. TQuery is a non-visual class that has properties that enable it to be attached to a database. The SQL property is provided to enable the setting of the required SQL statement. TQuery then interacts with a TDataSource just like TTable.In the sample application, the Active property is set to True when the Titles... button is clicked. This causes the SQL to be executed in TQuery.MyQuery and the Grid control to be populated with the results of the SELECT statement. The Grid control, TDBGrid.MyDBGrid, is hooked to the data source for presentation of the query results. Clicking the Titles... button executes the default SQL that was set in TQuery.MyQuery at design time. The Publishers... button dynamically changes the SQL and executes it. Listing 22.6 shows what is required to do this SQL changing at runtime in Delphi 2. Listing 22.6 DPHI20.PRJ Setting a SQL Statement in a Delphi 2 Control and then Activating (Executing) It
Using Stored Procedures and Command BatchesDelphi 2's implementation, Stored Procedures and Command Batches, is identical to that of the general query execution principle. A StoredProcName property is provided to enable you to hook the object with a stored procedure in the server. The TStoredProc is then attached/hooked to a TDataSource and accessed as normal through other data controls.
More Information and ExamplesBecause the version of Delphi 2 that I was working with was an early beta, there was a limited amount of documentation and sample code supplied. However, the documentation was excellent when it was there. A neat feature of Delphi 2 is that class specific help is invoked whenever F1 is pushed during design time and when an object is highlighted. This will make it easy for you to find out about the properties and methods of the Delphi 2 classes provided by Borland.Also, check out the Delphi forum on CompuServe where you will get a lot of help from other Delphi users.
Using Microsoft Visual Basic 4Visual Basic 4 is Microsoft's latest incarnation of the BASIC standard. As a general tool, it has great features and has a huge install base. Microsoft has always tried to latch on to client/server connectivity, and still has some work to do to support it as cleanly as either SQLWindows or Delphi 2.This was the first time we had ever tried to use Visual Basic for database connectivity. We were consistently frustrated by a non-intuitive environment that clearly lacked the integration of a mature database development environment. All the source code that follows for the Visual Basic 4 application can be found in VB4DEMO.FRM and VB4.VBP on the CD-ROM.
Understanding the Visual Basic 4 Database ModelVisual Basic and Delphi 2 have a similar approach to data access. Data access is controlled by a Data object that resides on a form. Unlike Delphi 2, VB4 does not use truly non-visual object classes or container classes. Instead, it is possible to alter the visible property of the Data object to hide it if you want.VB4's Data control is more of a combination of the TDataSource and TDBNavigator classes that are found in Delphi 2, and the visual nature of the arrow buttons explains the need to have it visible most of the time. Once a Data control is correctly configured and bound to a particular table, it is possible to hook data fields to it as a declared data source on the form.
Connecting to SQL ServerWe had expected to find some kind of configuration utility in Visual Basic 4 for database connectivity; however, VB4 is so closely integrated to ODBC, that ODBC is the only interface provided.To set up a Data control we tried to specify ODBC as the Connect property of the Data control that we was adding to the form; however, VB4 refused to correctly work with this. In the end, we gave up and used the Data Form Designer Wizard to create a data form and then copied the Connect string from there. This was truly an inelegant interface and is very surprising from Microsoft. For future reference, the following is a Connect string for ODBC connectivity to SQL Server:
Preparing and Executing SQL StatementsVB4's Data control can be used to either represent tables or process queries directly. The RecordSource property can either be set to a table name or to a query. The following code snippet demonstrates a pushbutton changing the query used to populate the Data control. In turn, the Data control acts as a DataSource for a DBGrid control that is also on the form. Performing the Refresh method of the Data control causes the query to execute and in turn causes the grid to be populated:
About the Sample Application VB4DEMOThe sample application that is included on the CD-ROM demonstrates a basic form that enables browsing and editing of the authors table in the pubs database. The application has an Update button that interfaces with Data.MyDataSource and invokes its UpdateRecord method, as follows:
From Here...In this chapter, you learned about the basics of ODBC and how to configure it on a workstation. In addition, you were introduced to three different approaches to client/server application programming and connectivity: Gupta's SQLWindows 5.0.2, Borland's Delphi 2 and Microsoft's Visual Basic 4. In addition, there are many fine tools that were not mentioned here.Each of the products covered in this chapter is strong in certain areas and weak in others. Choosing one of these development tools should be done after carefully evaluating a project's needs.
|