主页 > 知识库 > asp.net Oracle数据库访问操作类

asp.net Oracle数据库访问操作类

热门标签:怎样在地图标注消火栓图形 济源人工智能电话机器人价格 泰州手机外呼系统软件 内蒙古智能电销机器人哪家强 百度地图标注点击事件 地图标注位置多的钱 山东防封电销卡办理套餐 厦门四川外呼系统 杭州智能电话机器人

复制代码 代码如下:

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Data.OracleClient;
using System.Configuration;
using System.Data.Common;
using System.Collections.Generic;

    /// summary>
    /// 数据访问抽象基础类
    ///
    /// /summary>
public class DBBase
{


    //数据库连接字符串(web.config来配置),可以动态更改connectionString支持多数据库.       
    public static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString1"].ToString();
    public DBBase()
    {
    }

    #region 检查用户名是否存在
    /// summary>
    /// 检查用户名是否存在,存在返回true,不存在返回false
    /// /summary>
    /// param name="strSql">/param>
    /// returns>/returns>
    public static bool Exists(string strSql)
    {

        using (OracleConnection connection = new OracleConnection(connectionString))
        {
            connection.Open();
            OracleCommand myCmd = new OracleCommand(strSql, connection);
            try
            { 
                object obj = myCmd.ExecuteScalar(); //返回结果的第一行一列
                myCmd.Parameters.Clear();
                if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                {
                    return false;
                }
                else
                {
                    return true;
                }          
            }
            catch (Exception ex)
             {
                 throw ex;
             }
       }
    }

    #endregion

    #region  执行简单SQL语句 返回影响的记录数

    /// summary>
    /// 执行SQL语句,返回影响的记录数
    /// /summary>
    /// param name="SQLString">SQL语句/param>
    /// returns>影响的记录数/returns>
    public static int ExecuteSql(string SQLString)
    {

        OracleConnection connection = null;
        OracleCommand cmd = null;
        try
        {
            connection = new OracleConnection(connectionString);
            cmd = new OracleCommand(SQLString, connection);
            connection.Open();
            int rows = cmd.ExecuteNonQuery();
            return rows;
        }
        finally
        {
            if (cmd != null)
            {
                cmd.Dispose();
            }
            if (connection != null)
            {
                connection.Close();
                connection.Dispose();
            }
        }
    }
       #endregion

 

    #region   执行查询语句,返回SqlDataReader
    /// summary>
    /// 执行查询语句,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
    /// /summary>
    /// param name="strSQL">查询语句/param>
    /// returns>SqlDataReader/returns>
    public static OracleDataReader ExecuteReader(string strSQL)
    {
        OracleConnection connection = new OracleConnection(connectionString);
        OracleCommand cmd = new OracleCommand(strSQL, connection);
        try
        {
            connection.Open();
            OracleDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            return myReader;
        }
        catch (System.Data.OracleClient.OracleException e)
        {
            throw e;
        }
        finally
        {
            connection.Close();

        }
    }
    #endregion

    #region  执行SQL查询语句,返回DataTable数据表
    /// summary>
    /// 执行SQL查询语句
    /// /summary>
    /// param name="sqlStr">/param>
    /// returns>返回DataTable数据表/returns>
    public static DataTable GetDataTable(string sqlStr)
    {
        OracleConnection mycon = new OracleConnection(connectionString);
        OracleCommand mycmd = new OracleCommand(sqlStr, mycon);
        DataTable dt = new DataTable();
        OracleDataAdapter da = null;
        try
        {
            mycon.Open();
            da = new OracleDataAdapter(sqlStr, mycon);
            da.Fill(dt);


        }
        catch (Exception ex)
        {

            throw new Exception(ex.ToString());
        }
        finally
        {
            mycon.Close();
        }
        return dt;
    }
    #endregion

    #region 存储过程操作
    /// summary>
    ///  运行存储过程,返回datatable;
    /// /summary>
    /// param name="storedProcName">存储过程名称/param>
    /// param name="parameters">参数/param>
    /// returns>/returns>
    public static DataTable RunProcedureDatatable(string storedProcName, IDataParameter[] parameters)
    {
        using (OracleConnection connection = new OracleConnection(connectionString))
        {
            DataSet ds = new DataSet();
            connection.Open();
            OracleDataAdapter sqlDA = new OracleDataAdapter();
            sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
            sqlDA.Fill(ds);
            connection.Close();
            return ds.Tables[0];
        }
    }
    /// summary>
    /// 执行存储过程
    /// /summary>
    /// param name="storedProcName">存储过程名称/param>
    /// param name="parameters">参数/param>
    /// returns>/returns>
    public static int RunProcedure(string storedProcName, IDataParameter[] parameters)
    {
        using (OracleConnection connection = new OracleConnection(connectionString))
        {
            try
            {
                connection.Open();
                OracleCommand command = new OracleCommand(storedProcName, connection);
                command.CommandType = CommandType.StoredProcedure;
                foreach (OracleParameter parameter in parameters)
                {
                    if (parameter != null)
                    {
                        // 检查未分配值的输出参数,将其分配以DBNull.Value.
                        if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input)
                            (parameter.Value == null))
                        {
                            parameter.Value = DBNull.Value;
                        }
                        command.Parameters.Add(parameter);
                    }
                }
               int rows = command.ExecuteNonQuery();
               return rows;
            }

            finally
            {
                connection.Close();
            }
        }
    }

    /// summary>
    /// 构建 OracleCommand 对象(用来返回一个结果集,而不是一个整数值)
    /// /summary>
    /// param name="connection">数据库连接/param>
    /// param name="storedProcName">存储过程名/param>
    /// param name="parameters">存储过程参数/param>
    /// returns>OracleCommand/returns>
    private static OracleCommand BuildQueryCommand(OracleConnection connection, string storedProcName, IDataParameter[] parameters)
    {
        OracleCommand command = new OracleCommand(storedProcName, connection);
        command.CommandType = CommandType.StoredProcedure;
        foreach (OracleParameter parameter in parameters)
        {
            if (parameter != null)
            {
                // 检查未分配值的输出参数,将其分配以DBNull.Value.
                if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input)
                    (parameter.Value == null))
                {
                    parameter.Value = DBNull.Value;
                }
                command.Parameters.Add(parameter);
            }
        }
        return command;
    }


    #endregion

    #region 事务处理

    /// summary>
    /// 执行多条SQL语句(list的形式),实现数据库事务。
    /// /summary>
    /// param name="SQLStringList">多条SQL语句/param>   
    /// 调用 Transaction 对象的 Commit 方法来完成事务,或调用 Rollback 方法来取消事务。
    public static int ExecuteSqlTran(ListString> SQLStringList)
    {
        using (OracleConnection connection = new OracleConnection(connectionString))
        {
            connection.Open();
            // 为事务创建一个命令
            OracleCommand cmd = new OracleCommand();
            cmd.Connection = connection;
            OracleTransaction tx = connection.BeginTransaction();// 启动一个事务
            cmd.Transaction = tx;
            try
            {
                int count = 0;
                for (int n = 0; n SQLStringList.Count; n++)
                {
                    string strsql = SQLStringList[n];
                    if (strsql.Trim().Length > 1)
                    {
                        cmd.CommandText = strsql;
                        count += cmd.ExecuteNonQuery();
                    }
                }
                tx.Commit();//用Commit方法来完成事务
                return count;//
            }
            catch
            {
                tx.Rollback();//出现错误,事务回滚!
                return 0;
            }
            finally
            {
                cmd.Dispose();
                connection.Close();//关闭连接
            }
        }
    }
    #endregion
    #region 事务处理

    /// summary>
    /// 执行多条SQL语句(字符串数组形式),实现数据库事务。
    /// /summary>
    /// param name="SQLStringList">多条SQL语句/param>   
    /// 调用 Transaction 对象的 Commit 方法来完成事务,或调用 Rollback 方法来取消事务。
    public static int ExecuteTransaction(string[] SQLStringList,int p)
    {
        using (OracleConnection connection = new OracleConnection(connectionString))
        {
            connection.Open();
            // 为事务创建一个命令
            OracleCommand cmd = new OracleCommand();
            cmd.Connection = connection;
            OracleTransaction tx = connection.BeginTransaction();// 启动一个事务
            cmd.Transaction = tx;
            try
            {
                int count = 0;
                for (int n = 0; n p; n++)
                {
                    string strsql = SQLStringList[n];
                    if (strsql.Trim().Length > 1)
                    {
                        cmd.CommandText = strsql;
                        count += cmd.ExecuteNonQuery();
                    }
                }
                tx.Commit();//用Commit方法来完成事务
                return count;//
            }
            catch
            {
                tx.Rollback();//出现错误,事务回滚!
                return 0;
            }
            finally
            {
                cmd.Dispose();
                connection.Close();//关闭连接
            }
        }
    }

    #endregion
    /// summary>
    /// 执行存储过程获取所需编号(各表主键)
    /// /summary>
    /// param name="FlowName">存储过程参数/param>
    /// param name="StepLen">存储过程参数(默认为1)/param>
    /// returns>编号(各表主键)/returns>
    public static string Get_FlowNum(string FlowName, int StepLen = 1)
    {
        OracleConnection mycon = new OracleConnection(connectionString);
        try
        {
            mycon.Open();
            OracleCommand MyCommand = new OracleCommand("ALARM_GET_FLOWNUMBER", mycon);
            MyCommand.CommandType = CommandType.StoredProcedure;
            MyCommand.Parameters.Add(new OracleParameter("I_FlowName", OracleType.VarChar, 50));
            MyCommand.Parameters["I_FlowName"].Value = FlowName;
            MyCommand.Parameters.Add(new OracleParameter("I_SeriesNum", OracleType.Number));
            MyCommand.Parameters["I_SeriesNum"].Value = StepLen;
            MyCommand.Parameters.Add(new OracleParameter("O_FlowValue", OracleType.Number));
            MyCommand.Parameters["O_FlowValue"].Direction = ParameterDirection.Output;
            MyCommand.ExecuteNonQuery();
            return MyCommand.Parameters["O_FlowValue"].Value.ToString();
        }
        catch
        {
            return "";
        }
        finally
        {
            mycon.Close();
        }
    }

}

您可能感兴趣的文章:
  • 一个ASP.NET的MYSQL的数据库操作类自己封装的
  • asp.net下使用DbProviderFactories的数据库操作类
  • ASP.NET对SQLServer的通用数据库访问类
  • asp.net下Oracle,SQL Server,Access万能数据库通用类
  • asp.net 数据库连接类代码(SQL)
  • asp.net 数据库的连接和datatable类
  • ASP.NET封装的SQL数据库访问类
  • ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法
  • asp.net连接查询SQL数据库并把结果显示在网页上(2种方法)
  • ASP.NET 6种常用数据库的连接方法
  • ASP.NET2.0 SQL Server数据库连接详解
  • Asp.net把图片存入数据库和读取图片的方法
  • ASP.NET数据库操作类实例

标签:洛阳 周口 朝阳 新乡 朔州 喀什 台州 百色

巨人网络通讯声明:本文标题《asp.net Oracle数据库访问操作类》,本文关键词  asp.net,Oracle,数据库,访问,;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 下面列出与本文章《asp.net Oracle数据库访问操作类》相关的同类信息!
  • 本页收集关于asp.net Oracle数据库访问操作类的相关信息资讯供网民参考!
  • 推荐文章