• .net OleDbParameter转SugarParameter


    说明原因 :

    因公司项目是很早的老项目,底层用的dbhelper数据库操作类,
    因需要支持多个数据库,本次是要求支持postgresql数据库,边学边做,期间也遇到了很多postgresql数据库的问题,如果重新改框架、换底层等等一些来不及、所有就直接改了连接数据库、封装了许多sqlsugar的底层查询方法及更新事务方法、目前支持mysql、postgresql、oracle、sqlserver等

    要连接PostgreSQL数据库,需要使用PostgreSQL的.NET数据提供程序,例如Npgsql。可以使用NpgsqlConnection类来建立与PostgreSQL数据库的连接。以下是一个简单的示例:

    using Npgsql;
    using System.Data;
    
    string connectionString = "Server=myServerAddress;Port=myPortNumber;Database=myDataBase;User Id=myUsername;Password=myPassword;";
    NpgsqlConnection connection = new NpgsqlConnection(connectionString);
    connection.Open();
    
    // 使用连接执行查询或更新等操作
    
    connection.Close();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    但是因为一些原因,还是直接改了底层,下面举例说明一个典型的例子,其中两个Query方法中是查询数据库存储为dataset的方法(未改前),两个入参类型不同:

                  /// 
            /// 执行查询语句,返回DataSet
            /// 
            /// 查询语句
            /// DataSet
            public static DataSet Query(string SQLString, params OleDbParameter[] cmdParms)
            {
                using (OleDbConnection connection = new OleDbConnection(connectionString))
                {
                    OleDbCommand cmd = new OleDbCommand();
                    PrepareCommand(cmd, connection, null, SQLString, cmdParms);
                    using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
                    {
                        DataSet ds = new DataSet();
                        try
                        {
                            da.Fill(ds, "ds");
                            cmd.Parameters.Clear();
                        }
                        catch (System.Data.OleDb.OleDbException ex)
                        {
                            throw new Exception(ex.Message);
                        }
                        return ds;
                    }
                }
            }
    
            /// 
            /// 执行查询语句,返回DataSet
            /// 
            /// 查询语句
            /// DataSet
            public static DataSet Query(string SQLString, List<OleDbParameter> cmdParms)
            {
                using (OleDbConnection connection = new OleDbConnection(connectionString))
                {
                    OleDbCommand cmd = new OleDbCommand();
                    PrepareCommand(cmd, connection, null, SQLString, cmdParms);
                    using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
                    {
                        DataSet ds = new DataSet();
                        try
                        {
                            da.Fill(ds, "ds");
                            cmd.Parameters.Clear();
                        }
                        catch (System.Data.OleDb.OleDbException ex)
                        {
                            throw new Exception(ex.Message);
                        }
                        return ds;
                    }
                }
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    OleDbConnection是不支持连接postgresql数据库,所以就考虑到使用比较主流的框架SqlSugar,其中把OleDbParameter转换为SugarParameter进行查询,代码如下:

    
            #region OleDbParameter转SugarParameter
            /// 
            /// OleDbParameter转SugarParameter处理
            /// 
            /// 
            public static (SugarParameter[], string) GetSugarParameterArry(OleDbParameter[] dbParameters, string SQLString)
            {
                SugarParameter[] SGParameter = new SugarParameter[dbParameters.Length];
                DateTime? dtime = new DateTime?();
                for (int i = 0; i < dbParameters.Length; i++)
                {
                    int firstIndex = SQLString.IndexOf("?", i + 1);//查找第几次出现的_的索引位置
                    if (firstIndex > -1)
                    {
                        SQLString = SQLString.Remove(firstIndex, 1).Insert(firstIndex, ":" + dbParameters[i].ParameterName);  //删除索引位置处的一个字符,再进行该位置的insert
                    }
                    if (dbParameters[i].DbType.ToString() == "DateTime")
                    {
                        if (dbParameters[i].Value != null && dbParameters[i].Value.GetType().Name != "DBNull")
                        {
                            dtime = Convert.ToDateTime(dbParameters[i].Value);
                            SGParameter[i] = new SugarParameter(dbParameters[i].ParameterName, dtime);
                        }
                        else
                        {
                            SGParameter[i] = new SugarParameter(dbParameters[i].ParameterName, dtime, dbParameters[i].DbType);
    
                        }
                    }
                    else if (dbParameters[i].DbType.ToString() == "Decimal")
                    {
                        SGParameter[i] = new SugarParameter(dbParameters[i].ParameterName, Convert.ToDecimal(dbParameters[i].Value));
                    }
                    else if (dbParameters[i].DbType.ToString() == "AnsiString" && dbParameters[i].Value != null
                       && dbParameters[i].Value.GetType().Name != "String")
                    {
                        SGParameter[i] = new SugarParameter(dbParameters[i].ParameterName, dbParameters[i].Value.ToString());
                    }
                    else
                    {
                        SGParameter[i] = new SugarParameter(dbParameters[i].ParameterName, ((dbParameters[i].Value == null || Convert.ToString(dbParameters[i].Value) == "") ? DBNull.Value : (object)dbParameters[i].Value).ToString());
                    }
                }
                return (SGParameter, SQLString);
            }
    
    
            /// 
            /// OleDbParameter转SugarParameter处理
            /// 
            /// 
            public static SugarParameter[] GetSugarParameterList(List<OleDbParameter> dbParameters, ref string SQLString)
            {
                SugarParameter[] SGParameter = new SugarParameter[dbParameters.Count];
                DateTime? dtime = new DateTime?();
                for (int i = 0; i < dbParameters.Count; i++)
                {
                    int firstIndex = SQLString.IndexOf("?", i + 1);//查找第几次出现的_的索引位置
                    if (firstIndex > -1)
                    {
                        SQLString = SQLString.Remove(firstIndex, 1).Insert(firstIndex, ":" + dbParameters[i].ParameterName);  //删除索引位置处的一个字符,再进行该位置的insert
                    }
                    SGParameter[i] = new SugarParameter(dbParameters[i].ParameterName, dbParameters[i].Value);
                    if (dbParameters[i].DbType.ToString() == "DateTime")
                    {
                        if (dbParameters[i].Value != null && dbParameters[i].Value.GetType().Name != "DBNull")
                        {
                            if (dbParameters[i].Value.ToString() != "")
                            {
                                dtime = Convert.ToDateTime(dbParameters[i].Value);
                                SGParameter[i] = new SugarParameter(dbParameters[i].ParameterName, dtime);
                            }
                            else
                            {
                                SGParameter[i] = new SugarParameter(dbParameters[i].ParameterName, dtime, dbParameters[i].DbType);
                            }
                        }
                        else
                        {
                            SGParameter[i] = new SugarParameter(dbParameters[i].ParameterName, dtime, dbParameters[i].DbType);
                        }
                    }
                    else if (dbParameters[i].DbType.ToString() == "Decimal")
                    {
                        SGParameter[i] = new SugarParameter(dbParameters[i].ParameterName, Convert.ToDecimal(dbParameters[i].Value));
                    }
                    else if (dbParameters[i].DbType.ToString() == "AnsiString" && dbParameters[i].Value != null
                       && dbParameters[i].Value.GetType().Name != "String")
                    {
                        SGParameter[i] = new SugarParameter(dbParameters[i].ParameterName, dbParameters[i].Value.ToString());
                    }
                    else
                    {
                        SGParameter[i] = new SugarParameter(dbParameters[i].ParameterName, ((dbParameters[i].Value == null || Convert.ToString(dbParameters[i].Value) == "") ? DBNull.Value : (object)dbParameters[i].Value).ToString());
                    }
                }
                return SGParameter;
            }
    
    
            /// 
            /// 根据数据库类型替换某些内置函数
            /// 
            /// 
            /// 
            public static string ReplaceType(string SqlString_Place)
            {
                if (!string.IsNullOrEmpty(DbTypeStr))
                {
                    //数据库providerName:SqlClient MySqlClient SQLite OracleManaged/OracleClient Npgsql
                    if (DbTypeStr.EndsWith("SqlClient", StringComparison.OrdinalIgnoreCase))
                    {
                        //SqlSugar.DbType.SqlServer;
                    }
                    else if (DbTypeStr.EndsWith("MySqlClient", StringComparison.OrdinalIgnoreCase))
                    {
                        //SqlSugar.DbType.MySql;
                    }
                    else if (DbTypeStr.EndsWith("SQLite", StringComparison.OrdinalIgnoreCase))
                    {
                        //SqlSugar.DbType.Sqlite;
                    }
                    else if (DbTypeStr.EndsWith("Oracle", StringComparison.OrdinalIgnoreCase))
                    {
                        //SqlSugar.DbType.Oracle;
                        SqlString_Place = CaseInsenstiveReplace(SqlString_Place, "decode", "COALESCE");
                    }
                    else if (DbTypeStr.EndsWith("Npgsql", StringComparison.OrdinalIgnoreCase))
                    {
                        //SqlSugar.DbType.PostgreSQL;
                        SqlString_Place = CaseInsenstiveReplace(SqlString_Place, "to_date", "to_timestamp");
                        SqlString_Place = CaseInsenstiveReplace(SqlString_Place, "sysdate", "current_timestamp");
                        SqlString_Place = CaseInsenstiveReplace(SqlString_Place, "sys_guid", "gen_random_uuid");
                        SqlString_Place = CaseInsenstiveReplace(SqlString_Place, "INSTR", "strpos");
                    }
                }
                return SqlString_Place;
            }
    
    
            static string CaseInsenstiveReplace(string originalString, string oldValue, string newValue)
            {
                Regex regEx = new Regex(oldValue,
                RegexOptions.IgnoreCase | RegexOptions.Multiline);
                return regEx.Replace(originalString, newValue);
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147

    下表列出了在Oracle数据库中常用的函数和它们在PostgreSQL数据库中的替代函数:

    Oracle函数Postgresql替代函数
    NVL(exp1, exp2)COALESCE(exp1, exp2)
    DECODE(exp, value1, result1, value2, result2, ..., default_result)使用CASE表达式进行等效操作
    GREATEST(value1, value2, ...)GREATEST(value1, value2, ...)
    LEAST(value1, value2, ...)LEAST(value1, value2, ...)
    MONTHS_BETWEEN(date1, date2)EXTRACT(MONTH FROM age(date2, date1))
    LAST_DAY(date)date_trunc('month', date) + interval '1 month - 1 day'
    TRUNC(date [, format])date_trunc('format', date)
    ADD_MONTHS(date, num_months)date + interval 'num_months month'
    SYSDATECURRENT_TIMESTAMP
    ROWNUMROW_NUMBER() OVER()
    TO_NUMBER(string [, format])CAST(string AS numeric)
    TO_DATE(string, format)TO_DATE(string, format)CAST(string AS date)
    INSTR(string, substring [, start_position [, occurrence]])POSITION(substring IN string [, start_position [, occurrence]])
    SUBSTR(string, start [, length])SUBSTRING(string FROM start [FOR length])
    LENGTH(string)LENGTH(string)CHAR_LENGTH(string)
    REPLACE(string, search_string, replacement_string)REPLACE(string, from_string, to_string)
    UPPER(string)UPPER(string)
    LOWER(string)LOWER(string)
    ROUND(number [,decimal_places])ROUND(number [, decimal_places])TRUNC(number [, decimal_places])
    ABS(number)ABS(number)
    CEIL(number)CEILING(number)
    FLOOR(number)FLOOR(number)

    这些函数在Oracle和PostgreSQL之间的语法和行为可能存在差异,因此在编写跨平台应用程序时,请务必查阅有关不同数据库之间的特定功能和语法差异的文档。

    以上两个查询方法修改之后为:

          /// 
            /// 执行查询语句,返回DataSet
            /// 
            /// 查询语句
            /// DataSet
            public static DataSet Query(string SQLString, params OleDbParameter[] cmdParms)
            {
                SugarDbContext sugar = new SugarDbContext();
                var result = GetSugarParameterArry(cmdParms, SQLString);
                DataSet ds = null;
                try
                {
                    using (var db = sugar.GetInstance())
                    {
                        SQLString = ReplaceType(SQLString);
                        ds = db.Ado.GetDataSetAll(result.Item2, result.Item1);
                    }
                     --注意:oracel默认字段是大写,postgresql默认字段是小写
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message + "\r\nSQLString:" + SQLString);
                }
                return ds;
            }
    
    
    
            /// 
            /// 执行查询语句,返回DataSet
            /// 
            /// 查询语句
            /// DataSet
            public static DataSet Query(string SQLString, List<OleDbParameter> cmdParms)
            {
                SugarDbContext sugar = new SugarDbContext();
                SugarParameter[] arParms = GetSugarParameterList(cmdParms, ref SQLString);
                DataSet dataSet = null;
                try
                {
                    using (var Db = sugar.GetInstance())
                    {
                        SQLString = ReplaceType(SQLString);
                        dataSet = Db.Ado.GetDataSetAll(SQLString, arParms);
                    }
                   --注意:oracel默认字段是大写,postgresql默认字段是小写
                    return dataSet;
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message + "\r\nSQLString:" + SQLString);
                }
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    SugarDbContext类

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Text;
    using SqlSugar;
    
    namespace  DBUtility
    {
        public class SugarDbContext
        {
            //获取连接字符串        
            private static string Connection = DESEncrypt.Decrypt(ConfigurationManager.ConnectionStrings["OleDbConnectionString"].ConnectionString);
            private static DbType DBType;
            private static readonly string providerName = ConfigurationManager.AppSettings["DbType"];
            public SugarDbContext()
            {
    
            }
    
    
            /// 
            /// SugarDb
            /// 
            /// 
            /// 
            public SqlSugarClient GetInstance(string ConnectionString = null)
            {
                if (!string.IsNullOrEmpty(ConnectionString))
                {
                    Connection = ConnectionString;
                }
                if (string.IsNullOrEmpty(Connection))
                    throw new ArgumentNullException("数据库连接字符串为空!");
                else
                    DBType = GetSugarDbType();
                return new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = Connection,           //必填, 数据库连接字符串
                    DbType = DBType,                         //必填, 数据库类型
                    IsAutoCloseConnection = true,            //默认false, 时候知道关闭数据库连接, 设置为true无需使用using或者Close操作
                    InitKeyType = InitKeyType.SystemTable    //默认SystemTable, 字段信息读取, 如:该属性是不是主键,是不是标识列等等信息
                });
            }
    
    
    
            /// 
            /// 根据链接字符串的providerName决定那种数据库类型
            /// 
            /// 
            /// 
            private DbType GetSugarDbType()
            {
                DBType = SqlSugar.DbType.Oracle;
                if (!string.IsNullOrEmpty(providerName))
                {
                    //数据库providerName:SqlClient MySqlClient SQLite OracleManaged/OracleClient Npgsql
                    if (providerName.EndsWith("SqlClient", StringComparison.OrdinalIgnoreCase))
                    {
                        DBType = SqlSugar.DbType.SqlServer;
                    }
                    else if (providerName.EndsWith("MySqlClient", StringComparison.OrdinalIgnoreCase))
                    {
                        DBType = SqlSugar.DbType.MySql;
                    }
                    else if (providerName.EndsWith("SQLite", StringComparison.OrdinalIgnoreCase))
                    {
                        DBType = SqlSugar.DbType.Sqlite;
                    }
                    else if (providerName.EndsWith("Oracle", StringComparison.OrdinalIgnoreCase))
                    {
                        DBType = SqlSugar.DbType.Oracle;
                    }
                    else if (providerName.EndsWith("Npgsql", StringComparison.OrdinalIgnoreCase))
                    {
                        DBType = SqlSugar.DbType.PostgreSQL;
                    }
                }
                return DBType;
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84

    以上是总结的之前遇到的问题,其他未注明,如有错误,还请指正~

  • 相关阅读:
    【C++14算法】make_unique
    iPhone 隔空投送使用指南:详细教程
    linux相关知识以及有关指令3
    攻防世界-PWN-new_easypwn
    vue3项目搭建手册
    【数据结构】线性表与顺序表
    多重背包问题 ← 规模小时可转化为0-1背包问题
    Mysql 45讲学习笔记(二十五)MYSQL保证高可用
    Python 基于 Yolov8 + CPU 实现物体检测
    数组中的第K个最大元素 -- 堆&快排
  • 原文地址:https://blog.csdn.net/weixin_42064877/article/details/130869007