C # SQL helper

 

C # SQL helper

I own package SQLHelper classes to perform CRUD sql server data with Oracle

vs Oracle's own database references need to install the Oracle client, such as do not want to install the Oracle client, you can reference  Oracle.ManagedDataAccess.dll

Download Link: https://pan.baidu.com/s/1dzzTxL2fmUPrMy2Qc246jg extraction code: 16my 

public  class SQLHelper 
    { 
        ///  <Summary> 
        /// execute a query
         ///  </ Summary> 
        ///  <param name = "ConnStr"> connection string </ param> 
        ///  <param name = "the Sql "> SQL statement </ param> 
        ///  <param name =" param "> field name query </ param> 
        ///  <returns a> query results or error messages </ returns a> 
        public  static  String [] ExecuteReaderSql ( String ConnStr, String Sql,string[] Param)
        {
            string[] info = new string[Param.Count()];
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(ConnStr);
                conn.Open();
                SqlCommand cmd = new SqlCommand(Sql, conn);
                SqlDataReader sdr = cmd.ExecuteReader();
                sdr.Read();
                if (!sdr.HasRows) { info[0] = "null"; return info; }
                for (int i = 0; I <Param.Count (); I ++ ) 
                { 
                    info [I] = . SDR [the Param [I]] the ToString (); 
                } 
            } 
            the catch (Exception EX) 
            { 
                info [ 0 ] = " SQL Query error message: " + ex.Message;
                 return info; 
            } 
            the finally 
            { 
                conn.Close (); 
            } 

            return info; 
        } 
        ///  <Summary> 
        /// perform CRUD
         ///  </ Summary> 
        /// <param name = "ConnStr"> connection string </ param> 
        ///  <param name = "the Sql"> SQL statement </ param> 
        ///  <Returns> affected by the number of rows or error messages </ returns> 
        public  static  String ExecuteSQL ( String ConnStr, String the Sql) 
        { 
            the SqlConnection Conn = null ;
             int Row = 0 ;
             the try 
            { 
                Conn = new new the SqlConnection (ConnStr); 
                conn.Open ();
                SqlCommand cmd = new new the SqlCommand (the Sql, Conn); 
                Row = the cmd.ExecuteNonQuery (); 
            } 
            the catch (Exception EX) 
            { 
                return  " SQL execution error message: " + ex.Message; 
            } 
            the finally 
            { 
                conn.Close (); 
            } 
            return Row.ToString (); 
        } 
        ///  <Summary> 
        /// the Oracle CRUD
         ///  </ Summary> 
        ///  <param name = "ConnStr"> connection string </ param> 
        ///  <param name = "ORCL "> the Oracle statement</param>
        /// <returns>受影响行数或错误信息</returns>
        public static string ExecuteOrcl(string ConnStr, string orcl)
        {
            OracleConnection conn = null;
            int Row = 0;
            try
            {
                conn = new OracleConnection(ConnStr);
                conn.Open();
                OracleCommand cmd = new OracleCommand(orcl, conn);
                Row =the cmd.ExecuteNonQuery (); 
            } 
            the catch (Exception EX) 
            { 
                return  " ORCL execution error message: " + ex.Message; 
            } 
            the finally 
            { 
                conn.Close (); 
            } 
            return Row.ToString (); 
        } 
        ///  <Summary> 
        /// the Oracle query
         ///  </ Summary> 
        ///  <param name = "ConnStr"> connection string </ param> 
        ///  <param name = "ORCL"> the Oracle statement </ param> 
        ///  <param name = "param">Query field name </ param>
        /// <returns>查询结果或错误信息</returns>
        public static string[] ExecuteReaderOrcl(string ConnStr, string orcl, string[] Param)
        {
            OracleConnection conn = null;
            string[] info = new string[Param.Count()];
            try
            {
                conn = new OracleConnection(ConnStr);
                conn.Open();
                OracleCommand cmd = new OracleCommand(orcl, conn);
                OracleDataReader odr = cmd.ExecuteReader();
                odr.Read();
                if(!odr.HasRows) { info[0] = "null"; return info; }
                for (int i = 0; i < Param.Count(); i++)
                {
                    info[i] = odr[Param[i]].ToString();
                }
            }
            catch (Exception ex)
            {
                info[0] = "orcl查询错误信息:" + ex.Message;
                return info;
            }
            finally
            {
                conn.Close();
            }
            return info;
        }
    }
SqlHelper

 

Guess you like

Origin www.cnblogs.com/ocean-wang/p/10967808.html