无法使用.Net客户端访问嵌入式Firebird数据库服务器

时间:2014-10-03 13:09:00

标签: c# .net firebird firebird-embedded

无法使用.Net客户端访问嵌入式Firebird数据库服务器

我的目标是开发一个使用嵌入式Firebird服务器的程序,但在尝试使用.Net客户端建立连接时会遇到错误。我已经按照多个线程的建议来使其工作,但我无法弄明白。我已经尝试更改连接字符串和文件但仍然遇到相同的错误。

以下是我的研究的详细解释以及迄今为止我尝试过的所有内容:

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

    我下载链接中指定的文件,按照步骤运行代码并获得与原始海报相同的错误消息:

      

    FirebirdSql.Data.FirebirdClient.FbException   消息=无法完成主机“127.0.0.1”的网络请求。

    使用“localhost”代替IP会产生相同的错误。

    接受的答案是确保将所有.dll和配置文件复制到我的项目文件(带代码的目录)和输出目录(bin / debug),这是我所做的。我复制了zip文件夹中的每个文件。另外@Robin Van Persi表示不使用“紧凑型.Net数据提供商”,我怎么知道我是否使用它?我从问题中的链接下载了该文件。

    @PlageMan提供的另一个答案是添加ServerType = 1;连接字符串并删除产生这些错误的DataSource和Port属性:

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

    System.NotSupportedException消息=指定的服务器类型不正确。

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

    System.ArgumentException   Message =提供了无效的连接字符串参数,或者未提供必需的连接字符串参数。

    @Toastgeraet的最后一个答案添加了将fbembed.dll重命名为fbclient.dll或gds32.dll。我已经尝试了三种方式,没有改变。实际上http://www.firebirdsql.org/en/firebird-net-provider-faq/表示fbembded.dll,但这也不起作用。

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

    对将fbembed.dll重命名为fbclient.dll也有同样的建议,这对于原始海报也不起作用。 连接字符串中接受的答案ServerType = 1,但@ cincura.net下的评论给了我一个新的调查可能性;处理器架构。不幸的是,在64bt和32bit版本之间切换没有任何区别。

    我认为自从我使用Visual Studios Express以来,切换到32位版本可能就是答案。

  3. Error in using Embeded Firebird

    这个帖子的最后一个评论是另一个人说改为32位也没有解决问题。

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

    我回去查找有关ServerType的信息,因为我已将其视为1和0并找到了这篇文章。 @Nigel的回答是更新到.Net提供商的最新版本。不幸的是,我无法弄清楚如何在Firebird网站(4.5.1.0)上使用最新版本,因为它缺少示例中的FirebirdSql命名空间。另外,当我导入时,Visual Studios会给我一些关于.Net的错误版本的警告。

  5. 我做错了什么?我是否需要使用不同的连接字符串或新版本的Firebird / .Net提供程序?我错过了别的什么吗?

    我意识到这个问题可能被视为重复,但到目前为止我找到的答案都没有解决我的问题。另外,我之前引用的之前的StackOverflow Q / A已经存在多年了,所以我希望有人可以分享新的信息。

2 个答案:

答案 0 :(得分:2)

我刚刚创建了一个非常基本的程序来测试从C#嵌入的Firebird。您需要添加最新的FirebirdSql.Data.FirebirdClient nuget包(4.5.1.0),并将Firebird嵌入式zip工具包的整个内容放入与 .exe

请注意,您需要匹配应用程序的位数:

AnyCPU似乎相当棘手。当我将可执行文件编译为AnyCPU并在64位计算机上运行时,它与Firebird Embedded 64位组合时提供了BadImageFormatException,但是使用了Firebird Embedded 32位;这与我的预期相反。

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));
                }
            }
        }
    }
}

此程序生成的连接字符串为:

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

这似乎是连接所需的最低要求。请注意,虽然嵌入在Windows上的Firebird不执行任何身份验证,但是需要提供user id,否则会触发受信任的身份验证,这对于嵌入的Firebird不起作用。

答案 1 :(得分:0)

对我有用的唯一方法:在连接字符串中设置客户端路径。

<add name="MyEmbeddedDb" connectionString="user=SYSDBA;Password=masterkey;Database=|DataDirectory|MyDb.fdb;DataSource=localhost;Port=3050;Dialect=3;Charset=NONE;ServerType=1;client library=C:\firebird\fbembed.dll" providerName="FirebirdSql.Data.FirebirdClient" />

C:\ Firebird是我提取整个下载的zip文件的文件夹(Firebird-2.5.4.26856-0_Win32_embed.zip)

相关问题