达梦数据库.net驱动框架适配PetaPoco框架(附源码)

一、前言

1. PetaPoco框架

PetaPoco是一个小型、快速、单文件的微型ORM(Object Relational Mapper)框架,可在.NET和Mono环境运行。
https://github.com/CollaboratingPlatypus/PetaPoco

2. 测试版本

名称 版本
DM8 1.2.192
PetaPoco 5.1.306
.net core 3.1
DmProvider 1.1.0

二、新建项目及SQL语句

达梦数据库.net驱动框架适配PetaPoco框架(附源码)_第1张图片

CREATE TABLE "AnalysisJobQueue"
(
"AnalysisName" VARCHAR(50),
"Status" INT) STORAGE(ON "MAIN", CLUSTERBTR) ;

三、NuGet安装驱动

1. 安装达梦包

达梦数据库.net驱动框架适配PetaPoco框架(附源码)_第2张图片

2. 安装PetaPoco

达梦数据库.net驱动框架适配PetaPoco框架(附源码)_第3张图片

达梦数据库.net驱动框架适配PetaPoco框架(附源码)_第4张图片

四、修改PetaPoco源码

  1. 新建DmDatabaseProvider.cs文件 , 源码中的PetaPoco\Core目录下
using System.Data.Common;
using PetaPoco;

namespace DMConnectTest
{

    public class DmDatabaseProvider : DatabaseProvider
    {
        /// 达梦数据库类型
        public const string TYPE_NAME = "DmDb";

        public override DbProviderFactory GetFactory()
            => GetFactory("Dm.DmClientFactory,DmProvider");

        public override string GetParameterPrefix(string connectionString)
            => ":";

        public override string EscapeSqlIdentifier(string sqlIdentifier)
            => $"\"{sqlIdentifier.ToUpperInvariant()}\"";
    }
}

  1. 修改PetaPoco.cs文件Resolve函数(参数不同)

internal static IProvider Resolve(Type type, bool allowDefault, string connectionString)

          if (typeName.StartsWith(DmDatabaseProvider.TYPE_NAME))
            {
                //达梦数据库
                return Singleton<DmDatabaseProvider>.Instance;
            }

达梦数据库.net驱动框架适配PetaPoco框架(附源码)_第5张图片
3. PetaPoco.cs文件Resolve函数(参数不同)
internal static IProvider Resolve(string providerName, bool allowDefault, string connectionString)

            if (providerName.IndexOf(DmDatabaseProvider.TYPE_NAME, StringComparison.InvariantCultureIgnoreCase) >= 0)
            {
                // 达梦数据库
                return Singleton<DmDatabaseProvider>.Instance;
            }

达梦数据库.net驱动框架适配PetaPoco框架(附源码)_第6张图片

五、程序测试

1. Program.cs

using System;

namespace dm_connect_PetaPoco
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            PgDataRepository pgDataRepository = new PgDataRepository("Server=10.15.1.18:5236;UserId=SYSDBA; PWD=SYSDBA;;schema=SYSDBA");//DM数据库
            pgDataRepository.GetAnalysisJobQueues();//调用GetSites方法


        }
    }
}

2. PgDataRepository.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DMConnectTest;
using PetaPoco;

namespace dm_connect_PetaPoco
{
    public class PgDataRepository
    {
        /// <summary>
        ///  example:
        /// "Host=127.0.0.1;Username=postgres;Password=000123;Database=postgres;Port=5432"
        ///  <summary>
        public string ConfigString { get; set; }

        /// <summary>
        /// 海上风电类型 默认 false
        /// </summary>
        private Boolean isDEDOffShoreWindPower = false;

        /// <summary>
        /// catmodel db connection
        /// </summary>
        private IDatabase db = null;

        /// <summary>
        /// model db connection
        /// </summary>
        private IDatabase _metadb = null;

        
 

        public PgDataRepository(String connectString)
        {
            this.ConfigString = connectString;
            this.CreateMetaDB();
        }
 

        private void CreateMetaDB()
        {
            try
            {
                if (this._metadb == null)
                {
                    //之前调用的方法
                    //this._metadb = new PetaPoco.Database("MetaDB");
                    //现在改读配置:appsettings.json 后,采用下面的方法创建【ADD BY 夏新语 2022-05-06//  this._metadb = new PetaPoco.Database(this.ConfigString,
                  //          "Npgsql");

                   this._metadb = new PetaPoco.Database(this.ConfigString,
                           "DmDb");
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error occurred while create database configuration: " + ex.Message);
            }
        }
        /// <summary>
        /// create db connection
        /// </summary>
        private void CreateDataBase()
        {
            try
            {
                db = DatabaseConfiguration.Build()
                    .UsingConnectionString(this.ConfigString)
                    .UsingProvider<DmDatabaseProvider>().Create();
            }
            catch (Exception ex)
            {
                throw new Exception("Error occurred while create database configuration: " + ex.Message);
            }
        }

        /// <summary>
        /// get db connection
        /// </summary>
        /// <returns></returns>
        public IDatabase GetDataBase()
        {
            if (this.db == null)
                this.CreateDataBase();

            return this.db;

        }

        #region 获取信息
        public void GetAnalysisJobQueues()
        {
            string firstJobSql = "SELECT \"AnalysisName\",\"Status\" from \"AnalysisJobQueue\" ;";
            List<DMAnalysisJobQueue> analysisJobQueues = this.GetDataBase().Fetch<DMAnalysisJobQueue>(firstJobSql);
            foreach (var analysisJobQueue in analysisJobQueues)
            {
                Console.WriteLine("Analysis Name: {0}, Status: {1}", analysisJobQueue.AnalysisName, analysisJobQueue.Status);
            }

            Console.WriteLine("Analysis Success");
        }
        #endregion

    }
}

3. DMAnalysisJobQueue.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DMConnectTest
{
    public class DMAnalysisJobQueue
    {
        public string AnalysisName { get; set; }

        public int Status { get; set; }

    }
}

4. 运行结果

达梦数据库.net驱动框架适配PetaPoco框架(附源码)_第7张图片

6.源码

链接:https://pan.baidu.com/s/1Xb9zoHqLDf_QEoUw7rKbqw?pwd=hz5a
提取码:hz5a
–来自百度网盘超级会员V4的分享

你可能感兴趣的:(达梦数据库,数据库,.net,java)