C# OracleHelper

C# 同时被 2 个专栏收录
67 篇文章 0 订阅
4 篇文章 0 订阅

 OracleHelper.cs


using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using Oracle.ManagedDataAccess.Client;

public class OraDBHelper
{
    public static OracleCommand cmd = null;
    public static OracleConnection conn = null;
    public static string connstr = ConfigurationManager.ConnectionStrings["defaultNew"].ConnectionString;
    public OraDBHelper()
    { }
    #region 建立Oracle数据库连接对象
    /// <returns>返回一个数据库连接的OracleConnection对象</returns>   
    public static OracleConnection init()
    {
        try
        {
            conn = new OracleConnection(connstr);
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
        }
        catch (Exception e)
        {
            throw new Exception(e.Message.ToString());
        }
        return conn;
    }
    #endregion
    #region 设置OracleCommand对象
    /// <param name="cmd">OracleCommand对象 </param>   
    /// <param name="cmdText">命令文本</param>   
    /// <param name="cmdType">命令类型</param>   
    /// <param name="cmdParms">参数集合</param>   
    private static void SetCommand(OracleCommand cmd, string cmdText, CommandType cmdType, OracleParameter[] cmdParms)
    {
        cmd.Connection = conn;
        cmd.CommandText = cmdText;
        cmd.CommandType = cmdType;
        if (cmdParms != null)
        {
            cmd.Parameters.AddRange(cmdParms);
        }
    }
    #endregion
    #region 执行相应的Oracle sql语句,返回相应的DataSet对象
    /// <param name="sqlstr">sql语句</param>   
    /// <returns>返回相应的DataSet对象</returns>   
    public static DataSet GetDataSet(string sqlstr)
    {
        DataSet set = new DataSet();
        try
        {
            init();
            OracleDataAdapter adp = new OracleDataAdapter(sqlstr, conn);
            adp.Fill(set);
            conn.Close();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message.ToString());
        }
        return set;
    }
    #endregion
    #region 执行sql语句,返回DataSet对象
    /// <param name="sqlstr">sql语句</param>   
    /// <param name="tableName">表名</param>   
    /// <returns>返回DataSet对象</returns>   
    public static DataSet GetDataSet(string sqlstr, string tableName)
    {
        DataSet set = new DataSet();
        try
        {
            init();
            OracleDataAdapter adp = new OracleDataAdapter(sqlstr, conn);
            adp.Fill(set, tableName);
            conn.Close();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message.ToString());
        }
        return set;
    }
    #endregion
    #region 执行不带参数的sql语句,返回受影响的行数
    /// <param name="cmdstr">增,删,改sql语句</param>   
    /// <returns>返回受影响的行数</returns>   
    public static int ExecuteNonQuery(string cmdText)
    {
        int count;
        try
        {
            init();
            cmd = new OracleCommand(cmdText, conn);
            count = cmd.ExecuteNonQuery();
            conn.Close();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message.ToString());
        }
        return count;
    }
    #endregion
    #region 执行带参数的Oracle sql语句或存储过程,返回行数
    /// <param name="cmdText">带参数的sql语句和存储过程名</param>   
    /// <param name="cmdType">命令类型</param>   
    /// <param name="cmdParms">参数集合</param>   
    /// <returns>返回受影响的行数</returns>   
    public static int ExecuteNonQuery(string cmdText, CommandType cmdType, OracleParameter[] cmdParms)
    {
        int count;
        try
        {
            init();
            cmd = new OracleCommand();
            SetCommand(cmd, cmdText, cmdType, cmdParms);
            count = cmd.ExecuteNonQuery();
            cmd.Parameters.Clear();
            conn.Close();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message.ToString());
        }
        return count;
    }
    #endregion
    #region 执行不带参数sql语句,返回一个从数据源读取数据的OracleDataReader对象
    /// <param name="cmdstr">相应的sql语句</param>   
    /// <returns>返回一个从数据源读取数据的OracleDataReader对象</returns>   
    public static OracleDataReader ExecuteReader(string cmdText)
    {
        OracleDataReader reader;
        try
        {
            init();
            cmd = new OracleCommand(cmdText, conn);
            reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message.ToString());
        }
        return reader;
    }
    #endregion
    #region 执行带参数的sql语句或存储过程,返回一个从数据源读取数据的OracleDataReader对象
    /// <param name="cmdText">sql语句或存储过程名</param>   
    /// <param name="cmdType">命令类型</param>   
    /// <param name="cmdParms">参数集合</param>   
    /// <returns>返回一个从数据源读取数据的OracleDataReader对象</returns>   
    public static OracleDataReader ExecuteReader(string cmdText, CommandType cmdType, OracleParameter[] cmdParms)
    {
        OracleDataReader reader;
        try
        {
            init();
            cmd = new OracleCommand();
            SetCommand(cmd, cmdText, cmdType, cmdParms);
            reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message.ToString());
        }
        return reader;
    }
    #endregion
    #region 执行不带参数sql语句,返回结果集首行首列的值object
    /// <param name="cmdstr">相应的sql语句</param>   
    /// <returns>返回结果集首行首列的值object</returns>   
    public static object ExecuteScalar(string cmdText)
    {
        object obj;
        try
        {
            init();
            cmd = new OracleCommand(cmdText, conn);
            obj = cmd.ExecuteScalar();
            conn.Close();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message.ToString());
        }
        return obj;
    }
    #endregion
    #region 执行带参数sql语句或存储过程,返回结果集首行首列的值object
    /// <param name="cmdText">sql语句或存储过程名</param>   
    /// <param name="cmdType">命令类型</param>   
    /// <param name="cmdParms">返回结果集首行首列的值object</param>   
    /// <returns></returns>   
    public static object ExecuteScalar(string cmdText, CommandType cmdType, OracleParameter[] cmdParms)
    {
        object obj;
        try
        {
            init();
            cmd = new OracleCommand();
            SetCommand(cmd, cmdText, cmdType, cmdParms);
            obj = cmd.ExecuteScalar();
            conn.Close();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message.ToString());
        }
        return obj;
    }
    #endregion
}

 

db.config

<?xml version="1.0" encoding="utf-8"?>
<connectionStrings>
  <add name="defaultNew" connectionString="Password=oracle;User ID=c##esd_monitor;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.34)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=XE)));" />
  <add name="default" connectionString="Provider=OraOLEDB.Oracle.1;Password=oracle;User ID=c##esd_monitor;Data Source=XE;Persist Security Info=True" providerName="System.Data.OleDb" />
</connectionStrings>

web.config

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data" requirePermission="true"/>
    <section name="oracle.manageddataaccess.client" type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.122.19.1, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
  </configSections>
  <appSettings>
    <add key="ISLDAPLogin" value="N"/>

  </appSettings>
  <dataConfiguration defaultDatabase="default">
    <providerMappings>
      <clear/>
    </providerMappings>
  </dataConfiguration>
  <connectionStrings configSource="db.config"/>
  <!--
    有关 web.config 更改的说明,请参见 http://go.microsoft.com/fwlink/?LinkId=235367。

    可在 <httpRuntime> 标记上设置以下特性。
      <system.Web>
        <httpRuntime targetFramework="4.5.2" />
      </system.Web>
  -->
  <system.web>
    <customErrors mode="Off"/>
    <httpRuntime maxQueryStringLength="10240" maxRequestLength="10240" executionTimeout="300"/>
    <!-- 限制10M 上传 -->
    <compilation debug="true" targetFramework="4.5.2"/>
  </system.web>

</configuration>

C#中调用

DataSet ds = OraDBHelper.GetDataSet("select * from SYSUSER t");
        public static Organization[] GetWorkLineBelongToOrg(string orgCode)
        { 
            string sqlstr = @"select *
                                from (select *
                                   from organization
                                   start with orgcode = :orgcode
                                   connect by porgcode = prior orgcode)
                                      where orgproperty = 'WorkLine'";

            OracleParameter[] op =  {
               new OracleParameter(":orgcode",orgCode),
            };
            DataTable dt = OraDBHelper.ExecuteDatatable(sqlstr, CommandType.Text, op);

            Organization[] org;
            org = EntityHelper.DatatableToEntity<Organization>(dt).ToArray(); 

            return org;
        }

 

  • 0
    点赞
  • 0
    评论
  • 3
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值