C# Dapper 操作Oracle数据库

nuget安装内容 

 1.配置连接字符串 OracleConnectionString这个可用

{

  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",

  "DB": {
    "OracleConnStr": "Password=123456;User ID=system;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.20.13)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=XE)));",
    "OracleConnectionString": "data source=192.168.20.13:1521/XE;password=123456;user id=system;Incr Pool Size=5;Decr Pool Size=2;",
    "OracleConnStr1": "Data Source=LINKASIA;User Id=system;Password=123456",
    "MySQLConnStr": "server=127.0.0.1;port=3306;user=root;password=123456; database=spongeglueing;",
    "SqliteDBName": "mydb.db"
  },
  "SerialPort": {
    "Name": "COM!"
  }
}

 2.读取配置文件类


using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;

namespace TireDirection.Helper.Settings
{
    /// 
    /// 读取配置信息
    /// 
    public class AppSettingsHelper
    {
        /// 
        /// 
        /// 
        public static IConfiguration Configuration { get; set; }
        static AppSettingsHelper()
        {
            //ReloadOnChange = true 当appsettings.json被修改时重新加载            
            Configuration = new ConfigurationBuilder()
            .Add(new JsonConfigurationSource { Path = "Config\\appsettings.json", ReloadOnChange = true })
            .Build();
        }
    }
}

3.Dapper数据库操作类 

using Dapper;
using Oracle.ManagedDataAccess.Client;
using TireDirection.Helper.Settings;

namespace TireDirection.Helper.DB
{
    public class DapperOracleHelper
    { 
        public class DapperFactory
        { 
            public static readonly string ConnectionStr = AppSettingsHelper.Configuration["DB:OracleConnectionString"] ?? "";
            public static OracleConnection CrateOracleConnection()
            {
                var connection = new OracleConnection(ConnectionStr);
                connection.Open();
                return connection;
            }
        }

        /// 
        /// 执行SQL返回集合
        /// 
        /// sql语句
        /// 
        public static List Query(string strSql)
        {
            using (var conn = DapperFactory.CrateOracleConnection())
            {
                return conn.Query(strSql, null).ToList();
            }
        } 

        public static List QuerySqlString(string sqlStr,object param)
        {
            List result = new List();
            using (var conn = DapperFactory.CrateOracleConnection())
            {
                //string query = "SELECT  *  FROM sys_user t where username=:id ";
                //var par = new { id = "admin" };
                //var par = JsonConvert.DeserializeObject(param);
                result = conn.Query(sqlStr, param).ToList();

                // conn.Insert(new SysUser());
                // conn.Update(new SysUser());//根据主键更新
                // conn.Delete(new SysUser()); //根据主键删除
            }
            return result;
        }
        public static int ExecuteNonQuery(string sql, params SQLiteParameter[] ps)
        {
            int result = -1;
            using (var conn = DapperFactory.CrateOracleConnection())
            {
                string query = "SELECT  *  FROM sys_user t where username=:id ";
                var par = new { id = "admin" };
                result = conn.Execute(query, par);

               // conn.Insert(new SysUser());
               // conn.Update(new SysUser());//根据主键更新
               // conn.Delete(new SysUser()); //根据主键删除
            }
            return result;
        }
    }
}

4.操作数据实例 

            public static List Test( )
            {
                List result = new List();
                string SqlStr = @$"Select ID, Name,Value from LINKASIA ";
                try
                {
                    result = DapperOracleHelper.Query(SqlStr);

                    SqlStr = @$"Select ID, Name,Value from LINKASIA where ID=:id";
                    var par =   new { id = "1" };
                    result = DapperOracleHelper.QuerySqlString(SqlStr, par);
                }
                catch (Exception ex)
                {
                    Log.Logger.Error($"LinkAsiaDB GetAllSpongeGlueing 异常 {ex.Message}");
                }

                return result;
            }

你可能感兴趣的:(数据库,c#,oracle)