DotNetFirebird.org DotNetFirebird
Using Firebird SQL in .NET.
Wednesday, March 09, 2005

Batch SQL/DDL Execution

The FirebirdSql.Data.Firebird.Isql namespace provides classes for batch SQL/DDL execution. This allows you to make such operations as:

  • creating the database structure programmatically
  • updating the database structure automatically
  • automating the maintenance procedure
  • etc.

Running a batch script is not difficult. First you need to parse the script using FbScript class:

FbScript script = new FbScript("employee.sql");
script.Parse();

The result is stored in script.Results, one item per command. Now we can use FbBatchExecution class to run the script:

FbConnection c = new FbConnection(@"Database=employee.fdb;User=SYSDBA;Password=masterkey");
c.Open();

FbBatchExecution fbe = new FbBatchExecution(c);
foreach (string cmd in script.Results) {
   fbe.SqlStatements.Add(cmd);
}

fbe.Execute();
c.Close();

By default, the transaction is committed after each command. You can disable this by calling fbe.Execute(false).

Here is a complete example that uses an embedded Firebird to create a new employee.fdb database and initialize the structure using a DDL script. The DDL script is generated from the example employee.fdb (it's modified on two places because of the current bugs in the provider, see the Known Issues below).

On my Athlon XP 2000+ the execution of this batch script takes 0.921 seconds - which is very good I guess. 

Known Issues

  • The current 1.7 RC2 version of ADO.NET provider has some bugs that prevent the example from runnin. It's fixed in the CVS. The example download contains a compiled current CVS snapshot. Recognition of 'CREATE DESCENDING INDEX' is added.
  • Even the current CVS version has problems with '--' inside a string (it thinks that it's a line comment). For now I've changed all '--' occurences in the strings to '-'.

Update

The issues are already fixed because my changes were incorporated into the provider.

Download

  • Example source - 1.6 MB (includes also the VS.NET project, compiled binary, fixed Firebird ADO.NET provider binary, employee.sql DDL script and embedded Firebird runtime with error messages file)

Related

Comments:

I have given a try and it works.

I would like the newly created fb file to
be released after the connection.Close() statement but it is not (unable to rename or delete it for example)
The file is released only when the application finished.
Any solution ?

Thanks
Vincent QUILLET
This is caused by connection pooling. You need either to:

1) turn off the connection pooling using Pooling=false in the connection string;

or

2) close all connections when you are finished using FbConnection.ClearPool() or FbConnection.ClearAllPools(().
Thank you very much !
I have tried to set Pooling=false and it's OK.
Now I can't get working character set like ISO8859_1 or DOS863.
connection.Open() throws following exception :
bad parameters on attach or create database\nCHARACTER SET ISO8859_1 is not defined

Setting parameter to 'ASCII' or 'UNICODE_FSS' works fine.

Any idea ?

Thanks

Vincent QUILLET
When using any of the special charsets with embedded Firebird you need to include also the "intl/fbintl.dll" library (remember to put it into the "intl" subdirectory of your app's directory).
This post has been removed by a blog administrator.
I always have trouble finding C# code samples for Firebird.

Here is a link to my an article on my Blog which includes examples:
Run an embedded SQL Script against Firebird

Your Blog helped me on my way.

Many thanks.


Wayne Phipps
When I use FbBatchExecution to execute insert statment I got "Execut exception System.NullReferenceException: Object reference not set to an instance of an object...."
If I execute create table statement I won't get this message.

Any help appreciated
Blog comments are closed.



Previous

Archives