Search code examples
c#.netfirebirdfirebird-embedded

Unable to Access Embedded Firebird Database Server with .Net Client


Unable to Access Embedded Firebird Database Server with .Net Client

I aim to develop a program which uses the Embedded Firebird Server but face errors when trying to make a connection using the .Net client. I have followed the advice from multiple threads on making it work, but I can’t figure it out. I’ve tried changing the connection string and files but keep getting the same errors.

Here is a detailed explanation of my research and everything I have tried so far:

  1. How to connect and use Firebird db embedded server with Visual C# 2010

    I download the files specified in the links, followed the steps and ran the code and got the same error message as did the original poster:

    FirebirdSql.Data.FirebirdClient.FbException Message=Unable to complete network request to host "127.0.0.1".

    Using "localhost" in place of the IP generates the same error.

    The accepted answer is to make sure all the .dll and config files are copied to my project files (directory with the code ) and the output directory (bin/debug), which I have done. I copied over every file in the zip folder. Additionally @Robin Van Persi states to not use the “compact .Net data provider”, how do I know if I am using this? I downloaded the file from the link in the question.

    Another answer contributed by @PlageMan is to add ServerType=1; to the connection string and remove the DataSource and Port attributes which produces these errors:

    FbConnection con = new FbConnection("User=SYSDBA;Password=masterkey;Database=TEST.FDB;Dialect=3;Charset=UTF8;ServerType=1;");
    

    System.NotSupportedException Message=Specified server type is not correct.

    FbConnection con = new FbConnection("User=SYSDBA;Password=masterkey;Database=TEST.FDB;Dialect=3;Charset=UTF8;");
    

    System.ArgumentException Message=An invalid connection string argument has been supplied or a required connection string argument has not been supplied.

    The last answer by @Toastgeraet adds to rename the fbembed.dll to either fbclient.dll or gds32.dll. I’ve tried it all three ways, no change. In fact http://www.firebirdsql.org/en/firebird-net-provider-faq/ says fbembded.dll but that didn’t work either.

  2. How to solve connection error in c# while using firebird embeded database?

    Has the same advice on renaming the fbembed.dll to fbclient.dll which didn’t work for the original poster either. The accepted answer ServerType=1 in the connection string, but a comment under @cincura.net answer gave me a new possibility to investigate; processor architecture. Unfortunately, switching between 64bt and 32bit versions didn’t make any difference.

    I thought switching to the 32bit version might have been the answer since I'm using Visual Studios Express.

  3. Error in using Embeded Firebird

    The last comment in this thread is another person saying that changing to 32bit didn’t solve the problem either.

  4. Trying to use the firebird embedded server - Specified server type is not correct

    I went back to looking up information on the ServerType because I have seen it as 1 and 0 and found this post. @Nigel answer is to update to the latest version of the .Net provider. Unfortunately, I can’t figure out how to use the newest version on the Firebird website (4.5.1.0) because it lacks FirebirdSql namespace from the examples. Additionally, Visual Studios gives me some warning about the wrong version of .Net being targeted when I import it.

What am I doing wrong? Do I need to use a different connection string or new version of Firebird/ .Net provider? Am I missing something else?

I realize that this question may be considered a duplicate, but none of the answers I have found so far have solved my issue. Additionally, the previous StackOverflow Q/A’s which I have cited above are all years old so I’m hoping someone may have new information to share.


Solution

  • I just created a very basic program to test Firebird embedded from C#. You need to add the latest FirebirdSql.Data.FirebirdClient nuget package (4.5.1.0), and drop the entire contents of a Firebird embedded zip kit into the same folder as the .exe.

    Note that you need to match the bitness of the application:

    AnyCPU seems to be rather tricky. When I compiled the executable as AnyCPU and ran it on a 64 bit machine, it gave a BadImageFormatException when combined with Firebird Embedded 64 bit, but worked with Firebird Embedded 32 bit; which is the opposite of what I expected.

    class Program
    {
        private const string DefaultDatabase = @"D:\data\db\employee.fdb";
    
        static void Main(string[] args)
        {
            string database = args.Length > 0 ? args[0] : DefaultDatabase;
            var test = new TestEmbedded(database);
    
            test.RunTestQuery();
    
            Console.ReadLine();
        }
    }
    
    class TestEmbedded
    {
        private readonly string connectionString;
    
        public TestEmbedded(string database)
        {
            var connectionStringBuilder = new FbConnectionStringBuilder();
            connectionStringBuilder.Database = database;
            connectionStringBuilder.ServerType = FbServerType.Embedded;
            connectionStringBuilder.UserID = "sysdba";
    
            connectionString = connectionStringBuilder.ToString();
    
            Console.WriteLine(connectionString);
        }
    
        internal void RunTestQuery()
        {
            using (var connection = new FbConnection(connectionString))
            using (var command = new FbCommand("select 'success' from RDB$DATABASE", connection))
            {
                Console.WriteLine("Connecting...");
                if (connection.State == System.Data.ConnectionState.Closed) 
                { 
                    connection.Open(); 
                }
    
                Console.WriteLine("Executing query");
                using (var reader = command.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        Console.WriteLine(reader.GetString(0));
                    }
                }
            }
        }
    }
    

    The connection string generated by this program is:

    initial catalog=D:\data\db\employee.fdb;server type=Embedded;user id=sysdba
    

    This seems to be the minimum required to connect. Note that although Firebird embedded on windows doesn't perform any authentication, providing user id is required, otherwise the trusted authentication is triggered which doesn't work with Firebird embedded.