如何在 C# 中打开 firebird 数据库文件?

How to open firebird database files in C#?

我们从客户端获取了firebird数据库的.ydb文件。目前我们创建了一个带有一些附加 installation/drivers 的 DSN,然后访问文件中的表和数据。

我们计划将此过程转移到 Azure 云服务(而非 Azure VM),以避免创建 DSN 等。我们需要从 C# 代码访问 .ydb 文件。

我无法直接使用 firebird ado.net 提供程序打开,抛出异常。

以下是在本机创建DSN的步骤。长期有效。

  1. Windows 服务器中的 Firebird ODBC 设置

    DSNName-DSNName1,

    Driver-IscDbc,

    数据库-E:\Somefolder\FileName.ydb

    客户端-C:\ProgramFiles\Firebird\Firebird2_5\WOW64\fbclient.dll

    数据库帐户- SYSDBA

    密码 - masterkey

    角色 - SYSDBA

    字符集 - None

  2. 然后使用下面的 C# 代码通过 DSN 访问 FileName.ydb。

    using (var connection = new OdbcConnection("DSN=DSNName1"))
    {
        connection.Open();
        var schema = connection.GetSchema("Tables");
        var tableNames = new List<string>();                    
    }
    

现在修改上面的DSN创建过程,我在c#解决方案中添加了FirebirdSql.Data.FirebirdClient nuget包。

string connectionString = "User=SYSDBA;" + "Password=masterkey;" +
"Database=E:\Somefolder\Filename.ydb;" + "Dialect=3;" + "Charset=NONE;" +
"Role=SYSDBA;";

FbConnection fbConn = new FbConnection(connectionString);
            fbConn.Open();

            var schema = fbConn.GetSchema("Tables");

它在 fbConn.Open() 上抛出异常; - 无法完成对主机 "localhost".

的网络请求

如何在不创建 DSN 的情况下直接在 C# 中打开 .ydb 文件?

您似乎遇到的最大问题是您没有安装 Firebird 服务器或 运行,因此您实际上无法连接到它并要求它打开数据库文件。

您可以从 http://www.firebirdsql.org/en/downloads/ 下载 Firebird(您可能需要 Firebird 2.5)并安装它。然后在引用 FirebirdSql.Data.FirebirdClient 的项目中,您应该能够连接到:

using (var connection = new FbConnection(@"User=username;Password=password;Database=D:\data\DB\database.fdb"))
{
    connection.Open();
}

如果出于某种原因您不想安装 Firebird 服务器,则需要使用 Firebird embedded(也可以从上面的 link 下载)。

您需要确保您的应用程序是 运行 32 位或 64 位,并下载正确的 Firebird 嵌入式程序包。将它放在路径中,或放在可执行文件的文件夹中。在URL中你需要添加ServerType=1以获得嵌入式支持(默认是ServerType=0):

using (var connection = new FbConnection(@"ServerType=1;User=username;Password=password;Database=D:\data\DB\database.fdb"))
{
    connection.Open();
}