如何使用c#连接InterBase数据库
我需要连接到报表 Interbase 服务器并从表中提取数据。
我有以下代码,我正在尝试几个选项来成功连接到服务器,但无法这样做。您能让我知道我的代码有什么问题吗?或者请向我指出任何介绍成功连接到 Interbase 服务器并提取数据的分步方法的文章。
using System;
using System.Data;
using System.Data.Common;
using InterBaseSql.Data.InterBaseClient;
string connectionString = "server=remoteserver_ip_address;dataBase=C:\\test\\interbasedb\\database.gdb;User_Name=myusername;Password=mypassword;";
using (var connection = new IBConnection(connectionString))
{
connection.Open();
using (var transaction = connection.BeginTransaction())
{
using (var command = new IBCommand("select * from table rows 1", connection, transaction))
{
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var values = new object[reader.FieldCount];
reader.GetValues(values);
Console.WriteLine(string.Join("|", values));
}
}
}
}
}
我怀疑问题出在连接字符串上。我尝试了多种方式更改连接字符串,但每次都会出现不同的错误。 以下所有错误。
- 客户端库 - ibclient64 未找到(我将 ibclient64.dll 复制到项目文件夹中,此错误已解决)
- 您的用户名和密码未定义。请您的数据库管理员设置 InterBase 登录。
- 连接被远程接口拒绝
- 无法完成对主机“remoteserver_ip_address”的网络请求。无法找到主机。未定义服务 gds_db/tcp。
我能够使用 IBConsole 应用程序连接到同一台服务器,我相信这是一个连接到 Interbase 服务器的客户端软件(例如用于 SQL Server 的 Management Studio、用于 MySQL 的 WorkBench 和用于 PostgresSQL 的 PGAdmin
)我用于使用 IBConsole 连接到 Interbase Server 的参数与我在 C# 代码中使用的参数相同。
报表服务器 IP:remoteserver_ip_address
数据库:C:\test\interbasedb\database.gdb
用户名:我的用户名
密码:mypassword
上述问题得到部分突破。
经过几个小时的反复试验,我终于能够使用 Embarcadero 驱动程序成功连接到 interbase 服务器。
我必须将连接字符串更改为如下所示才能成功连接。
server=remoteserver_ip_address;database=C:\test\interbasedb\database.gdb;user=myusername;password=mypassword
但现在我遇到了另一个问题。当我使用像 select * from some_table_which_doesnot_exists 这样的查询时,我清楚地收到一条消息,指出找不到表。
当我使用像 Select * from a table_that_exists_in_the_db 这样的查询时,我总是收到以下错误。
- 动态 SQL 错误
- SQL 错误代码 - 804
- SQLDA 错误(我相信 SQLDA = SQL DataAdapter,因为这就是代码抛出错误的地方)
我去了 Embarcadero 错误代码列表 查看此错误的更多信息,发现原因是SQLDA版本缺失或不正确,或者变量数量/类型不正确。。我被困在这里不知道如何进一步进行。请帮忙。
I have a need to connect to a report Interbase server and pull data from a table.
I have the following code, I am trying several options to make a successful connection to the server, however not able to do so. Could you please let me know what's wrong with my code. Or please point me to any article which shows a step by step approach to successfully connect to an Interbase server and pull data.
using System;
using System.Data;
using System.Data.Common;
using InterBaseSql.Data.InterBaseClient;
string connectionString = "server=remoteserver_ip_address;dataBase=C:\\test\\interbasedb\\database.gdb;User_Name=myusername;Password=mypassword;";
using (var connection = new IBConnection(connectionString))
{
connection.Open();
using (var transaction = connection.BeginTransaction())
{
using (var command = new IBCommand("select * from table rows 1", connection, transaction))
{
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var values = new object[reader.FieldCount];
reader.GetValues(values);
Console.WriteLine(string.Join("|", values));
}
}
}
}
}
I suspect the issue is with the connection string. I tried changing the connection string several ways, but getting different error every time.
All errors below.
- Client library - ibclient64 not found (I copied ibclient64.dll into the project folder, and this error was resolved)
- Your user name and password are not defined. Ask your database administrator to set up a InterBase login.
- connection rejected by remote interface
- Unable to complete network request to host "remoteserver_ip_address". Failed to locate host machine. Undefined service gds_db/tcp.
I am able to connect to the same server using IBConsole application, Which I believe is a client software to connect to Interbase server (Like Management Studio for SQL Server, and WorkBench for MySQL and PGAdmin for PostgresSQL)
The Parameters I am using to connect to the Interbase Server using IBConsole are same as that of I am using in the C# code.
Report Server IP: remoteserver_ip_address
DataBase: C:\test\interbasedb\database.gdb
User Name: myusername
Password: mypassword
Partial breakthrough for above issue.
After several hours of trial and error, I was finally able to connect to the interbase server successfully using Embarcadero drivers.
I had to change the connectionstring to look as below for a successful connection.
server=remoteserver_ip_address;database=C:\test\interbasedb\database.gdb;user=myusername;password=mypassword
But now I am stuck with another issue. When I use a query like select * from some_table_which_doesnot_exists, I clearly receive a message that the Table is not found.
And when I use a query like Select * from a table_that_exists_in_the_db, I always get follow error.
- Dynamic SQL Error
- SQL error code - 804
- SQLDA error (I believe SQLDA = SQL DataAdapter, because thats where the code it throwing error)
I went to the Embarcadero Error Codes List to see more information on this error and found the reason to be SQLDA missing or incorrect version, or incorrect number/type of variables.. I am stuck here not sure how to proceed further. Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您缺少连接到 interbase 服务器实例的“PORT”详细信息。
您可以尝试以下连接字符串解决方案。这在我的本地机器上对我有用。
I think you are missing "PORT" details for connection to the interbase server instance.
You can try the below solution for the connection string. This has worked for me in my local machine.
名称空间winformsapp1_3grupa
{
公共部分班级表格1:表格
{
sqlConnection konekcija = new SqlConnection(@“ data source =(localdb)\ mssqllocaldb; actactdbfilename; intectdbfilename; intemperated Security = true; true; connect timeout = 30');
公共形式1()
{
InitializeComponent();
}}
}
namespace WinFormsApp1_3grupa
{
public partial class Form1 : Form
{
SqlConnection konekcija = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename;Integrated Security=True;Connect Timeout=30");
public Form1()
{
InitializeComponent();
}
}