如何使用c#连接InterBase数据库

发布于 2025-01-20 05:03:27 字数 2716 浏览 4 评论 0原文

我需要连接到报表 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));
                }
            }
        }
    }
}

我怀疑问题出在连接字符串上。我尝试了多种方式更改连接字符串,但每次都会出现不同的错误。 以下所有错误。

  1. 客户端库 - ibclient64 未找到(我将 ibclient64.dll 复制到项目文件夹中,此错误已解决)
  2. 您的用户名和密码未定义。请您的数据库管理员设置 InterBase 登录。
  3. 连接被远程接口拒绝
  4. 无法完成对主机“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.

  1. Client library - ibclient64 not found (I copied ibclient64.dll into the project folder, and this error was resolved)
  2. Your user name and password are not defined. Ask your database administrator to set up a InterBase login.
  3. connection rejected by remote interface
  4. 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

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾北清歌寒 2025-01-27 05:03:27

我认为您缺少连接到 interbase 服务器实例的“PORT”详细信息。

您可以尝试以下连接字符串解决方案。这在我的本地机器上对我有用。

static void Main(string[] args)
    {
        var cs = BuildConnectionStringBuilder().ToString();
        try
        {
            using (var connection = new IBConnection(cs))
            {
                connection.Open();
                using (var transaction = connection.BeginTransaction())
                {
                    using (var command = new IBCommand("select * from employee 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));
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        
    }
private static IBConnectionStringBuilder BuildConnectionStringBuilder()
    {
        var builder = new IBConnectionStringBuilder();
        builder.UserID = "SYSDBA";
        builder.Password = "masterkey";
        builder.DataSource = "localhost";
        builder.Database = AppDomain.CurrentDomain.BaseDirectory + "test-employee.ib";
        builder.Port = 3050;
        return builder;
    }

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.

static void Main(string[] args)
    {
        var cs = BuildConnectionStringBuilder().ToString();
        try
        {
            using (var connection = new IBConnection(cs))
            {
                connection.Open();
                using (var transaction = connection.BeginTransaction())
                {
                    using (var command = new IBCommand("select * from employee 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));
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        
    }
private static IBConnectionStringBuilder BuildConnectionStringBuilder()
    {
        var builder = new IBConnectionStringBuilder();
        builder.UserID = "SYSDBA";
        builder.Password = "masterkey";
        builder.DataSource = "localhost";
        builder.Database = AppDomain.CurrentDomain.BaseDirectory + "test-employee.ib";
        builder.Port = 3050;
        return builder;
    }
沫离伤花 2025-01-27 05:03:27
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

名称空间winformsapp1_3grupa
{
公共部分班级表格1:表格
{
sqlConnection konekcija = new SqlConnection(@“ data source =(localdb)\ mssqllocaldb; actactdbfilename; intectdbfilename; intemperated Security = true; true; connect timeout = 30');
公共形式1()
{
InitializeComponent();
}}

    private void tabPage1_Click(object sender, EventArgs e)
    {

    }

    private void buttonIzadji_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        string sqlUpit = "select * from Citalac";
        try
        {
            SqlCommand komanda = new SqlCommand(sqlUpit, konekcija);
            SqlDataAdapter da = new SqlDataAdapter(komanda);
            DataSet ds = new DataSet();
            da.Fill(ds);
            DataTable dt = ds.Tables[0];
            foreach (DataRow row in dt.Rows)
            {
                ListViewItem listItem = new ListViewItem(row["HotelID"].ToString());
                listItem.SubItems.Add(row["Naziv"].ToString());
                listItem.SubItems.Add(row["Adresa"].ToString());
                listItem.SubItems.Add(row["Telefon"].ToString());
                listItem.SubItems.Add(row["Grad"].ToString());
                listView1.Items.Add(listItem); // Dodaje red u ListView
            }
        }
        catch (Exception)
        {
            //MessageBox.Show("Greska");
        }
    }

    private void buttonPrikazDGV_Click(object sender, EventArgs e)
    {
        SqlParameter param = new SqlParameter();
        param.ParameterName = "@param1";
        param.Value = numericUpDown1.Value;

        string sqlUpit = "select * from Hotel where HotelID=@param1";
        SqlCommand komanda = new SqlCommand(sqlUpit, konekcija);
        komanda.Parameters.Add(param);
        try
        {
            konekcija.Open();
            SqlDataReader dr = komanda.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);
            dataGridView2.DataSource = dt;
        }
        catch (Exception)
        {
            MessageBox.Show("Greska");
        }
        finally
        {
            konekcija.Close();
        }
    }

    private void buttonPrikazLV_Click(object sender, EventArgs e)
    {
        string sqlUpit = "select * from Hotel";

        try
        {
            SqlCommand komanda = new SqlCommand(sqlUpit, konekcija);


            SqlDataAdapter da = new SqlDataAdapter(komanda);
            DataSet ds = new DataSet();
            da.Fill(ds);
            DataTable dt = ds.Tables[0];
            foreach (DataRow row in dt.Rows)
            {
                ListViewItem listItem = new ListViewItem(row["HotelID"].ToString());
                listItem.SubItems.Add(row["Naziv"].ToString());
                listItem.SubItems.Add(row["Adresa"].ToString());
                listItem.SubItems.Add(row["Telefon"].ToString());
                listItem.SubItems.Add(row["Grad"].ToString());
                listItem.SubItems.Add(row["Drzava"].ToString());
                listItem.SubItems.Add(row["Kategorija"].ToString());
                listView1.Items.Add(listItem); // Dodaje red u ListView
            }
        }
        catch (Exception)
        {
            MessageBox.Show("Greska");
        }
    }

    private void buttonIzadji2_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

    private void tabPage1_Click(object sender, EventArgs e)
    {

    }

    private void buttonIzadji_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        string sqlUpit = "select * from Citalac";
        try
        {
            SqlCommand komanda = new SqlCommand(sqlUpit, konekcija);
            SqlDataAdapter da = new SqlDataAdapter(komanda);
            DataSet ds = new DataSet();
            da.Fill(ds);
            DataTable dt = ds.Tables[0];
            foreach (DataRow row in dt.Rows)
            {
                ListViewItem listItem = new ListViewItem(row["HotelID"].ToString());
                listItem.SubItems.Add(row["Naziv"].ToString());
                listItem.SubItems.Add(row["Adresa"].ToString());
                listItem.SubItems.Add(row["Telefon"].ToString());
                listItem.SubItems.Add(row["Grad"].ToString());
                listView1.Items.Add(listItem); // Dodaje red u ListView
            }
        }
        catch (Exception)
        {
            //MessageBox.Show("Greska");
        }
    }

    private void buttonPrikazDGV_Click(object sender, EventArgs e)
    {
        SqlParameter param = new SqlParameter();
        param.ParameterName = "@param1";
        param.Value = numericUpDown1.Value;

        string sqlUpit = "select * from Hotel where HotelID=@param1";
        SqlCommand komanda = new SqlCommand(sqlUpit, konekcija);
        komanda.Parameters.Add(param);
        try
        {
            konekcija.Open();
            SqlDataReader dr = komanda.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);
            dataGridView2.DataSource = dt;
        }
        catch (Exception)
        {
            MessageBox.Show("Greska");
        }
        finally
        {
            konekcija.Close();
        }
    }

    private void buttonPrikazLV_Click(object sender, EventArgs e)
    {
        string sqlUpit = "select * from Hotel";

        try
        {
            SqlCommand komanda = new SqlCommand(sqlUpit, konekcija);


            SqlDataAdapter da = new SqlDataAdapter(komanda);
            DataSet ds = new DataSet();
            da.Fill(ds);
            DataTable dt = ds.Tables[0];
            foreach (DataRow row in dt.Rows)
            {
                ListViewItem listItem = new ListViewItem(row["HotelID"].ToString());
                listItem.SubItems.Add(row["Naziv"].ToString());
                listItem.SubItems.Add(row["Adresa"].ToString());
                listItem.SubItems.Add(row["Telefon"].ToString());
                listItem.SubItems.Add(row["Grad"].ToString());
                listItem.SubItems.Add(row["Drzava"].ToString());
                listItem.SubItems.Add(row["Kategorija"].ToString());
                listView1.Items.Add(listItem); // Dodaje red u ListView
            }
        }
        catch (Exception)
        {
            MessageBox.Show("Greska");
        }
    }

    private void buttonIzadji2_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文