DotNetFirebird.org DotNetFirebird
Using Firebird SQL in .NET.
Friday, January 14, 2005

Creating a Database Programmatically

The maximum you can specify:
Hashtable parameters = new Hashtable();
parameters.Add("User", "SYSDBA");
parameters.Add("Password", "masterkey");
parameters.Add("Database", @"c:\database.fdb");
parameters.Add("Dialect", 3);
parameters.Add("DataSource", "localhost");
parameters.Add("Port", "3050");
parameters.Add("Charset", "NONE");
parameters.Add("PageSize", "8192");
parameters.Add("ForcedWrite", true);
FbConnection.CreateDatabase(parameters);
The miminum you must specify:
Hashtable parameters = new Hashtable();
parameters.Add("User", "SYSDBA");
parameters.Add("Password", "masterkey");
parameters.Add("Database", @"c:\database.fdb");
FbConnection.CreateDatabase(parameters);

See also:

Comments:

Please give a complete function how to create a new database. I tried but got an error:

Unable to complete network request to host "localhost".

My function look like this:

private void btnNewDB_Click(object sender, System.EventArgs e)
{
Hashtable parameters = new Hashtable();
FbConnection myconnect = new FbConnection();
parameters.Add("User", "SYSDBA");
parameters.Add("Password", "masterkey");
parameters.Add("Database", @"Hemekonomi.fdb");
FbConnection.CreateDatabase(parameters);
}
It seems like you don't have Firebird server installed on your machine. You need either to:

1) Install Firebird server on your machine; or
2) Use embedded Firebird by

a) downloading it and copying fbembed.dll to your application's directory (e.g. bin/Debug); and
b) adding the following parameter:

parameters.Add("ServerType", "1");
How can i programitically create the Table. is there any API which are exposed to do this ? Please gimme a code snippet of the doing this task. Thanks in anticipation.

Pranav
hi i am trying to finde an example of how to use
FbScript and FbBatchExecution

i want to create some stored procedures that i have in an file
I'm going to cover batch execution in one of the future posts.
Hello Dan,

Your rundown on getting Firebird (embedded) up and running under .NET has been invaluable.

I am having some difficulty though... (I've got a sample console app pulled together...)

I'm using:
- VB.NET 2003 Standard
- fbembed.dll v1.5.2.4731
- FirebirdSql.Data.Firebird.dll v1.6.3.0
- WinXP Home SP2

+ fbembed is in the EXE directory.

My code looks like (I can't figure out how to get the code formatted... PRE/CODE/etc. won't work in comments)...


Private Function CreateDB() As Boolean
Dim result As Boolean = False

Dim h As New Hashtable
With h
.Add("User", "SYSDBA")
.Add("Password", "masterkey")
.Add("Database", "c:\data\mydb.fdb")

'NOTE: Needed or it tries a server connection instead of embedded connection
.Add("ServerType", 1)
End With

Try
FbConnection.CreateDatabase(h)
result = True
Catch ex As Exception
Console.Out.WriteLine(ex.ToString)
End Try

Return result
End Function


I get the following exception during runtime (with a breakpoint on the Console.Out...):


Run-time exception thrown : System.InvalidCastException - Cast from string "FirebirdSql.Data.Firebird.FbExce" to type 'Integer' is not valid.


If I let the app run, the Exception that gets written to the console is:


FirebirdSql.Data.Firebird.FbException: invalid database handle (no active connection)
at FirebirdSql.Data.Firebird.FbConnection.CreateDatabase(Hashtable values)
at FBembedTest.FBembedTest.CreateDB() in D:\documents\Visual Studio Projects\FBembedTest\FBembedTest.vb:line 43


Any ideas?

(If I come up with anything, I'll be sure to drop you a line.)

Thanks,

Matt
Hello Dan,

Forgive me for being a complete twit...

I revisited the code, and it struck me that I don't have a data folder on C:\... I do on D:\...

I made that change and things worked like a charm!

Thanks!!!

(Now as to how those exceptions could be better... I suppose they might be different in version 1.7 when it comes out. Once it's out of Release Candidate state I suppose I'll give it a try.)
Hello Dan,

I am able to create FB database using below code:

Hashtable parameters = new Hashtable();
parameters.Add("User", UserName);
parameters.Add("Password", Password);
parameters.Add("Database", @FilePath);
parameters.Add("ServerType", 1);

FbConnection.CreateDatabase(parameters);

After creating the database, I am able to open it using FBConnection for the first time. But after that, whenever I open the database, I am getting this error:

internal gds software consistency check (Internal error code (165))

Any idea? Thanks. :)
I'm having the same problem as Victor Tai. The creation & first connection is fine, but if I close my app and restart it, the Connection.Open fails

I've put together a simple C# app (869k) that re-creates the problem
Vitani, Victor Tai--

See my new post: http://www.dotnetfirebird.org/blog/2005/05/problems-with-creating-new-database.html.
Thank-you ever so much Dan, and thank-you again for replying so quickly! It is very much appreciated. Thank-you one more time :)
Can you tell me plea how can I create a table. I've tried doing it programmatically using a create table sql statement but it doesn't seem to work.
Thank you!
Claudiu: Can you post a sample code? What is the error message your get?
it's getting even simpler if we use .NET data provider version 1.7 or later;

FbConnection.CreateDatabase(connection_string);

where will only need a 'connection_string' rather than whole bunch of 'HashTable'..
My application is also embedded by different applications. I put the main dll in the windows dir, but how do I tell it where is the intl dll?
I cannot find an example for creating an embedded DB for 1.7 using a connection string.
I cannot create the embeded DB. I have tried various ways in c# using the connection string is not working. I have used 1.5 hash table and it works great. An example for 1.7 in C# would be great.
The following code will create a database:
//instanciate a new connection string builder
FbConnectionStringBuilder csb = new FbConnectionStringBuilder();
csb.Database = "Mytest.fdb";
csb.UserID = "SYSDBA";
csb.Password = "mykey";
csb.ServerType = 1; //embedded server

//create the database
FbConnection.CreateDatabase(csb.ToString());

The following code will check if a table exists:

FbConnection con = new FbConnection(csb.ToString());

//check if the table named 'test' exists
FbCommand cmd = new FbCommand("SELECT COUNT(RDB$RELATION_NAME)"+
"FROM RDB$RELATIONS WHERE (RDB$RELATION_NAME = 'test')"+
"AND RDB$VIEW_SOURCE IS NULL", con);
con.Open();
int tableCount = (int)cmd.ExecuteScalar();
con.Close();

The tableCount variable will be 0 if the table is not found or 1 if it exists.

The following code will create the table if the table then doesn't exist:

if (tableCount == 0)
{
cmd = new FbCommand("CREATE TABLE \"test\" "+
"(field1 integer not null," +
"field2 char(10),"+
"PRIMARY KEY (field1))",con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}

It's certainly not very neat but I haven't yet found a better way.

Regards


Wayne Phipps
Blog comments are closed.



Previous

Archives