Oracle在.NET里面的DBHelper、OracleDBHelper

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using ADODB;
using System.Data.OracleClient;
//return new OracleConnection(ConfigurationManager.ConnectionStrings["OracleConnectionString"].ConnectionString);
namespace EIPWebService.DAL
{
    public class DBHelperOracle
    {
        private static OracleConnection cnn = null;
        /// 
        /// 连接数据库
        /// 
        public static OracleConnection Cnn
        {
            get
            {
                if (cnn == null)
                {
                    cnn = new OracleConnection(@"Data Source=数据库名;User Id=用户名;Password=密码;");

                    //string cnnstr = ConfigurationManager.ConnectionStrings["cnnstr"].ToString();
                    //cnn = new OracleConnection(cnnstr);
                    cnn.Open();

                }
                else if (cnn.State == ConnectionState.Closed)
                {
                    cnn.Open();
                }
                else if (cnn.State == ConnectionState.Broken)
                {
                    cnn.Close();
                    cnn.Open();
                }
                return cnn;
            }
        }
        /// 
        /// int 增删改功能的Oracle语句。实现增删改功能,返回受影响行数。
        /// 
        /// 
        /// 
        public static int ExecuteCommand(string Oracle)
        {
            OracleCommand cmm = new OracleCommand(Oracle, Cnn);
            return cmm.ExecuteNonQuery();
        }

        /// 
        /// int 增删改功能的Oracle语句。实现增删改功能,返回受影响行数。
        /// [where条件时绑定数据,防止注入式攻击]
        /// 
        /// 
        /// 
        public static int ExecuteCommand(string Oracle, OracleParameter[] sp)
        {
            OracleCommand cmm = new OracleCommand(Oracle, Cnn);
            cmm.Parameters.AddRange(sp);
            return cmm.ExecuteNonQuery();
        }

        /// 
        /// string 查询返回单行单列的Oracle语句。( 例:select count(*) from 表名或 select name from 表名 )。
        /// 
        /// 
        /// 
        public static string ExecuteScalar(string Oracle)
        {
            OracleCommand cmm = new OracleCommand(Oracle, Cnn);
            return cmm.ExecuteScalar().ToString();
        }
        /// 
        /// string 查询返回单行单列的Oracle语句。( 例:select count(*) from 表名或 select name from 表名 )。
        ///  [where条件时绑定数据,防止注入式攻击]
        /// 
        /// 
        /// 
        public static string ExecuteScalar(string Oracle, OracleParameter[] sp)
        {
            OracleCommand cmm = new OracleCommand(Oracle, Cnn);
            cmm.Parameters.AddRange(sp);
            return cmm.ExecuteScalar().ToString();
        }
        /// 
        /// OracleDataReader 查询返回符合条件的记录 每次返回一行
        /// 
        /// 
        /// 
        public static OracleDataReader ExecuteDataReader(string Oracle)
        {
            OracleCommand cmm = new OracleCommand(Oracle, Cnn);
            return cmm.ExecuteReader();
        }
        /// 
        /// OracleDataReader 查询返回符合条件的记录 每次返回一行.
        ///  [where条件时绑定数据,防止注入式攻击]
        /// 
        /// 
        /// 
        public static OracleDataReader ExecuteDataReader(string Oracle, OracleParameter[] sp)
        {
            OracleCommand cmm = new OracleCommand(Oracle, Cnn);
            cmm.Parameters.AddRange(sp);
            return cmm.ExecuteReader();
        }
        /// 
        /// DataTable 查询返回符合条件的记录表
        /// 
        /// 
        /// 
        public static DataTable ExcuteDataTable(string Oracle)
        {
            OracleCommand cmm = new OracleCommand(Oracle, Cnn);
            OracleDataAdapter da = new OracleDataAdapter(cmm);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds.Tables[0];
        }

        /// 
        /// DataTable 查询返回符合条件的记录表
        ///  [where条件时绑定数据,防止注入式攻击]
        /// 
        /// 
        /// 
        public static DataTable ExcuteDataTable(string Oracle, OracleParameter[] sp)
        {
            OracleCommand cmm = new OracleCommand(Oracle, Cnn);
            cmm.Parameters.AddRange(sp);
            OracleDataAdapter da = new OracleDataAdapter(cmm);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds.Tables[0];
        }

        /// 
        /// DataTable 查询返回DataTable 带有分页参数,startRecord 开始记录数,maxRecords 最大记录数 适用于分页控件
        /// startRecord :(AspNetPager1.CurrnetPageIndex - 1 )*AspNetPager1.Pagesize
        ///  maxRecords:AspNetPager1.pagesize
        /// 
        /// 
        /// 
        /// 
        /// 
        public static DataTable DataTablePage(string Oracle, int startRecord, int maxRecords)
        {
            OracleCommand cmm = new OracleCommand(Oracle, Cnn);
            OracleDataAdapter sdp = new OracleDataAdapter(cmm);
            DataTable dt = new DataTable();
            sdp.Fill(startRecord, maxRecords, dt);
            return dt;
        }


        /// 
        /// DataTable 查询返回【存储过程的Oracle语句】
        /// 
        /// 
        /// 
        /// 
        public static DataTable ExcuteDataTableByProcedure(string procedure, OracleParameter[] sp)
        {
            OracleCommand cmm = new OracleCommand(procedure, Cnn);
            cmm.CommandType = CommandType.StoredProcedure;
            cmm.Parameters.AddRange(sp);
            OracleDataAdapter da = new OracleDataAdapter(cmm);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds.Tables[0];
        }
    }
}


转载于:https://www.cnblogs.com/wybshyy/p/5847846.html

你可能感兴趣的:(人工智能,数据库)