精选国内外互联网行业最新文章及报告,让网友获得最新的海内外互联网动态
当前位置:主页 > 网络编程 > 数据库技术 >

oracle简单数据连接封装

2015-01-21 09:20 来源: 编辑:admin
public class OraDatabase 

    private OracleConnection OraConn; 
 
    private void Open() 
    { 
        if (OraConn == null) 
        { 
            OraConn = new OracleConnection(ConnectionInf.GetSFISConString()); 
        } 
        if (OraConn.State == System.Data.ConnectionState.Closed) 
            OraConn.Open(); 
    } 
 
    private void Close() 
    { 
        if (OraConn != null) 
            OraConn.Close(); 
    } 
 
    private void Dispose() 
    { 
        if (OraConn != null) 
        { 
            OraConn.Dispose(); 
            OraConn = null; 
        } 
    } 
 
 
    public  OracleCommand Oracmd(string ProcName, OracleParameter[] parms) 
    { 
        Open(); 
 
        OracleCommand cmd = new OracleCommand(ProcName, OraConn); 
        cmd.CommandType = CommandType.StoredProcedure; 
 
        if (parms != null) 
        { 
            foreach (OracleParameter parameter in parms) 
                cmd.Parameters.Add(parameter);          
        } 
                  
        return cmd; 
    } 
 
    public void RunProcedure(string ProcName) 
    { 
        OracleCommand cmd = Oracmd(ProcName, null); 
        cmd.ExecuteNonQuery(); 
        this.Close(); 
    } 
    //调用ORACLE函数,返回值  
    public string RunProcedure(string ProcName,OracleParameter [] parms) 
    { 
        string x = ""; 
        OracleCommand cmd = Oracmd(ProcName, parms); 
        //添加oracle返回参数  
        OracleParameter p = new OracleParameter("result", OracleType.Int32, 4); 
        p.Direction = System.Data.ParameterDirection.ReturnValue; 
        cmd.Parameters.Add(p); 
 
        cmd.ExecuteNonQuery(); 
        this.Close(); 
        x = cmd.Parameters["result"].Value.ToString(); 
        return x; 
       // return cmd.Parameters["result"].Value.ToString();  
       //result是ORACLE函数的自动返回值变量,类似于SQL SERVER中的returnvalue  
         
    } 
     
 
    public DataTable DT_Proc(string ProcName, OracleParameter[] parms) 
    { 
        OracleCommand cmd = Oracmd(ProcName, parms); 
        
        DataTable dt = null; 
        DataSet ds = new DataSet(); 
        OracleDataAdapter da = new OracleDataAdapter(cmd); 
        da.Fill(ds); 
        
        dt = ds.Tables[0]; 
        Close(); 
        return dt; 
    } 
 
    public OracleParameter MakeInParam(string ParamName, OracleType DbType, object Value) 
    { 
        return MakeParam(ParamName, DbType, ParameterDirection.Input, Value); 
    } 
 
 
    public OracleParameter MakeOutParam(string ParamName, OracleType DbType) 
    { 
        return MakeParam(ParamName, DbType,ParameterDirection.Output,null); 
    } 
 
    public OracleParameter MakeParam(string ParamName, OracleType DbType, ParameterDirection Direction, object Value) 
    { 
        OracleParameter param; 
 
        //if (Size > 0)  
        //    param = new OracleParameter(ParamName, DbType, Size);  
        //else  
        param = new OracleParameter(ParamName, DbType); 
 
        param.Direction = Direction; 
        if (!(Direction == ParameterDirection.Output && Value == null)) 
            param.Value = Value; 
 
        return param; 
    } 

    public class OraDatabase
    {
        private OracleConnection OraConn;
 
        private void Open()
        {
            if (OraConn == null)
            {
                OraConn = new OracleConnection(ConnectionInf.GetSFISConString());
            }
            if (OraConn.State == System.Data.ConnectionState.Closed)
                OraConn.Open();
        }
 
        private void Close()
        {
            if (OraConn != null)
                OraConn.Close();
        }
 
        private void Dispose()
        {
            if (OraConn != null)
            {
                OraConn.Dispose();
                OraConn = null;
            }
        }
 
 
        public  OracleCommand Oracmd(string ProcName, OracleParameter[] parms)
        {
            Open();
 
            OracleCommand cmd = new OracleCommand(ProcName, OraConn);
            cmd.CommandType = CommandType.StoredProcedure;
 
            if (parms != null)
            {
                foreach (OracleParameter parameter in parms)
                    cmd.Parameters.Add(parameter);        
            }
                    
            return cmd;
        }
 
        public void RunProcedure(string ProcName)
        {
            OracleCommand cmd = Oracmd(ProcName, null);
            cmd.ExecuteNonQuery();
            this.Close();
        }
        //调用ORACLE函数,返回值 www.bitsCN.com
        public string RunProcedure(string ProcName,OracleParameter [] parms)
        {
            string x = "";
            OracleCommand cmd = Oracmd(ProcName, parms);
            //添加oracle返回参数
            OracleParameter p = new OracleParameter("result", OracleType.Int32, 4);
            p.Direction = System.Data.ParameterDirection.ReturnValue;
            cmd.Parameters.Add(p);
 
            cmd.ExecuteNonQuery();
            this.Close();
            x = cmd.Parameters["result"].Value.ToString();
            return x;
           // return cmd.Parameters["result"].Value.ToString();
           //result是ORACLE函数的自动返回值变量,类似于SQL SERVER中的returnvalue
           
        }
       
 
        public DataTable DT_Proc(string ProcName, OracleParameter[] parms)
        {
            OracleCommand cmd = Oracmd(ProcName, parms);
          
            DataTable dt = null;
            DataSet ds = new DataSet();
            OracleDataAdapter da = new OracleDataAdapter(cmd);
            da.Fill(ds);
          
            dt = ds.Tables[0];
            Close();
            return dt;
        }
 
        public OracleParameter MakeInParam(string ParamName, OracleType DbType, object Value)
        {
            return MakeParam(ParamName, DbType, ParameterDirection.Input, Value);
        }
 
   
        public OracleParameter MakeOutParam(string ParamName, OracleType DbType)
        {
            return MakeParam(ParamName, DbType,ParameterDirection.Output,null);
        }
 
        public OracleParameter MakeParam(string ParamName, OracleType DbType, ParameterDirection Direction, object Value)
        {
            OracleParameter param;
 
            //if (Size > 0)
            //    param = new OracleParameter(ParamName, DbType, Size);
            //else
            param = new OracleParameter(ParamName, DbType);
 
            param.Direction = Direction;
            if (!(Direction == ParameterDirection.Output && Value == null))
                param.Value = Value;
 
            return param;
        }
    }
 
 
摘自 liu_liu213的专栏
标签
你喜欢的文章
返回首页
扫描微信
返回顶部