GBase 8s
运维管理
文章

EFCore连接GBase8s使用操作步骤【Linux版】

GBase社区管理员
发表于2024-04-18 16:07:44355次浏览0个评论

一、 使用前准备

1、在/etc/profile 文件中增加csdk设置

 

export GBASEDBTDIR=/opt/net/csdk

export GBASEDBTSERVER=ol_gbasedbt1210_1

export PATH=$GBASEDBTDIR/bin:$PATH

export GBASEDBTSQLHOSTS=$GBASEDBTDIR/etc/sqlhosts

export LD_LIBRARY_PATH=$GBASEDBTDIR/lib:$GBASEDBTDIR/lib/esql:$GBASEDBTDIR/lib/cli:$LD_LIBRARY_PATH

红色部分更换为csdk文件夹实际路径

 

2、修改/opt/net/csdk/etc/sqlhosts 文件内容

 

ol_gbasedbt1210_1 onsoctcp 192.168.71.1289077

 

要连接的实例名称

Ip地址

端口号

 

3、 source /etc/profile使修改后的环境变量生效

 

二、 编写Demo,以dotnet环境为例

 

1、安装dotnet-sdk-6.0

yum -y install dotnet-sdk-6.0

 

2、创建Test工程

mkdir /Project/Test

cd /Project/Test

 

dotnet new console

 

3、添加 GeneralData.EntityFrameworkCore.GBase 安装包

dotnet nuget add source "/opt/net/Project" -n mysource

dotnet nuget list source

dotnet add package GeneralData.EntityFrameworkCore.GBase

 

红色字体为Nuget包所在路径

 

4、在.csproj配置文件中增加依赖

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>

<OutputType>Exe</OutputType>

<TargetFramework>net6.0</TargetFramework>

<ImplicitUsings>enable</ImplicitUsings>

<Nullable>enable</Nullable>

</PropertyGroup>

<ItemGroup>

<PackageReference Include="GeneralData.EntityFrameworkCore.GBase" Version="1.0.6.1"/>

</ItemGroup>

</Project>

 

5、编写程序文件

using System;
using GBS.Data.GBasedbt;
namespace ConsoleApp1
{
class Program
{
	static void Main(string[] args)
	{
		GbsConnectionStringBuilder builder = new GbsConnectionStringBuilder();
		builder.Host = "192.168.71.128";
		builder.Service = "9077";
		builder.Server = "ol_gbasedbt1210_1";
		builder.Database = "sysmaster";
		builder.UID = "root";
		builder.Pwd = "a";
		GbsConnection conn = new GbsConnection(builder.ConnectionString);
		conn.Open();
		GbsCommand cmd = conn.CreateCommand();
		cmd.CommandText = "select distinct tabname from flags_text";
		GbsDataReader rd = cmd.ExecuteReader();
		while (rd.Read())
		{
			Console.WriteLine(rd["tabname"]);
		}
	}
}
}

 

6、编译程序

dotnet build

 

7、运行程序

dotnet run

 

评论已关闭