当前位置:Gxlcms > 数据库问题 > OracleDBHeper

OracleDBHeper

时间:2021-07-01 10:21:17 帮助过:50人阅读

System; using Oracle.ManagedDataAccess; using Oracle.ManagedDataAccess.Types; using Oracle.ManagedDataAccess.Client; using System.Data; using System.Collections.Generic; using Omu.ValueInjecter; using Common.Data; namespace Common.DataBase { public class OracleDBHelper { public static string connStr = string.Empty; public static void IniConnStr(string host, int port, string serverName, string userName, string pwd) { connStr = $"Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={host})(PORT={port})))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME={serverName})));User Id={userName};Password={pwd};"; } public static DataTable GetTable(string sql) { DataTable OutDataTable = new DataTable(); DataRow dataRow; using (OracleConnection con = new OracleConnection(connStr)) { con.Open(); OracleCommand cmd = new OracleCommand(sql, con); string data = string.Empty; OracleDataReader daReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); for (int j = 0; j < daReader.FieldCount; j++) { OutDataTable.Columns.Add(daReader.GetName(j)); } while (daReader.Read()) { dataRow = OutDataTable.NewRow(); for (int j = 0; j < daReader.FieldCount; j++) { data = daReader[j].ToString(); dataRow[j] = data; } OutDataTable.Rows.Add(dataRow); } return OutDataTable; } } public static IEnumerable<T> GetEnumerable<T>(string sql) where T : new() { using (OracleConnection con = new OracleConnection(connStr)) { con.Open(); OracleCommand cmd = new OracleCommand(sql, con); OracleDataReader daReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); while (daReader.Read()) { var o = new T(); o.InjectFrom<ReaderInjection>(daReader); yield return o; } } } public static object ExecuteScalar(string sql) { using (OracleConnection con = new OracleConnection(connStr)) { con.Open(); OracleCommand cmd = new OracleCommand(sql, con); return cmd.ExecuteScalar(); } } public static void ExecuteNoneQuery(string sql) { using (OracleConnection con = new OracleConnection(connStr)) { con.Open(); OracleCommand cmd = new OracleCommand(sql, con); cmd.ExecuteNonQuery(); } } } }

以上代码中用到的ReaderInjection的代码如下

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Omu.ValueInjecter;

namespace Common.Data
{
    public class ReaderInjection : KnownSourceValueInjection<IDataReader>
    {
        protected override void Inject(IDataReader source, object target)
        {
            for (var i = 0; i < source.FieldCount; i++)
            {
                var activeTarget = target.GetProps().GetByName(source.GetName(i),true);
                if (activeTarget == null) continue;

                var value = source.GetValue(i);
                if (value == DBNull.Value) continue;

                activeTarget.SetValue(target, value);
            }
        }
    }
}

 

OracleDBHeper

标签:datarow   use   HERE   code   ica   open()   rabl   bsp   rom   

人气教程排行