Meditation and Mindfulness for Software / IT Professionals. Conducted by Bipin Joshi in Thane. Read more...

<%@ Page %>

Database Access using COBOL.NET and .NET Classes

Introduction

In the last article of this series we saw how to use .NET namespaces and classes in COBOL.NET. Continuing this further this article shows how to work with Databases and web controls using COBOL.NET. More specifically it will show how to displays data in a windows forms Datagrid using COBOL.NET code. As in previous case we will be using Fujitsu's NetCOBOL compiler for .NET. You can download it from their web site.

COBOL Classes and .NET Base Classes

The following COBOL code uses the .NET Base Classes:
  • System.Data.SQLClient - to handle Database.
  • System.Drawing - for various sizes and fonts.

Source Code

Open VS.NET and select COBOL Projects -> Console Application. Alternatively, you can also write it in Notepad and save the file as codedb.cob. Write the code snippet given below:
000010 CLASS-ID. SDBASE INHERITS Form.
000020 ENVIRONMENT DIVISION.
000030 CONFIGURATION SECTION.
000040 REPOSITORY.
000050     CLASS Form AS "System.Windows.Forms.Form"
000060     CLASS MessageBox AS 
		"System.Windows.Forms.MessageBox"
000070     CLASS Application AS 
		"System.Windows.Forms.Application"
000080     CLASS Container AS 
	"System.ComponentModel.Container"
000090     CLASS Button AS 
	"System.Windows.Forms.Button"
000100     CLASS DataGrid AS 
	"System.Windows.Forms.DataGrid"
000110     DELEGATE EventHandler AS 
	"System.EventHandler"
000120     CLASS EventArgs AS "System.EventArgs"
000130     CLASS Point AS "System.Drawing.Point"
000140     CLASS Drawing-size AS "System.Drawing.Size"
000150     CLASS Font AS "System.Drawing.Font"
000160     CLASS Sys-Object AS "System.Object"
000170     CLASS Sys-Boolean AS "System.Boolean"
000180     CLASS SqlConnection AS 
		"System.Data.SqlClient.SqlConnection"
000190     CLASS SqlDataAdapter AS 
		"System.Data.SqlClient.SqlDataAdapter"
000200     CLASS DataSet AS "System.Data.DataSet"	
000210     CLASS Sys-Type AS "System.Type"
000220     CLASS Control-Collection AS 
	"System.Windows.Forms.Control+ControlCollection"
000230     PROPERTY Prop-Enabled AS "Enabled"
000240     PROPERTY Prop-Location AS "Location"
000250     PROPERTY Prop-Size AS "Size"
000260     PROPERTY Prop-TabIndex AS "TabIndex"
000270     PROPERTY Prop-Font AS "Font"
000280     ENUM Font-Style AS "System.Drawing.FontStyle"
000290     PROPERTY Prop-Bold AS "Bold"
000300     PROPERTY Prop-Text AS "Text"
000310     PROPERTY App-Controls AS "Controls"
000320     PROPERTY Prop-AutoScaleBaseSize AS 
		"AutoScaleBaseSize"
000330     PROPERTY Prop-ClientSize AS "ClientSize"
000340     PROPERTY Prop-DataMember as "DataMember"
000350     PROPERTY Prop-Name as "MyDatagrid"
000360     PROPERTY DataSource as "DataSource"
000370     PROPERTY DataGrid1.
000380     
000390*Main Block of code
000400 STATIC.
000410 PROCEDURE DIVISION.
000420 METHOD-ID. MAIN.
000430 DATA DIVISION.
000440 WORKING-STORAGE SECTION.
000450 01 OBJ OBJECT REFERENCE SDBASE.
000460 PROCEDURE DIVISION.
000470     INVOKE SDBASE "NEW" RETURNING OBJ.
000480     INVOKE Application "Run" USING BY VALUE OBJ.
000490 END METHOD MAIN.
000500 END STATIC.
000510 
000520*
000530 OBJECT.
000540 DATA DIVISION.
000550 WORKING-STORAGE SECTION.
000560 01 COMPONENTS OBJECT REFERENCE Container.
000570 01 Button1 OBJECT REFERENCE Button.
000580 01 DataGrid1 OBJECT REFERENCE DataGrid.
000590 PROCEDURE DIVISION.
000600
000610*Procedure New
000620 METHOD-ID. NEW.
000630 DATA DIVISION.
000640 WORKING-STORAGE SECTION.
000650 PROCEDURE DIVISION.
000660     INVOKE Container "NEW" RETURNING COMPONENTS.
000670     INVOKE SELF "InitializeComponent".
000680 END METHOD NEW.
000690 
000700*Procedure Dispose
000710 METHOD-ID. DISPOSE AS "Dispose" PROTECTED OVERRIDE.
000720 DATA DIVISION.
000730 WORKING-STORAGE SECTION.
000740 01 FLAG PIC 1 USAGE BIT.
000750 LINKAGE SECTION.
000760 01 DISPOSING OBJECT REFERENCE Sys-Boolean.
000770 PROCEDURE DIVISION USING BY VALUE DISPOSING.
000780     SET FLAG TO DISPOSING.
000790     IF FLAG = B"1" AND COMPONENTS NOT 
	= NULL THEN
000800         INVOKE COMPONENTS "Dispose"
000810     END-IF.
000820     INVOKE SUPER "Dispose" USING 
	BY VALUE DISPOSING.
000830 END METHOD DISPOSE.
000840
000850*Procedure Initialize Component
000860 METHOD-ID. InitializeComponent AS 
		"InitializeComponent".
000870 DATA DIVISION.
000880 WORKING-STORAGE SECTION.
000890 01 CLICK-EVENT OBJECT REFERENCE EventHandler.
000900 01 BOLD-Font OBJECT REFERENCE Font-Style.
000910 PROCEDURE DIVISION.
000920     INVOKE Button "NEW" RETURNING Button1.
000930     INVOKE DataGrid "NEW" RETURNING Datagrid1.
000940     INVOKE SELF "SuspendLayout".
000950
000960*Set the Properties for the Button
000970     SET Prop-Location OF Button1 TO 
		Point::"NEW" (40, 10).
000980     SET Prop-Size OF Button1 TO 
		Drawing-size::"NEW" (100, 28).
000990*Set the Properties for the Button     
001000     SET Prop-Location OF DataGrid1 
		TO Point::"NEW" (40, 74).
001010     SET Prop-Size OF DataGrid1 TO 
		Drawing-size::"NEW" (200, 200).
001020*     
001030     MOVE 0 TO Prop-TabIndex OF Button1.
001040     SET Prop-Text OF Button1 TO "Click".
001050     SET BOLD-Font TO Prop-Bold OF Font-Style.
001060     SET Prop-Font OF Button1 TO
001070         Font::"NEW" 
	("Verdana", 10.0, BOLD-Font).
001080     INVOKE EventHandler "NEW" 
		USING BY VALUE SELF "Button1_Click"
001090         RETURNING CLICK-EVENT.
001100     INVOKE Button1 "add_Click" USING 
	BY VALUE CLICK-EVENT.
001110     INVOKE App-Controls OF SELF "Add" USING 
	BY VALUE Button1.
001120     INVOKE App-Controls OF SELF "Add" USING 
	BY VALUE DataGrid1.
001130     SET Prop-Text OF SELF 
	TO "Dataaccess".
001140     SET Prop-AutoScaleBaseSize OF SELF TO 
		Drawing-size::"NEW" (5, 13).
001150     SET Prop-ClientSize OF SELF TO 
		Drawing-size::"NEW" (292, 274).    
001160*
001170
001180     INVOKE SELF "ResumeLayout" USING 
	BY VALUE B'0'.
001190 END METHOD InitializeComponent.
001200
001210*Procedure for Button_Click
001220 METHOD-ID. Button1_Click 
	AS "Button1_Click".
001230 DATA DIVISION.
001240
001250 WORKING-STORAGE SECTION.
001260*Declaring Objects
001270 01 myConnection OBJECT 
	REFERENCE SqlConnection.
001280 01 myDA OBJECT 
	REFERENCE SqlDataAdapter.
001290 01 myDS OBJECT 
	REFERENCE DataSet.
001300 01 myType OBJECT 
	REFERENCE Sys-Type.
001310
001320 LINKAGE SECTION.
001330 01 sender OBJECT REFERENCE Sys-Object.
001340 01 e OBJECT REFERENCE EventArgs. 
001350  PROCEDURE DIVISION USING 
	BY VALUE sender e.
001360*Instantiating the Objects
001370     INVOKE SqlConnection "NEW" 
001380                         USING BY VALUE 
	"Data Source=localhost;uid=sa;password=;
	Database=northwind;" 
	RETURNING myConnection.
001390                                            
001400     INVOKE SqlDataAdapter "NEW" 
001410                         USING BY VALUE
	"Select * From Products" myConnection 
	RETURNING myDA.
001420    
001430     INVOKE DataSet "NEW" RETURNING myDS.
001440     INVOKE myDA "Fill" USING BY VALUE myDS.
001450     SET DataSource OF DataGrid1 TO myDS.
001460 END METHOD Button1_Click.
001470*
001480 END OBJECT.
001490*
001500 END CLASS SDBASE.
001510

Compiling the code

You can compile the project from VS.NET or if you are using command line compiler enter following command at command prompt:
cobolc
/reference:System.Windows.Forms.DLL,
System.Drawing.DLL,
System.Data.DLL,
System.DLL 
/main:SDBASE,MAIN codedb.cob
Now run the application go to the specific folder and start the .exe generated.

I hope you must have found the articles interesting. See you again!




Sushila Bowalekar, is presently working as Training Consultant in Mumbai, India. Her skill set includes HTML, JavaScript, VBScript, ASP, Site Server Commerce Edition 3.0 and presently she is working on .NET.

Posted On : 12 Jul 2002



Tags : Data Access .NET Framework Visual Studio