Keywords: Visual Studio 2008, VS 2005, DDL queries, metadata schemas, ODBC Escape Sequences, InterBase ODBC, Firebird ADO Net provider, C#, connect InterBase
Database metadata schemes
The important part of all the OLE DB providers is the metadata schemes.
They are used by clients for getting the database description: list of stored procedures, table structures, registered domens,
limitations, primary and external keys, etc.
To work with ADO.Net Library OLE DB provider should support OLE DB schemes, as the library components often use this information.
It’s possible to request a specific scheme by its name. For this an OleDbConnection object has a GetSchema() method.
In Net 2 there is a GetOleDbSchema() method which takes one of the OleDbSchemaGuid enum values as an argument.
Both modes behave the same way and back absolutely similar DataTable copies with an information set of the scheme.
Each scheme has a set of columns, with the help of which it is possible to filter the returned result. For example, in COLUMN scheme
is it possible to make limitations for the following fields:
Restriction columns: TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME.
If we want to get the description of all the columns for EMPLOYEE table we have to use COLUMNS scheme this way:
This type of queries enables to manage database metadata. You may create, drop and alter the columns,
tables and whole databases via SQL — expressions, in the text of those there are DDL instructions.
If to use them together with metadata schemes it would be possible
to copy the existed database structures and create new ones without any effort.
All DDL queries, excluding CREATE DATABASE and DROP DATABASE, required transaction (in manual on in auto commit mode).
By default the auto commit mode for DDD queries is disabled. It is made due to the reason of safety.
To turn on the confirmation of DDL queries it is necessary to setup auto_commit_ddl property.
Its description you may find in the chapter of this article
called Controlling transactions automatically in IBProvider.
I will give the example of DROP DATABASE use for database dropping:
privatevoid DropDatabase()
{
if (File.Exists(databasePath))
{
OleDbConnectionStringBuilder builder =
CreateConnectionStringBuilderForSample();
//turn off connection pool
builder.OleDbServices = OleDbServicesValues.EnableAll &
~OleDbServicesValues.ResourcePooling;
OleDbConnection con = new OleDbConnection(builder.ToString());
con.Open();
new OleDbCommand(«drop database»,con).ExecuteNonQuery();
con.Close();
}
}
Pay attention to the fact that for connecting that will be used for database dropping, we turn off any pooling services.
IBProvider can inform about OLE DB services that became unavailable, connections, and in this case that action is excessive.
But it is left here for solving different problems while using OLE DB providers of other manufacturers:
And, finally, the finished example that firstly drops database,
then creates the new one on its place and specifies two tables connected by external key in it:
publicvoid CreateNewDBSample()
{
DropDatabase();
OleDbConnection con = CreateDatabase();
con.Open();
OleDbTransaction trans = con.BeginTransaction();
//create SAMPLE_TABLE with 2 columns
ExecuteDDL(
«CREATE TABLE SAMPLE_TABLE( « + //int column » ID INTEGER NOT NULL, « + //varchar column» NAME VARCHAR(64), « + //primary key«CONSTRAINT PK_SAMPLE_TABLE PRIMARY KEY(ID) )», trans);
//create SAMPLE_TABLE_2 referenced via FOREIGN KEY
ExecuteDDL(
«CREATE TABLE SAMPLE_TABLE_2 ( « +
» ID INTEGER NOT NULL, « + //int columns» PARENT INTEGER NOT NULL, « + //int column«CONSTRAINT PK_SAMPLE_TABLE_2 PRIMARY KEY(ID), « + //primary key«CONSTRAINT FK_SAMPLE_TABLE_PARENT « + //foreign key«FOREIGN KEY(PARENT) REFERENCES SAMPLE_TABLE(ID))», trans);
trans.Commit();
con.Close();
}
In latest version of IBProvider v3 you can create database without DDL and database connection
Escape Sequences enable to upgrade the query text in the process of implementation. Sequence is included into the query text in braces.
For example, {fn CURDATE} — will be upgraded into the server time value.
In ODBC escape sequences for the following characteristics are specified:
Work with time and dates
Type conversion fucntions
Calling stored procedures
others
This extension is widely used by such tools as MS SQL Server, Business Intelligence, Crystal Reports,
and their support by OLE DB provider gives the possibility of their mutual use.
Moreover, it enables to write queries that do not depend on database.
There are escape sequences in MS SQL, Oracle and many other database servers.
To turn on the ODBC extensions support to IBProvider it is necessary to setup the following
initialization property: support_odbc_query = true.
On default the support is off. Further information about ODBC escape sequences,
supported functions you will find in ODBC Escape Sequences Guide.
The following example demonstrates the use of escape sequences in SQL queries text:
publicvoid ODBCQueriesTest()
{
OleDbConnectionStringBuilder builder =
ConnectionProvider.GetConnectionStringBuilderFromUDL();
builder.Provider = «LCPI.IBProvider.2»;
builder.Add(«support_odbc_query»,«true»);
OleDbConnection con = new OleDbConnection(builder.ToString());
con.Open();
OleDbTransaction trans = con.BeginTransaction();
//select current day name
OleDbCommand cmd = new OleDbCommand(
«select « +
«{fn dayname({fn now()})} as DAY_NAME,» +
«{fn dayofweek({fn now()})} as DAY_OF_WEEK,» +
«{fn dayofmonth({fn now()})} as DAY_OF_MONTH,» +
«{fn dayofyear({fn now()})} as DAY_OF_YEAR « +
«from RDB$DATABASE», con, trans);
using (OleDbDataReader rdr = cmd.ExecuteReader())
if (rdr.Read())
for (int i = 0; i < rdr.FieldCount; i++)
Console.WriteLine(rdr.GetName(i) + «: « + rdr[i].ToString());
trans.Commit();
con.Close();
}
Conclusion
In my guide with the example of OLE DB provider I reviewed the most often used possibilities of ADO.Net Library.
The main advantage of OLE DB Providers in front of escape ones (.Net Data Providers)
is the possibility to use them not only in .Net Framework but also in practically any context supporting COM.
I hope that this guide enables you to widen the range of possibilities used in ADO.Net not only for work with Firebird but also with other databases.
Publish date: 2008-04-01. Copyright: IBProvider. This material may be reproduced on other web sites, without written permission but link https://www.ibprovider.com/eng required.