• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

(转)OraDBHelper.csasp.net通用oracle数据库读写类

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

Web.config配置文件中节点

<connectionStrings>

<add name="ConnectionString" connectionString="Data Source=jde;Persist Security Info=True;User ID=jde;Password=jde;Unicode=True" providerName="System.Data.OracleClient"/>

</connectionStrings>

以上是oracle数据库的连接字符串

name:连接字符串名

connectionString:连接字符串

Data Souuce:本地机:就是数据库名;远程机子:如果在本地装有oracle数据库的客户端则也是数据库名,否则参考网络资源要加一个域。

User ID:用户名

 

 

 

OraDBHelper.cs文件

*******************************************************************************

using System;

using System.Data;

using System.Configuration;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Data.OracleClient;  

 

/// <summary>

///OraDBHelper 的摘要说明

/// </summary>

 

    public class OraDBHelper

    {

        public static OracleCommand cmd = null;

        public static OracleConnection conn = null;

        public static string connstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        public OraDBHelper()

        { }

        #region 建立数据库连接对象

        /// <summary>  

        /// 建立数据库连接  

        /// </summary>  

        /// <returns>返回一个数据库的连接OracleConnection对象</returns>  

        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对象

        /// <summary>  

        /// 设置OracleCommand对象         

        /// </summary>  

        /// <param name="cmd">OracleCommand对象 </param>  

        /// <param name="cmdText">命令文本</param>  

        /// <param name="cmdType">命令类型</param>  

        /// <param name="cmdParms">参数集合</param>  

        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 执行相应的sql语句,返回相应的DataSet对象

        /// <summary>  

        /// 执行相应的sql语句,返回相应的DataSet对象  

        /// </summary>  

        /// <param name="sqlstr">sql语句</param>  

        /// <returns>返回相应的DataSet对象</returns>  

        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对象

        /// <summary>  

        /// 执行相应的sql语句,返回相应的DataSet对象  

        /// </summary>  

        /// <param name="sqlstr">sql语句</param>  

        /// <param name="tableName">表名</param>  

        /// <returns>返回相应的DataSet对象</returns>  

        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语句,返回所影响的行数

        /// <summary>  

        /// 执行不带参数sql语句,返回所影响的行数  

        /// </summary>  

        /// <param name="cmdstr">增,删,改sql语句</param>  

        /// <returns>返回所影响的行数</returns>  

        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 执行带参数sql语句或存储过程,返回所影响的行数

        /// <summary>  

        /// 执行带参数sql语句或存储过程,返回所影响的行数  

        /// </summary>  

        /// <param name="cmdText">带参数的sql语句和存储过程名</param>  

        /// <param name="cmdType">命令类型</param>  

        /// <param name="cmdParms">参数集合</param>  

        /// <returns>返回所影响的行数</returns>  

        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对象

        /// <summary>  

        /// 执行不带参数sql语句,返回一个从数据源读取数据的OracleDataReader对象  

        /// </summary>  

        /// <param name="cmdstr">相应的sql语句</param>  

        /// <returns>返回一个从数据源读取数据的OracleDataReader对象</returns>  

        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对象

        /// <summary>  

        /// 执行带参数的sql语句或存储过程,返回一个从数据源读取数据的OracleDataReader对象  

        /// </summary>  

        /// <param name="cmdText">sql语句或存储过程名</param>  

        /// <param name="cmdType">命令类型</param>  

        /// <param name="cmdParms">参数集合</param>  

        /// <returns>返回一个从数据源读取数据的OracleDataReader对象</returns>  

        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

        /// <summary>  

        /// 执行不带参数sql语句,返回结果集首行首列的值object  

        /// </summary>  

        /// <param name="cmdstr">相应的sql语句</param>  

        /// <returns>返回结果集首行首列的值object</returns>  

        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

        /// <summary>  

        /// 执行带参数sql语句或存储过程,返回结果集首行首列的值object  

        /// </summary>  

        /// <param name="cmdText">sql语句或存储过程名</param>  

        /// <param name="cmdType">命令类型</param>  

        /// <param name="cmdParms">返回结果集首行首列的值object</param>  

        /// <returns></returns>  

        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数据库的一个DBHelp类,方法参考来源于网络

<connectionStrings>

<add name="ConnectionString" connectionString="Data Source=jde;Persist Security Info=True;User ID=jde;Password=jde;Unicode=True" providerName="System.Data.OracleClient"/>

</connectionStrings>

以上是oracle数据库的连接字符串

name:连接字符串名

connectionString:连接字符串

Data Souuce:本地机:就是数据库名;远程机子:如果在本地装有oracle数据库的客户端则也是数据库名,否则参考网络资源要加一个域。

User ID:用户名

 

 

 

OraDBHelper.cs文件

*******************************************************************************

using System;

using System.Data;

using System.Configuration;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Data.OracleClient;  

 

/// <summary>

///OraDBHelper 的摘要说明

/// </summary>

 

    public class OraDBHelper

    {

        public static OracleCommand cmd = null;

        public static OracleConnection conn = null;

        public static string connstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        public OraDBHelper()

        { }

        #region 建立数据库连接对象

        /// <summary>  

        /// 建立数据库连接  

        /// </summary>  

        /// <returns>返回一个数据库的连接OracleConnection对象</returns>  

        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对象

        /// <summary>  

        /// 设置OracleCommand对象         

        /// </summary>  

        /// <param name="cmd">OracleCommand对象 </param>  

        /// <param name="cmdText">命令文本</param>  

        /// <param name="cmdType">命令类型</param>  

        /// <param name="cmdParms">参数集合</param>  

        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 执行相应的sql语句,返回相应的DataSet对象

        /// <summary>  

        /// 执行相应的sql语句,返回相应的DataSet对象  

        /// </summary>  

        /// <param name="sqlstr">sql语句</param>  

        /// <returns>返回相应的DataSet对象</returns>  

        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对象

        /// <summary>  

        /// 执行相应的sql语句,返回相应的DataSet对象  

        /// </summary>  

        /// <param name="sqlstr">sql语句</param>  

        /// <param name="tableName">表名</param>  

        /// <returns>返回相应的DataSet对象</returns>  

        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语句,返回所影响的行数

        /// <summary>  

        /// 执行不带参数sql语句,返回所影响的行数  

        /// </summary>  

        /// <param name="cmdstr">增,删,改sql语句</param>  

        /// <returns>返回所影响的行数</returns>  

        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 执行带参数sql语句或存储过程,返回所影响的行数

        /// <summary>  

        /// 执行带参数sql语句或存储过程,返回所影响的行数  

        /// </summary>  

        /// <param name="cmdText">带参数的sql语句和存储过程名</param>  

        /// <param name="cmdType">命令类型</param>  

        /// <param name="cmdParms">参数集合</param>  

        /// <returns>返回所影响的行数</returns>  

        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对象

        /// <summary>  

        /// 执行不带参数sql语句,返回一个从数据源读取数据的OracleDataReader对象  

        /// </summary>  

        /// <param name="cmdstr">相应的sql语句</param>  

        /// <returns>返回一个从数据源读取数据的OracleDataReader对象</returns>  

        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对象

        /// <summary>  

        /// 执行带参数的sql语句或存储过程,返回一个从数据源读取数据的OracleDataReader对象  

        /// </summary>  

        /// <param name="cmdText">sql语句或存储过程名</param>  

        /// <param name="cmdType">命令类型</param>  

        /// <param name="cmdParms">参数集合</param>  

        /// <returns>返回一个从数据源读取数据的OracleDataReader对象</returns>  

        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

        /// <summary>  

        /// 执行不带参数sql语句,返回结果集首行首列的值object  

        /// </summary>  

        /// <param name="cmdstr">相应的sql语句</param>  

        /// <returns>返回结果集首行首列的值object</returns>  

        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

        /// <summary>  

        /// 执行带参数sql语句或存储过程,返回结果集首行首列的值object  

        /// </summary>  

        /// <param name="cmdText">sql语句或存储过程名</param>  

        /// <param name="cmdType">命令类型</param>  

        /// <param name="cmdParms">返回结果集首行首列的值object</param>  

        /// <returns></returns>  

        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数据库的一个DBHelp类,方法参考来源于网络


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap