Using FireBird.NET with Mono

by Miguel de Icaza

Today I tried out the Firebird database as we just integrated Carlos' bindings for it into the Mono release. Firebird has two modes of operation, it can be used as a server that clients connect to or it can be used as an embedded database, just like SQLite.

The traditional server/client setup works as expected, but I was more interested in using it as an embedded database, This mode is not very well documented. This blog entry tries to document the steps necessary to embed the database and use it with the managed API.

I installed the "FireBird Classic" configuration as an RPM, which is available here. Other downloads are available here.

These instructions are based on the data I found here and here.

Then you must create a mini-firebird environment for your embedded database, the following commands must be done as root:


	# mkdir /home/miguel/minibird
	# mkdir /home/miguel/minibird/bin
	# cd /home/miguel/minibird
	# cp /opt/firebird/firebird.conf .
	# cp /opt/firebird/firebird.msg .
	# cp /opt/firebird/security.fdb .
	# cp /opt/firebird/bin/fb_lock_mgr bin
	# chmod +rx bin/fb_lock_mgr
	# FIREBIRD=`pwd` /opt/firebird/bin/gsec
	GSEC> add miguel -pw pwd
	GSEC> quit
	# chown -R miguel *
	

Edit the firebird.conf file and add (or edit the commented line) the following line:

	RootDirectory = /home/miguel/minibird

That will create the mini-root and create an account "miguel" with the password "pwd" and set the permissions for all the necessary files.

You can now create a database:


	$ /opt/firebird/bin/isql
	SQL> create database "hola.gdb";
	SQL> create table developers (name varchar (40), email varchar(40) not null);
	SQL> insert into developers (name, email) values ('miguel', '[email protected]');
	SQL> insert into developers (name, email) values ('duncan', '[email protected]');
	SQL> commit;
	SQL> exit;
	

The C# program to access it is fairly simple:

    using System; 
    using System.Data; 
    using FirebirdSql.Data.Firebird;
     
    public class Test {
            public static void Main(string[] args) {
                    string connectionString = 
                    "Database=hola.gdb;servertype=1;user=miguel;password=pwd";
     
                    IDbConnection dbcon = new FbConnection(connectionString); 
                    dbcon.Open(); 
                    IDbCommand dbcmd = dbcon.CreateCommand(); 
                    string sql = "SELECT * FROM developers"; 
                    dbcmd.CommandText = sql; 
                    IDataReader reader = dbcmd.ExecuteReader(); 
                    while(reader.Read()) {
                            object dataValue = reader.GetValue(0); 
                            string sValue = dataValue.ToString(); 
                            Console.WriteLine("Value: " + sValue);
                    } 
                    
                    // clean up 
                    reader.Close(); 
                    reader = null; 
                    dbcmd.Dispose(); 
                    dbcmd = null; 
                    dbcon.Close(); 
                    dbcon = null;
            }
    }

Build your program:


	$ mcs sample.cs -r:FirebirdSql.Data.Firebird -r:System.Data

To run this program you must set the FIREBIRD environment variable to point to the mini-firebird directory:


	$ FIREBIRD=/home/miguel/minibird mono sample.exe
	Value: miguel
	Value: duncan

Posted on 30 Sep 2005