Home

ADO.NET and Paradox

 

Introduction

ADO.NET allows you to create a GUI database application that can connect to almost any type of database used on Microsoft Windows. Besides SQL Server, Microsoft Access, and Oracle, you can also create an application that connects to a Paradox database. This can be done through ODBC as implemented in ADO.NET.

Database Creation

If you plan to create a database application that would use a Paradox database, you can start by opening Paradox. After launching Paradox, locate the directory alias you want to use or create one. For this application, we will use the C:\Programs\FunctionX directory and we had created its alias as Data

After creating or locating the alias and selecting it in Paradox, you can create the necessary table(s) by clicking File -> New -> Table... from Paradox' main menu:

After creating the table, you should save it:

After creating and saving the table, you can enter a few records to test it. To do this, you can first close then open it and click View -> Edit Data from the main menu:

Creating a Data Source

To create a connection to a Paradox database, you can use ODBC as it is featured in ADO.NET. Before doing this, you can create a data source. To do this, you can double-click the Data Source (ODBC) icon from Control Panel. In the ODBC Data Source Administrator, click New. In the the Create New Data Source dialog box, select Microsoft Paradox Driver (*.db):

Click Finish. In the ODBC Paradox Setup dialog box, enter a name in the Data Source Name text box:

Click OK twice.

 

Application Creation

After creating a Paradox table and its ODBC data source, you can start a Windows Forms Application like any other. In its simplest format, you can equip a for with a DataGrid control and a button as follows:

To create a connection to a Paradox table, you can first declare a variable of type OdbcConnection. You can either use its second constructor that takes a string or you can access its ConnectionString property. Either of these allows you to specify how the connection would be carried. Here is an example:

#pragma once


namespace WinForms1
{
	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Data::Odbc;
	using namespace System::Drawing;

	/// <summary> 
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the 
	///          'Resource File Name' property for the managed resource compiler tool 
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public __gc class Form1 : public System::Windows::Forms::Form
	{	
	public:
		Form1(void)
		{
			InitializeComponent();
		}
  
	protected:
		void Dispose(Boolean disposing)
		{
			if (disposing && components)
			{
				components->Dispose();
			}
			__super::Dispose(disposing);
		}
	private: System::Windows::Forms::Button *  btnClose;
	private: System::Windows::Forms::Button *  btnLoad;
	private: System::Windows::Forms::DataGrid *  dataGrid1;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container * components;

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			System::Resources::ResourceManager *  resources = new System::Resources::ResourceManager(__typeof(WinForms1::Form1));
			this->btnClose = new System::Windows::Forms::Button();
			this->btnLoad = new System::Windows::Forms::Button();
			this->dataGrid1 = new System::Windows::Forms::DataGrid();
			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->dataGrid1))->BeginInit();
			this->SuspendLayout();
			// 
			// btnClose
			// 
			this->btnClose->Location = System::Drawing::Point(272, 168);
			this->btnClose->Name = S"btnClose";
			this->btnClose->TabIndex = 5;
			this->btnClose->Text = S"Close";
			this->btnClose->Click += new System::EventHandler(this, btnClose_Click);
			// 
			// btnLoad
			// 
			this->btnLoad->Location = System::Drawing::Point(16, 168);
			this->btnLoad->Name = S"btnLoad";
			this->btnLoad->TabIndex = 4;
			this->btnLoad->Text = S"Load";
			this->btnLoad->Click += new System::EventHandler(this, btnLoad_Click);
			// 
			// dataGrid1
			// 
			this->dataGrid1->AlternatingBackColor = System::Drawing::Color::WhiteSmoke;
			this->dataGrid1->BackColor = System::Drawing::Color::Gainsboro;
			this->dataGrid1->BackgroundColor = System::Drawing::Color::DarkGray;
			this->dataGrid1->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;
			this->dataGrid1->CaptionBackColor = System::Drawing::Color::DarkKhaki;
			this->dataGrid1->CaptionFont = new System::Drawing::Font(S"Tahoma", 8, System::Drawing::FontStyle::Bold);
			this->dataGrid1->CaptionForeColor = System::Drawing::Color::Black;
			this->dataGrid1->DataMember = S"";
			this->dataGrid1->FlatMode = true;
			this->dataGrid1->Font = new System::Drawing::Font(S"Times New Roman", 9);
			this->dataGrid1->ForeColor = System::Drawing::Color::Black;
			this->dataGrid1->GridLineColor = System::Drawing::Color::Silver;
			this->dataGrid1->HeaderBackColor = System::Drawing::Color::Black;
			this->dataGrid1->HeaderFont = new System::Drawing::Font(S"Tahoma", 8, System::Drawing::FontStyle::Bold);
			this->dataGrid1->HeaderForeColor = System::Drawing::Color::White;
			this->dataGrid1->LinkColor = System::Drawing::Color::DarkSlateBlue;
			this->dataGrid1->Location = System::Drawing::Point(8, 8);
			this->dataGrid1->Name = S"dataGrid1";
			this->dataGrid1->ParentRowsBackColor = System::Drawing::Color::LightGray;
			this->dataGrid1->ParentRowsForeColor = System::Drawing::Color::Black;
			this->dataGrid1->SelectionBackColor = System::Drawing::Color::Firebrick;
			this->dataGrid1->SelectionForeColor = System::Drawing::Color::White;
			this->dataGrid1->Size = System::Drawing::Size(344, 152);
			this->dataGrid1->TabIndex = 3;
			// 
			// Form1
			// 
			this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
			this->ClientSize = System::Drawing::Size(360, 198);
			this->Controls->Add(this->btnClose);
			this->Controls->Add(this->btnLoad);
			this->Controls->Add(this->dataGrid1);
			this->Icon = (__try_cast<System::Drawing::Icon *  >(resources->GetObject(S"$this.Icon")));
			this->Name = S"Form1";
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
			this->Text = S"People";
			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->dataGrid1))->EndInit();
			this->ResumeLayout(false);

		}	
	private: System::Void btnLoad_Click(System::Object *  sender, System::EventArgs *  e)
	{
		 OdbcConnection *conPeople = new OdbcConnection(S"DSN=People;DefaultDir=C:\\Programs\\FunctionX;"
														  S"DBQ=C:\\Programs\\FunctionX;");

		String *strSelection = S"SELECT Persons.* FROM Persons";
		OdbcCommand *cmdPeople = new OdbcCommand(strSelection, conPeople);

		OdbcDataAdapter *odaPeople = new OdbcDataAdapter(cmdPeople);

		 DataSet *dsPersons = new DataSet(S"Persons");
		 odaPeople->Fill(dsPersons);

		 this->dataGrid1->DataSource = dsPersons;
		 this->dataGrid1->DataMember = dsPersons->Tables->Item[0]->TableName;

		 conPeople->Open();
		 conPeople->Close();
	}

	private: System::Void btnClose_Click(System::Object *  sender, System::EventArgs *  e)
	{
		 Close();
	}
};
}

Home Copyright © 2005-2010 FunctionX, Inc.