.Net客户端无法访问嵌入式Firebird数据库服务器
我的目标是开发一个使用嵌入式Firebird服务器的程序,但是在尝试使用.Net客户端进行连接时会遇到错误。我遵循了多个线程的建议以使其正常运行,但我无法弄清楚。我试过更改连接字符串和文件,但始终收到相同的错误。
这是我的研究以及到目前为止我尝试过的一切的详细说明:
如何在Visual C#2010中连接和使用Firebird db嵌入式服务器
我下载了链接中指定的文件,按照步骤操作并运行了代码,并得到了与原始海报相同的错误消息:
FirebirdSql.Data.FirebirdClient.FbException Message =无法完成对主机“ 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 Message =指定的服务器类型不正确。
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,但这也不起作用。
使用Firebird嵌入式数据库时,如何解决C#中的连接错误?
对于将fbembed.dll重命名为fbclient.dll也有相同的建议,该建议也不适用于原始海报。连接字符串中可接受的答案ServerType = 1,但是@ cincura.net答案下的注释为我提供了一种新的调查可能性;处理器架构。不幸的是,在64bt和32bit版本之间切换没有任何区别。
我以为自从我使用Visual Studios Express以来,切换到32位版本可能是答案。
该线程中的最后一个评论是另一个人说更改为32bit也不能解决问题。
尝试使用Firebird嵌入式服务器-指定的服务器类型不正确
我回过头来查找有关ServerType的信息,因为我已经将其视为1和0,并找到了这篇文章。@Nigel的答案是更新到.Net提供程序的最新版本。不幸的是,我无法弄清楚如何使用Firebird网站(4.5.1.0)上的最新版本,因为示例中缺少FirebirdSql命名空间。此外,Visual Studios会在导入时提示我有关目标版本错误的.Net的警告。
我究竟做错了什么?我需要使用其他连接字符串还是新版本的Firebird / .Net提供程序?我还想念其他东西吗?
我意识到这个问题可能被认为是重复的,但是到目前为止,我所发现的答案都没有解决我的问题。另外,我上面引用的以前的StackOverflow Q / A已经使用多年了,因此我希望有人可以分享新信息。
我刚刚创建了一个非常基本的程序来测试从C#嵌入的Firebird。您需要添加最新的FirebirdSql.Data.FirebirdClient
nuget软件包(4.5.1.0),并将Firebird嵌入式zip套件的全部内容放入与.exe相同的文件夹中。
请注意,您需要匹配应用程序的位数:
AnyCPU似乎很棘手。当我将可执行文件编译为AnyCPU并在64位计算机上运行BadImageFormatException
时,与Firebird Embedded 64位结合使用时,它给出了a ,但与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。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句