Oracle的DBHelper

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using Oracle.ManagedDataAccess.Client;

public class OraDBHelper
{
    public static OracleCommand cmd = null;
    public static OracleConnection conn = null;
    public static string connstr = ConfigurationManager.AppSettings["OracleString"];
    public OraDBHelper()
    { }
    #region 建立Oracle数据库连接对象
    /// 返回一个数据库连接的OracleConnection对象   
    public static OracleConnection init()
    {
        try
        {
            conn = new OracleConnection(connstr);
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
        }
        catch (Exception e)
        {
            throw new Exception(e.Message.ToString());
        }
        return conn;
    }
    #endregion
    #region 设置OracleCommand对象
    /// OracleCommand对象    
    /// 命令文本   
    /// 命令类型   
    /// 参数集合   
    private static void SetCommand(OracleCommand cmd, string cmdText, CommandType cmdType, OracleParameter[] cmdParms)
    {
        cmd.Connection = conn;
        cmd.CommandText = cmdText;
        cmd.CommandType = cmdType;
        if (cmdParms != null)
        {
            cmd.Parameters.AddRange(cmdParms);
        }
    }
    #endregion
    #region 执行相应的Oracle sql语句,返回相应的DataSet对象
    /// sql语句   
    /// 返回相应的DataSet对象   
    public static DataSet GetDataSet(string sqlstr)
    {
        DataSet set = new DataSet();
        try
        {
            init();
            OracleDataAdapter adp = new OracleDataAdapter(sqlstr, conn);
            adp.Fill(set);
            conn.Close();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message.ToString());
        }
        return set;
    }
    #endregion
    #region 执行sql语句,返回DataSet对象
    /// sql语句   
    /// 表名   
    /// 返回DataSet对象   
    public static DataSet GetDataSet(string sqlstr, string tableName)
    {
        DataSet set = new DataSet();
        try
        {
            init();
            OracleDataAdapter adp = new OracleDataAdapter(sqlstr, conn);
            adp.Fill(set, tableName);
            conn.Close();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message.ToString());
        }
        return set;
    }
    #endregion
    #region 执行不带参数的sql语句,返回受影响的行数
    /// 增,删,改sql语句   
    /// 返回受影响的行数   
    public static int ExecuteNonQuery(string cmdText)
    {
        int count;
        try
        {
            init();
            cmd = new OracleCommand(cmdText, conn);
            count = cmd.ExecuteNonQuery();
            conn.Close();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message.ToString());
        }
        return count;
    }
    #endregion
    #region 执行带参数的Oracle sql语句或存储过程,返回行数
    /// 带参数的sql语句和存储过程名   
    /// 命令类型   
    /// 参数集合   
    /// 返回受影响的行数   
    public static int ExecuteNonQuery(string cmdText, CommandType cmdType, OracleParameter[] cmdParms)
    {
        int count;
        try
        {
            init();
            cmd = new OracleCommand();
            SetCommand(cmd, cmdText, cmdType, cmdParms);
            count = cmd.ExecuteNonQuery();
            cmd.Parameters.Clear();
            conn.Close();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message.ToString());
        }
        return count;
    }
    #endregion
    #region 执行不带参数sql语句,返回一个从数据源读取数据的OracleDataReader对象
    /// 相应的sql语句   
    /// 返回一个从数据源读取数据的OracleDataReader对象   
    public static OracleDataReader ExecuteReader(string cmdText)
    {
        OracleDataReader reader;
        try
        {
            init();
            cmd = new OracleCommand(cmdText, conn);
            reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message.ToString());
        }
        return reader;
    }
    #endregion
    #region 执行带参数的sql语句或存储过程,返回一个从数据源读取数据的OracleDataReader对象
    /// sql语句或存储过程名   
    /// 命令类型   
    /// 参数集合   
    /// 返回一个从数据源读取数据的OracleDataReader对象   
    public static OracleDataReader ExecuteReader(string cmdText, CommandType cmdType, OracleParameter[] cmdParms)
    {
        OracleDataReader reader;
        try
        {
            init();
            cmd = new OracleCommand();
            SetCommand(cmd, cmdText, cmdType, cmdParms);
            reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message.ToString());
        }
        return reader;
    }
    #endregion
    #region 执行不带参数sql语句,返回结果集首行首列的值object
    /// 相应的sql语句   
    /// 返回结果集首行首列的值object   
    public static object ExecuteScalar(string cmdText)
    {
        object obj;
        try
        {
            init();
            cmd = new OracleCommand(cmdText, conn);
            obj = cmd.ExecuteScalar();
            conn.Close();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message.ToString());
        }
        return obj;
    }
    #endregion
    #region 执行带参数sql语句或存储过程,返回结果集首行首列的值object
    /// sql语句或存储过程名   
    /// 命令类型   
    /// 返回结果集首行首列的值object   
    ///    
    public static object ExecuteScalar(string cmdText, CommandType cmdType, OracleParameter[] cmdParms)
    {
        object obj;
        try
        {
            init();
            cmd = new OracleCommand();
            SetCommand(cmd, cmdText, cmdType, cmdParms);
            obj = cmd.ExecuteScalar();
            conn.Close();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message.ToString());
        }
        return obj;
    }
    #endregion
}

你可能感兴趣的:(Oracle,连接数据库)