C# OracleDBhelper

引用

using System.Configuration;
using System.Data.OracleClient;

连接字符串

<connectionStrings>
    <add name="OracleConnString" connectionString="User ID=xxx;Password=xxx;Data Source=(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST=IP地址)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=orcl)))" />
</connectionStrings>

    public static class OracleDBhelper
    {
        private static OracleConnection cnn = null;
        /// <summary>
        /// 连接数据库
        /// </summary>
        public static OracleConnection Cnn
        {
            get
            {
                if (cnn == null)
                {
                    string cnnstr = ConfigurationManager.ConnectionStrings["OracleConnString"].ConnectionString;
                    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;
            }
        }

        /// <summary>
        /// int 增删改功能的Oracle语句。实现增删改功能,返回受影响行数。
        /// </summary>
        /// <param name="Oracle"></param>
        /// <returns></returns>
        public static int ExecuteCommand(string Oracle)
        {
            int intResult = 0;
            try
            {
                string cnnstr = ConfigurationManager.ConnectionStrings["OracleConnString"].ConnectionString;
                Console.Write(cnnstr);
                cnn = new OracleConnection(cnnstr);
                cnn.Open();
                OracleCommand cmm = new OracleCommand(Oracle, cnn);
                intResult = cmm.ExecuteNonQuery();
                cmm.Dispose();
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                throw ex;
            }
            finally
            {
                cnn.Close();
                cnn.Dispose();
            }
            return intResult;
        }

        /// <summary>
        /// int 增删改功能的Oracle语句。实现增删改功能,返回受影响行数。
        /// [where条件时绑定数据,防止注入式攻击]
        /// </summary>
        /// <param name="Oracle"></param>
        /// <returns></returns>
        public static int ExecuteCommand(string Oracle, OracleParameter[] sp)
        {
            int intResult = 0;
            try
            {
                OracleCommand cmm = new OracleCommand(Oracle, Cnn);
                cmm.Parameters.AddRange(sp);
                intResult = cmm.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                cnn.Close();
                cnn.Dispose();
            }
            return intResult;
        }

        /// <summary>
        /// string 查询返回单行单列的Oracle语句。( 例:select count(*) from 表名或 select name from 表名 )。
        /// </summary>
        /// <param name="Oracle"></param>
        /// <returns></returns>
        public static string ExecuteScalar(string Oracle)
        {
            OracleCommand cmm = new OracleCommand(Oracle, Cnn);
            return cmm.ExecuteScalar().ToString();
        }

        /// <summary>
        /// string 查询返回单行单列的Oracle语句。( 例:select count(*) from 表名或 select name from 表名 )。
        ///  [where条件时绑定数据,防止注入式攻击]
        /// </summary>
        /// <param name="Oracle"></param>
        /// <returns></returns>
        public static string ExecuteScalar(string Oracle, OracleParameter[] sp)
        {
            OracleCommand cmm = new OracleCommand(Oracle, Cnn);
            cmm.Parameters.AddRange(sp);
            return cmm.ExecuteScalar().ToString();
        }

        /// <summary>
        /// OracleDataReader 查询返回符合条件的记录 每次返回一行
        /// </summary>
        /// <param name="Oracle"></param>
        /// <returns></returns>
        public static OracleDataReader ExecuteDataReader(string Oracle)
        {
            OracleCommand cmm = new OracleCommand(Oracle, Cnn);
            return cmm.ExecuteReader();
        }

        /// <summary>
        /// OracleDataReader 查询返回符合条件的记录 每次返回一行.
        ///  [where条件时绑定数据,防止注入式攻击]
        /// </summary>
        /// <param name="Oracle"></param>
        /// <returns></returns>
        public static OracleDataReader ExecuteDataReader(string Oracle, OracleParameter[] sp)
        {
            OracleCommand cmm = new OracleCommand(Oracle, Cnn);
            cmm.Parameters.AddRange(sp);
            return cmm.ExecuteReader();
        }

        /// <summary>
        /// DataTable 查询返回符合条件的记录表
        /// </summary>
        /// <param name="Oracle"></param>
        /// <returns></returns>
        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];
        }

        /// <summary>
        /// DataTable 查询返回符合条件的记录表
        ///  [where条件时绑定数据,防止注入式攻击]
        /// </summary>
        /// <param name="Oracle"></param>
        /// <returns></returns>
        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];
        }

        /// <summary>
        /// DataTable 查询返回DataTable 带有分页参数,startRecord 开始记录数,maxRecords 最大记录数 适用于分页控件
        /// startRecord :(AspNetPager1.CurrnetPageIndex - 1 )*AspNetPager1.Pagesize
        ///  maxRecords:AspNetPager1.pagesize
        /// </summary>
        /// <param name="Oracle"></param>
        /// <param name="startRecord"></param>
        /// <param name="maxRecords"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// DataTable 查询返回【存储过程的Oracle语句】
        ///
        /// </summary>
        /// <param name="Oracle"></param>
        /// <returns></returns>
        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];
        }
    }

时间: 2024-10-11 11:04:24

C# OracleDBhelper的相关文章

关于Oracle和SQLServer数据库在.net中拼接数据库语句的不同

1 string sql = "insert into personinfo " + 2 "(id,employeename,employeecode, " + 3 "values(:id,:employeename,:employeecode, " )"; 4 OracleParameter[] pars = { new OracleParameter("id",t.Id), 5 new OracleParamet

我也来学着写写WINDOWS服务-解析xml抓取数据并插入数据库

项目告一段落,快到一年时间开发了两个系统,一个客户已经在试用,一个进入客户测试阶段,中间突然被项目经理(更喜欢叫他W工)分派一个每隔两小时用windows服务去抓取客户提供的外网xml,解析该xml,然后将截取的节点字段值插入到已经建好相应结构的表中.并记录日志. xml结构概要如下: <?xml version="1.0" encoding="UTF-8"?> <list> <info> <id>93ef7c7ccc

基于Windows服务的WCF

(1)创建WCF 代码示例: [ServiceContract] public interface ILimsDBService { [OperationContract] int ExecuteSql(string strSql); [OperationContract] DataTable GetDataTable(string strSql); [OperationContract] DataSet GetDataSet(string strSql); } public class Lim

基于ManagedDataAccess开发的OracleDBHelpe工具集伸手党的福音

在使用前先加入ManagedDataAccessDLL文件方可使用 添加方法:右键项目.点击管理NuGet程序包,点击浏览,在输入框内输入ManagedDataAccess,再点击安装即可 OracleDBHelper代码如下(Copy直接用) /// <summary> ///基於.net( 向下兼容4.0)開發 OracleDBHelper工具類 ///<para>作者: 害羞的青蛙</para> ///<para>時間: 2019-12-6</p