Click here to Skip to main content
15,884,986 members
Articles / Desktop Programming / Windows Forms

Interbase and Visual C# .NET

Rate me:
Please Sign up or sign in to vote.
4.52/5 (12 votes)
11 Jun 2008CPOL2 min read 55.5K   3.8K   13   6
A solution managing InterBase DB's with C# .NET

Introduction

In a university project I had to manage an interbase, Oracle and SQL Server database with C#. Oracle and SQL Server was easy but when I was searching for some information about connecting C# with Interbase, it was like hell. I had to read a lot of articles because I had never done it. But a few days later, bingo!

Background

The first problem faced was to find the correct database Driver. I was searching a lot and found IBProvider (free version doesn't work), ADO 1.0 (IB Driver). Then I found the ADO 2.0 Driver (only works with Windows XP and IB2007). ADO 2.0 from IB let me take a breath because I thought that I'll never find it.

The zip file includes ADO.NET 2.0 Driver for CodeGear Interbase, information about how it works. But the second problem was "how does it really work?" First install the driver, then Edit the machine.config file from the C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG directory.

Add the following line in the section labelled <DbProviderFactories>:

XML
<add name="InterBase ADO.NET 2.0 Data Provider" invariant="Borland.Data.AdoDbxClient" 
	description=".Net Framework Data Provider for InterBase" 
	type="Borland.Data.TAdoDbxProviderFactory, Borland.Data.AdoDbxClient, 
	Version=11.0.5000.0, Culture=neutral, PublicKeyToken=91d62ebb5b0d1b1b"/>

Start Visual Studio 2005.
File new C# Windows application.
Project- Add Reference and add the AdoDbxCleint.dll to your project.

Using the Code

The file spClass includes 2 classes:

  • spClass contains two methods (1 overloaded)
  • axUsu contains eight methods (created to understand how this driver works)

spClass methods are the ones that get the TAdoDbxCommand (get the connection and send a simple query). getDataIB gets the result of a query (like SELECT * FROM...)

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Borland.Data;
using Borland.Vcl;
using Borland.Data.Units;
using System.Data.SqlClient;
using System.Data.Common;
class spClass
    {
        public static TAdoDbxCommand getComdIB()
        { 
           //The string connection can change
            TAdoDbxConnection c = new TAdoDbxConnection(
                "DriverName=Interbase;Database=192.168.1.101:C:\\" +
                "Farmacia\\Farmacia.gdb;RoleName=RoleName;User_Name=sysdba;" +
                "Password=masterkey;SQLDialect=3;" +
                "MetaDataAssemblyLoader=Borland.Data.
			TDBXInterbaseMetaDataCommandFactory," +
                "Borland.Data.DbxReadOnlyMetaData,Version=11.0.5000.0,Culture=neutral," +
                "PublicKeyToken=91d62ebb5b0d1b1b;GetDriverFunc=getSQLDriverINTERBASE;" +
                "LibraryName=dbxint30.dll;VendorLib=GDS32.DLL");
            TAdoDbxCommand cmd = new TAdoDbxCommand();
            cmd.Connection = c;
            return cmd;
        }
        public static TAdoDbxCommand getComdIB(string query)
        {
            TAdoDbxConnection c = new TAdoDbxConnection(
                "DriverName=Interbase;Database=192.168.1.101:C:\\Farmacia\\" +
                "Farmacia.gdb;RoleName=RoleName;User_Name=sysdba;Password=masterkey;" +
                "SQLDialect=3;MetaDataAssemblyLoader=" +
                "Borland.Data.TDBXInterbaseMetaDataCommandFactory," +
                "Borland.Data.DbxReadOnlyMetaData,Version=11.0.5000.0," +
                "Culture=neutral,PublicKeyToken=91d62ebb5b0d1b1b;" +
                "GetDriverFunc=getSQLDriverINTERBASE;LibraryName=dbxint30.dll;" +
                "VendorLib=GDS32.DLL");
            TAdoDbxCommand cmd = new TAdoDbxCommand();
            cmd.Connection = c;
            cmd.CommandText = query;

            return cmd;
        }
        public static DataTable getDataIB(DbCommand cmd)
        {
            cmd.Connection.Open();
            DbDataReader myreader = cmd.ExecuteReader();
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            ds.Tables.Add(dt);
            ds.Load(myreader, LoadOption.PreserveChanges, ds.Tables[0]);
            myreader.Close();
            return ds.Tables[0];
        }
    }        

Now we have done the essential method to get a IBCommand, and a Datatable.

axUsu class includes methods to insert, delete, update, get Users, from a database named Farmacia.dgb in a remote server but we can focus on one method: Insert method(addUsr):

C#
public void addUsr(string usr, string pwd, string nombre, string nivel)
        {
            string query = "INSERT INTO USUARIOS(USR, PWD, NOMBRE, NIVEL)"
                + "VALUES(" + usr + "," + pwd + "," + nombre + "," + nivel + ")";
            TAdoDbxCommand cmd = spClass.getComdIB(query);
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            cmd.Connection.Close();
        }

In this method, we can identify an TAdoDbxCommand object named cmd that has an initial value from spClass.getComdIB(query). It means calling the method and getting the cmd object with connection and commandText properties initialized. Then open connection, execute the query and close connection, we have made a successful query.

If we want to get all the users in the table named users in a Windows Form, we can get them by writing this code:

C#
axUsu users = new axUsu();

DataTable UsAll= users.getUsrAll();
dataGridView1.DataSource = UsAll;

Insert method:

C#
axUsu addUsr = new axUsu();
addUsr.addUsr("User1",axUsu.cifrarSHA1("Password"), "Name", 1);

It's easy when we have completed the methods. I hope it will be useful.

History

  • 11th June, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems / Hardware Administrator
Mexico Mexico
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionProblem with Update the DB Pin
Member 1348419912-Nov-17 21:24
Member 1348419912-Nov-17 21:24 
GeneralCannot find dbxInt30.dll Pin
G.Reimers19-Sep-13 22:46
G.Reimers19-Sep-13 22:46 
QuestionHow to write the binary data to the database? Pin
fion4527-Jun-11 17:03
fion4527-Jun-11 17:03 
GeneralDo something with the formatting the connection string is a few miles long! Pin
Zoltan Balazs11-Jun-08 20:27
Zoltan Balazs11-Jun-08 20:27 
GeneralNothing new Pin
PIEBALDconsult11-Jun-08 17:18
mvePIEBALDconsult11-Jun-08 17:18 
GeneralRe: Nothing new Pin
pechan0000011-Jun-08 17:22
pechan0000011-Jun-08 17:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.