oracle package OracleHelper

Packaging category 1.Oracle

the using the System;
 the using the System.Collections.Generic;
 the using the System.Linq;
 the using the System.Text;
 the using Oracle.ManagedDataAccess.Client;
 the using the System.Data;
 the using the System.Data.SqlClient; 

namespace OracleHelper 
{ 
    public  static  class OracleSQLHelper 
    { 
        // from profile acquisition variable readonly modified connection string can only be assigned in the initialization or constructor; can only be read elsewhere
         // Private static readonly string constr ConfigurationManager.ConnectionStrings = [ "OperationHistory"] the ConnectionString;.
         //Private static Readonly String constr = "the Data the Source = Dellys; the Initial Cataog = ceshi; the User = SA; PassWord = 123"; 
        Private  static  String constr = " the Data the Source = (the DESCRIPTION = (ADDRESS = (the PROTOCOL = the TCP) (the HOST = 130.147 .246.144) (PORT = 1521)) (the CONNECT_DATA = (SERVICE_NAME = ECMS))); the Persist Security Info = True; the User ID = System; Password = Service01; " ; 

        ///  <Summary> 
        /// add, delete, the method of the number of lines [the ExecuteNonQuery] returns affected changed, perform other
         ///  </ Summary> 
        ///  <param name = "SQL"> the Sql command </ param> 
        ///  <param name = "cmdtype"> SQL statements (CommandType.Text) or a stored procedure (CommandType.StoredProcedure) </ param>
        /// <param name="pms"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(string sql, CommandType cmdtype, params OracleParameter[] pms)
        {
            int i = -1;
            try
            {
                using (OracleConnection con = new OracleConnection(constr))
                {
                    using (OracleCommand cmd = new OracleCommand(sql, con))
                    {
                        //存储过程或者Sql语句
                        cmd.CommandType = cmdtype;
                        IF (PMS =! null ) 
                        { 
                            cmd.Parameters.AddRange (PMS); 
                        } 

                        con.Open (); 
                        I = the cmd.ExecuteNonQuery (); 
                    } 
                } 
            } 
            the catch (Exception) 
            { 
                the throw ; 
            } 

            return I; 
        } 

        ///  <Summary> 
        /// perform a query, the number of data query; return to the first row, first column method [the ExecuteScalar] -1 on failure 
         ///  </ Summary> 
        ///  <param name = "SQL">Sql命令</param>
        /// <param name="cmdtype">SQL语句(CommandType.Text)或者存储过程(CommandType.StoredProcedure)</param>
        /// <param name="pms"></param>
        /// <returns></returns>
        public static object ExecuteScalar(string sql, CommandType cmdtype, params OracleParameter[] pms)
        {
            try
            {
                using (OracleConnection con = new OracleConnection(constr))
                {
                    using (OracleCommand cmd = new OracleCommand(sql,with)) 
                        //
                    {Sql statement stored procedure or 
                        cmd.CommandType = cmdtype;
                         IF (PMS =! Null ) 
                        { 
                            cmd.Parameters.AddRange (PMS); 
                        } 

                        con.Open (); 
                        return cmd.ExecuteScalar (); 
                    } 
                } 
            } 
            the catch (Exception) 
            { 
                the throw ; 
            } 
        } 

        ///  <Summary> 
        /// perform a query return multiple rows, a plurality of columns approach the ExecuteReader ()
         ///  </ Summary> 
        /// <param name="sql">Sql命令</param>
        /// <param name="cmdtype">SQL语句(CommandType.Text)或者存储过程(CommandType.StoredProcedure)</param>
        /// <param name="pms"></param>
        /// <returns></returns>
        public static OracleDataReader ExecuteReader(string sql, CommandType cmdtype, params OracleParameter[] pms)
        {
            using (OracleConnection con = new OracleConnection(constr))
            {
                using (OracleCommand cmd = new OracleCommand(sql,CON)) 
                    stored procedure Sql statement or//
                {
                    cmd.CommandType = cmdtype;
                    if (pms != null)
                    {
                        cmd.Parameters.AddRange(pms);
                    }

                    try
                    {
                        con.Open();
                        return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
                    }
                    catch
                    {
                        con.Close();
                        con.Dispose();
                        throw;
                    }
                }
            }
        }

        /// <Summary> 
        /// performed using a plurality of return query, returns DataTable type
         ///  </ Summary> 
        ///  <param name = "SQL"> the Sql command </ param> 
        ///  <param name = "cmdtype "> the SQL statement (CommandType.Text) or a stored procedure (CommandType.StoredProcedure) </ param> 
        ///  <param name =" PMS "> parameters </ param> 
        ///  <Returns> </ Returns> 
        public  static the DataTable ExecuteDataTable ( String SQL, the CommandType cmdtype, the params the OracleParameter [] PMS) 
        { 
            the DataTable dt = new new the DataTable ();
            try
            {
                //通过adapter读取数据。
                using (OracleDataAdapter adapter = new OracleDataAdapter(sql, constr))
                {
                    adapter.SelectCommand.CommandType = cmdtype;
                    if (pms != null)
                    {
                        adapter.SelectCommand.Parameters.AddRange(pms);
                    }

                    adapter.Fill(dt);
                    return dt;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <Summary> 
        /// time required to obtain a plurality of result sets using this method, the object returns DataSet.
        ///  </ Summary> 
        ///  <param name = "SQL statement"> </ param> 
        ///  <Returns> </ Returns> 

        public  static the DataSet ExecuteDataSet ( String SQL, the params the OracleParameter [] PMS) 
        { 
            the using ( CON = the OracleConnection new new the OracleConnection (constr)) 
            { 
                // the data adapter 
                the OracleDataAdapter SQLDA = new new the OracleDataAdapter (SQL, CON); 
                sqlda.SelectCommand.Parameters.AddRange (PMS);The DataSet (); 
                sqlda.Fill (DS); 
                return DS;
                 // . No need to open and close link 
            } 
        } 
    } 
}

 

Guess you like

Origin www.cnblogs.com/yuanshuo/p/11528313.html