OracleProcedureHelper

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.OracleClient;
using System.Data;

namespace WMSWebReport.DBUtility
{
    public class OracleProcedureHelper
    {
    //    public static readonly string ConnectionString = ConfigurationSettings.AppSettings["DefaultConnetion"];

        private OracleConnection GetConnection()
        {
            string ConnString = ConfigurationSettings.AppSettings["DefaultConnetion"];
            OracleConnection conn = new OracleConnection(ConnString);
            return conn;
        }

        /// <summary>
        /// 執行procedure,返回DataSet數據集
        /// </summary>
        /// <param name="spName">procedure名字</param>
        /// <param name="parameters">OracleParameter的參數數組</param>
        /// <returns></returns>

        public DataSet ExecuteSP(string spName, OracleParameter[] parameters)
        {
            using (OracleConnection conn = GetConnection())
            {
                conn.Open();
                DataSet ds = new DataSet();
                OracleCommand cmd = new OracleCommand(spName, conn);
                cmd.CommandType = CommandType.StoredProcedure;


                foreach (OracleParameter para in parameters)
                {
                    cmd.Parameters.Add(para);
                }
                try
                {
                    OracleDataAdapter da = new OracleDataAdapter(cmd);
                   
                    da.Fill(ds);
                   
                }
                catch (System.Data.OracleClient.OracleException e)
                {
                    throw new Exception(e.Message);
                }
                finally
                {
                    conn.Close();
                }
                return ds;
            }
        }

         ///執行返回結果為STRING 型的Procedure
         ///
        public string ExecuteSP1(string spName, OracleParameter[] parameters)
        {
           
            using (OracleConnection conn = GetConnection())
            {
                conn.Open();
                string result ="";
                OracleCommand cmd = new OracleCommand(spName, conn);
                cmd.CommandType = CommandType.StoredProcedure;

                foreach (OracleParameter para in parameters)
                {
                    cmd.Parameters.Add(para);
                }

                try
                {
                    cmd.ExecuteNonQuery();
                    result = parameters[parameters.Length - 1].Value.ToString();
                    return result;
                }
                catch (System.Data.OracleClient.OracleException e)
                {
                    throw new Exception(e.Message);
                }
                finally
                {
                    conn.Close();
                }
                return result;
            }
        }
        ///執行返回結果為INT 型的
        ///
        public int Execute(string sql)
        {
            using (OracleConnection conn = GetConnection())
            {
                conn.Open();
                int result;
                OracleCommand cmd = new OracleCommand(sql, conn);
                //cmd.CommandType = CommandType.StoredProcedure;

                //foreach (OracleParameter para in parameters)
                //{
                //    cmd.Parameters.Add(para);
                //}
                try
                {
                    result = cmd.ExecuteNonQuery();
                    //result = parameters[parameters.Length - 1].Value.ToString();
                    return result;
                }
                catch (System.Data.OracleClient.OracleException e)
                {
                    throw new Exception(e.Message);
                }
                finally
                {
                    conn.Close();
                }
            }
        }

        ///執行返回結果為COUNT
        ///
        public DataSet Executeselectwmscount(string sql)
        {
            using (OracleConnection conn = GetConnection())
            {
                conn.Open();
                int result;
                DataSet ds = new DataSet();
                OracleCommand cmd = new OracleCommand(sql, conn);
                try
                {
                    OracleDataAdapter da = new OracleDataAdapter(cmd);
                   cmd.ExecuteReader();
                   da.Fill(ds);
                    //result = parameters[parameters.Length - 1].Value.ToString();
                    return ds;
                }
                catch (System.Data.OracleClient.OracleException e)
                {
                    throw new Exception(e.Message);
                }
                finally
                {
                    conn.Close();
                }
            }
        }


        public bool iSelect(string sql) // add by chao.su 2014/09/09
        {
            using (OracleConnection conn = GetConnection())
            {
                try
                {
                    conn.Open();
                    OracleCommand cmd = new OracleCommand(sql, conn);
                    //OracleDataReader dr = cmd.ExecuteReader();
                    object o = cmd.ExecuteScalar();
                    //int count=Convert.ToInt32(cmd.ExecuteScalar());//cmd为Sqlcommand对象if (count>0){   //有,给出提示}else{  //无,给无提示}
                    if (o==null)
                    {
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                   
                }
                catch (System.Data.OracleClient.OracleException e)
                {
                    throw new Exception(e.Message);
                }
                finally
                {
                    conn.Close();
                }
            }
        }
    }
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。