使用.NET8/EFCore访问达梦数据库|C/S框架网
使用.NET8/EFCore访问达梦数据库|C/S框架网
NuGet包
C# 全选
<PackageReference Include="DM.DmProvider" Version="8.3.1.28188" />
<PackageReference Include="DM.Microsoft.EntityFrameworkCore" Version="8.0.0.28188" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.10" />
EF.Core 创建达梦数据库DmDbContext
C# 全选
/// <summary>
/// 达梦数据库DbContext | www.csframework.com demo案例
/// </summary>
public class DmDbContext : DbContext
{
public DmDbContext(DbContextOptions<DmDbContext> options) : base(options) { }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseDm("Server=localhost;Port=5236;Uid=SYSDBA;Pwd=12345654321;");
base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//注入测试用的模型
modelBuilder.Entity(typeof(dt_Customer));
modelBuilder.Entity(typeof(tb_DataSet));
}
/// <summary>
/// 获取查询接口
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public IQueryable<T> GetQueryable<T>() where T : class
{
return this.Set<T>().AsQueryable<T>().AsNoTracking();
}
}
EF.Core 测试程序
C# 全选
//创建DbContext实例 | www.csframework.com demo案例
var dbContext = new DmDbContext(new DbContextOptions<DmDbContext>());
//测试查询客户表
var q1 = dbContext.GetQueryable<dt_Customer>();
var customer = q1.FirstOrDefault();
Console.WriteLine(customer.CustomerCode + customer.NativeName);
//测试新增客户
var newCustomer = new dt_Customer
{
isid = Guid.NewGuid().ToString(),
CustomerCode = "Code" + DateTime.Now.ToString("ssfff"),//秒+毫秒
NativeName = "客户名称",
EnglishName = "EnglishName"
};
dbContext.Add(newCustomer);
dbContext.SaveChanges(); //保存数据
//测试查询账套表
var q2 = dbContext.GetQueryable<tb_DataSet>();
var ds = q2.FirstOrDefault();
Console.WriteLine(ds.DataSetID + ds.DataSetName);
ADO.NET测试程序
C# 全选
string connectionString = "Server=localhost;Port=5236;User Id=SYSDBA;Password=12345654321;";
DmConnection connection = new DmConnection(connectionString);
try
{
connection.Open();
Console.WriteLine("数据库连接成功!");
//这里可以执行SQL命令,例如查询或者更新操作
DbCommand command = connection.CreateCommand();
command.CommandText = "SELECT * FROM \"dt_Customer\"";
DataSet ds = new DataSet();
DbDataAdapter adapter = new DmDataAdapter();
adapter.SelectCommand = command;
adapter.Fill(ds);
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = ds.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
}
}
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网