GBASE数据库 | Connecting to GBase 8s Database Using ADO.NET
Cong Li
Posted on November 11, 2024
The GBase database (GBase数据库) is widely utilized across various enterprise applications. For developers, knowing how to connect to GBase database using ADO.NET is crucial.
This article will walk you through setting up an ADO.NET connection to a GBase database and performing basic database operations.
ADO.NET Connection Setup (Using .NET Framework)
Before getting started, ensure your environment is properly set up. This example uses Visual Studio 2015 Community Edition and requires CSDK 2.2.2 configuration. Ensure that your development environment has Visual Studio installed and is compatible with .NET Framework 4.5.2.
1. Open Visual Studio and Create a New Project
Go to File (F) -> New (N) -> Project (P)
.
2. Specify Programming Language and .NET Framework Version
Select Visual C# and .NET Framework 4.5.2 to create a Windows Forms Application. Name the project "TestDotNet" and choose the desired location for the project.
3. Add Controls to Form1
In the Form1 window, add and format the following controls:
-
Label: Name it
label1
-
DataGridView: Name it
dataGridView1
-
Button: Name it
btnSelect
4. Add Reference to GBS.Data.GBasedbt.dll
In Solution Explorer, right-click on References and add a reference. Browse and select the GBS.Data.GBasedbt.dll
file. For this example, the path is: D:\GBASE\GBase Client-SDK\bin\netf40\GBS.Data.GBasedbt.dll
.
5. Add C# Code
Copy the following sample code into Form1.cs
(adjust control names as needed):
using System;
using System.Data;
using System.Windows.Forms;
using GBS.Data.GBasedbt;
namespace TestDotNet
{
public partial class Form1 : Form
{
IfxConnection ifxconn;
DataSet ds;
public Form1()
{
InitializeComponent();
IfxConnectionStringBuilder build = new IfxConnectionStringBuilder();
// Complete connection details here instead of using setnet sqlhosts config
build.Host = "bd.gbasedbt.com"; // Hostname or IP address
build.Protocol = "onsoctcp"; // Protocol used by the database
build.Service = "9088"; // Port number
build.Server = "gbase01"; // Database server name
build.Database = "utf8"; // Database name (DBNAME)
build.UID = "gbasedbt"; // User
build.Pwd = "GBase123"; // Password
build.DbLocale = "zh_CN.utf8"; // Database character set
build.ClientLocale = "zh_CN.utf8"; // Client character set
build.PersistSecurityInfo = true; // Persist security info
ifxconn = new IfxConnection(build.ConnectionString);
ifxconn.Open();
using (IfxCommand ifxcmd = ifxconn.CreateCommand())
{
ifxcmd.CommandText = "drop table if exists company";
ifxcmd.ExecuteNonQuery();
ifxcmd.CommandText = "create table company(coid serial, coname varchar(255), coaddr varchar(255))";
ifxcmd.ExecuteNonQuery();
ifxcmd.CommandText = "insert into company values (0, 'GBASE', 'Tianjin High-Tech')";
ifxcmd.ExecuteNonQuery();
ifxcmd.CommandText = "insert into company values (0, 'GBASE Beijing Branch', 'Beijing Chaoyang')";
ifxcmd.ExecuteNonQuery();
ifxcmd.CommandText = "update company set coaddr = 'Tianjin Tianbai Park' where coid = 1";
ifxcmd.ExecuteNonQuery();
ifxcmd.CommandText = "select dbinfo('version','full') from dual";
IfxDataReader dr = ifxcmd.ExecuteReader();
if (dr.Read())
{
this.label1.Text = "Database version: " + dr[0];
}
}
}
private void btnSelect_Click(object sender, EventArgs e)
{
IfxDataAdapter ifxadpt = new IfxDataAdapter("select * from company", ifxconn);
ds = new DataSet();
ifxadpt.Fill(ds);
this.dataGridView1.DataSource = ds.Tables[0];
MessageBox.Show("Database operation successful using DotNet! \n");
}
}
}
6. Debug and Test Database Connection
Ensure that all software is 64-bit compatible, and set Debug
mode to x64
. If you are using a 32-bit CSDK, select x86
.
After the Form1 window appears, click the query button to display records from the company
table along with a success message.
By following the detailed steps above, you should now be able to connect to a GBase 8s database using ADO.NET and perform basic database operations. Thank you for reading!
If you have any questions or suggestions about GBase databse (GBase数据库), feel free to leave a comment. We look forward to discussing technology with you and progressing together.
Posted on November 11, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.