Connecting to InterBase from Visual Studio

From InterBase
Jump to: navigation, search

Go Up to Main Page

InterBase currently supports an ADO.Net driver that works with InterBase XE, XE3, and XE7 with both 32-bit and 64-bit support.

This topic covers the basic information for connecting to InterBase from Visual Studio:

Prerequisites

You will need the following:

  • .Net 2.0 SDK with update
  • Microsoft Visual Studio 2005 or above
  • InterBase XE or above

Installation Instructions

For instructions on running the ADO.Net 2.0 installer, click here.

Usage Instructions

  1. Start Visual Studio 2005/2008.
  2. File new C# Windows application.
  3. Project - Add Reference and add the AdoDbxClient.dll, DbxCommonDriver, DBXInterBaseDriver to your project.
  4. Add a DataGridView component to your Windows form.
  5. The sample code below fills a DataGridView component with the contents of the employee table of the employee.gdb sample InterBase database:


Example Code

This sample code was used with InterBase XE7 and Visual Studio 2013 Ultimate.

>>>
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.Data.Units;
using System.Data.SqlClient;
using System.Data.Common;
namespace IBXEApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            ReadData(getConnection());
        }
        public DbConnection getConnection()
        {
           // DbProviderFactory factory = DbProviderFactories.GetFactory
             //            ("Borland.Data.AdoDbxClient");
            DbConnection c = new TAdoDbx{{Product}}Connection();
            //DbConnection c = factory.CreateConnection();
            c.ConnectionString = "Database=C:\\Embarcadero\\{{Product}}\\examples\\database\\employee.gdb;User_Name=sysdba;Password=masterkey";
            return c;
        }
        public void ReadData(DbConnection conn)
        {
            string sql = "select * from employee";
            DbCommand cmd = conn.CreateCommand();
            cmd.CommandText = sql;
            conn.Open();
            DbDataReader myreader = cmd.ExecuteReader();
            dataGridView1.DataSource = myreader;
            DataSet ds = new DataSet();
            DataTable dt = new DataTable("employee");
            ds.Tables.Add(dt);
            ds.Load(myreader, LoadOption.PreserveChanges, ds.Tables[0]);
            dataGridView1.DataSource = ds.Tables[0];
            myreader.Close();
        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
   }
}
<<<