Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Gauge for SharePoint
Search :       Advanced Search »
Home » COBOL .NET » Using OLEDB in ADO.NET with COBOL

Using OLEDB in ADO.NET with COBOL

The SqlClient class was created by Microsoft and is used primarily to access a SQL Server database.

Author Rank :
Page Views : 5103
Downloads : 25
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
ADOOle.zip
 
 
Nevron Gauge for SharePoint
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Overview

In a previous article ("Data Access with ADO.NET", dated November 4, 2002) we discussed the use of the SqlClient class to access data in ADO.NET. The SqlClient class was created by Microsoft and is used primarily to access a SQL Server database. What about other databases? How would you access them? The answer is OleDB! Microsoft has provided a Framework to enable you to access other databases as well as SQL Server.

Through the use of OleDB you can access other databases that are .NET compliant. This is a major point to make here. The database MUST be .NET enabled for OleDB to work in .NET. One database we have been seeing quite a bit of interest in is Pervasive SQL. The latest version of this database, V8, is fully .NET enabled and we will utilize it in our example today.

I have installed the PervasiveV8 SDK and the Workgroup data engine. Both of these can be downloaded from the Pervasive Developer Zone (http://www.pervasive.com/developerZone/ ) and are free for development purposes. The sample we will be using a simple Console Application written in NetCOBOL that will access the DemoData database and the Person table specifically, displaying the data to the console. The version utilized returns 1500 rows.

Let's begin!

Ole!

We will be using the Ole class within the .NET Framework. The full Namespace specification can be found by performing a search in the Help system for System.Data.Oledb. ( I would recommend taking a few minutes and browsing through the online help files for OleDB. Microsoft has done a great job at providing good background and detail information) As with all of the Framework classes Microsoft has created a very extensive Namespace with many subclasses to assist you in developing your applications. Our application will be a basic connect, issue a command, read the data and handle any exceptions that occur. For this purpose we will utilize the following classes: OleDbConnection, OleDbCommand, OleDbDataReader, and OleDbException.

First Steps

Prior to beginning our Ole process we need to do some housework. We need to create references in our Repository to the Classes we mentioned above so we can access their methods and properties. To accomplish this we create four entries in the Repository, one for each of the aforementioned classes:

Notice the first four lines in the Repository reference the four Ole classes we are going to be using in our example. On the fifth line there is a Property statement. Within the OleDbException class their is a specific property we will be referencing and that is the Message. This property will contain a message whenever an exception is thrown in the OleDb namespace. "Great!" you may say, "So it gives me a message. Now what do I do with it?" Well every good COBOL programmer codes for exceptions and handles them accordingly, right? We're no exception (no pun intended) and have created a Declaratives section to handle any exceptions that occur.

The Declarative section is called any time an exception is generated. The Exception-Object is identified and an instance created (myException). Next the message is obtained from the instance, converted to a string and displayed on the console. A prompt has been added to halt execution of the application until the operator has responded.

In the Working-Storage Section we have created instance objects for each of the classes to be used as well as some other variables to receive the data and display it to the console. The Working-Storage Section will not be reviewed in more detail as it is pretty self-explanatory.

Now we can start on our four segments!

Connection (OleDbConnection)

The first item that has to be addressed is connecting to a database. Each database provider will have their own connection string and you will need to review the providers documentation in order to create a connection string in the proper format. The information necessary to connect to the Pervasive V8 database was located in the Pervasive SQL V8 SDK, Programmers Guide, ADO OLE/DB Programming, Connecting to a database using the OLE DB Provider. The connection was made an opened with the following lines of code:

The first line of code creates a new connection object (CONNECTIONOBJ) with the parameter specified in the USING phrase. This is where you need to be sure you have the proper connection string and syntax for the database you are connecting to. This is where it all starts. If you can't connect, you can't proceed. The second line of code invokes, or calls, the OPEN method of the connection class and establishes connectivity with the database.

Retrieve the Data (OleDbCommand)

Now we have to decide what we are going to retrieve. For our example we want to display the first and last name of the person, their email address and their date of birth. We first researched the database to obtain the proper names for the columns. The logic used to retrieve the data can best be described as 1. Set the criteria, 2. Create the command, and 3. Execute the command. The coding for doing this looks like the following

The first line of code establishes what we are going to select from the database. We are using a variable called MY-STRING to hold the selection because it made the code easier to read and follow. Another important point to consider is you can use the code as a template to create a dynamic selection method that can be called by other modules. By permitting your users to select options on another form you can pass in a selection string and then have it execute and return the data.

The second line of code creates a new command object, OLECOMMANDOBJ, that contains the command to execute. You have to create a new command object prior to execution, you can not merely execute the command. The third line of code calls the ExecuteReader method, which uses the command object and actually creates a data set that contains your data.

Read/Display the data (OleDbDataReader)

Outstanding! You've got data! Now what? Now we have to access the data in the DataReader object and display it on the screen. To accomplish this we will use the GetString method of the DataReader object (this is why it's a good idea to review the Namespaces and classes to see what it available to you before you begin coding). The following code will loop through accessing the datareader object and returning any data:

The first line of code checks to see if we have any data to process. It will set the variable MY-BOOLEAN to a binary "1" (true) if there is data to process, or binary "0" (false) if there is no data to process. The PERFORM loop will use MY-BOOLEAN to determine when to stop processing. The first four lines of code within the PERFORM loop read the data and place it into WORKING-STORAGE variables. The DATAREADEROBJ returns one column at a time identified by the USING phrase and has an offset of zero. The date of birth is a DateTime data type within Pervasive and thus has to be converted to a string before it can be displayed. This is done by first retrieving it in it's native format into a DateTime variable. This variable then invokes its ToString method to convert the data to a displayable format. While there may be other ways in which to display the data, I find it more acceptable to first retrieve the data to a comparable format and then convert the data via that data types methods. Finally we add one to a counter, display the data to the screen and check to see if we have any more data to retrieve.

If we have no other data to retrieve we close the connection, prompt for input from the user and exit the application. These steps can be reviewed in the attached code sample.

Wrap-up

Accessing databases within .NET other than SQL Server is very similar to the manner you use to access SQL Server. I realize that sounds funny but it's true. In accessing SQL Server via the SQLCLIENT namespace and other databases via the OLEDB namespace you perform the same steps:

  1. Establish the connection: Use the SqlConnection or OleDbConnection objects
  2. Create the command: Use the SqlCommand or OleDbCommand objects
  3. Read the data: Use the SqlDataReader or OleDbDataReader objects

The process is the same, the classes/methods are similar and the result is the same: Easy access to data. Hats off to Microsoft for providing a stream-lined data access methodology for not only their database but for others as well.

Review the zip file that goes along with this article. Remember though, you will need to have Pervasive SQL Version 8 SDK installed and a data engine. Feel free to use this code as a template to accessing other databases via OleDB!

Happy Coding!

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Rick Malek
Rick Malek is a Systems Engineer with Fujitsu Software based in Minot, North Dakota. While his primary duties are pre-sales support, Rick also does Consulting and Training. He has worked with COBOL since 1984, originally working on an IBM mainframe with CICS, VSAM and DB2.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Gauge for SharePoint
Become a Sponsor
 Comments
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.