主頁 > 知識庫 > asp.net Oracle數(shù)據(jù)庫訪問操作類

asp.net Oracle數(shù)據(jù)庫訪問操作類

熱門標(biāo)簽:怎樣在地圖標(biāo)注消火栓圖形 濟源人工智能電話機器人價格 泰州手機外呼系統(tǒng)軟件 內(nèi)蒙古智能電銷機器人哪家強 百度地圖標(biāo)注點擊事件 地圖標(biāo)注位置多的錢 山東防封電銷卡辦理套餐 廈門四川外呼系統(tǒng) 杭州智能電話機器人

復(fù)制代碼 代碼如下:

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>
    /// 數(shù)據(jù)訪問抽象基礎(chǔ)類
    ///
    /// /summary>
public class DBBase
{


    //數(shù)據(jù)庫連接字符串(web.config來配置),可以動態(tài)更改connectionString支持多數(shù)據(jù)庫.       
    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(); //返回結(jié)果的第一行一列
                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  執(zhí)行簡單SQL語句 返回影響的記錄數(shù)

    /// summary>
    /// 執(zhí)行SQL語句,返回影響的記錄數(shù)
    /// /summary>
    /// param name="SQLString">SQL語句/param>
    /// returns>影響的記錄數(shù)/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   執(zhí)行查詢語句,返回SqlDataReader
    /// summary>
    /// 執(zhí)行查詢語句,返回SqlDataReader ( 注意:調(diào)用該方法后,一定要對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  執(zhí)行SQL查詢語句,返回DataTable數(shù)據(jù)表
    /// summary>
    /// 執(zhí)行SQL查詢語句
    /// /summary>
    /// param name="sqlStr">/param>
    /// returns>返回DataTable數(shù)據(jù)表/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">參數(shù)/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>
    /// 執(zhí)行存儲過程
    /// /summary>
    /// param name="storedProcName">存儲過程名稱/param>
    /// param name="parameters">參數(shù)/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)
                    {
                        // 檢查未分配值的輸出參數(shù),將其分配以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>
    /// 構(gòu)建 OracleCommand 對象(用來返回一個結(jié)果集,而不是一個整數(shù)值)
    /// /summary>
    /// param name="connection">數(shù)據(jù)庫連接/param>
    /// param name="storedProcName">存儲過程名/param>
    /// param name="parameters">存儲過程參數(shù)/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)
            {
                // 檢查未分配值的輸出參數(shù),將其分配以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 事務(wù)處理

    /// summary>
    /// 執(zhí)行多條SQL語句(list的形式),實現(xiàn)數(shù)據(jù)庫事務(wù)。
    /// /summary>
    /// param name="SQLStringList">多條SQL語句/param>   
    /// 調(diào)用 Transaction 對象的 Commit 方法來完成事務(wù),或調(diào)用 Rollback 方法來取消事務(wù)。
    public static int ExecuteSqlTran(ListString> SQLStringList)
    {
        using (OracleConnection connection = new OracleConnection(connectionString))
        {
            connection.Open();
            // 為事務(wù)創(chuàng)建一個命令
            OracleCommand cmd = new OracleCommand();
            cmd.Connection = connection;
            OracleTransaction tx = connection.BeginTransaction();// 啟動一個事務(wù)
            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方法來完成事務(wù)
                return count;//
            }
            catch
            {
                tx.Rollback();//出現(xiàn)錯誤,事務(wù)回滾!
                return 0;
            }
            finally
            {
                cmd.Dispose();
                connection.Close();//關(guān)閉連接
            }
        }
    }
    #endregion
    #region 事務(wù)處理

    /// summary>
    /// 執(zhí)行多條SQL語句(字符串?dāng)?shù)組形式),實現(xiàn)數(shù)據(jù)庫事務(wù)。
    /// /summary>
    /// param name="SQLStringList">多條SQL語句/param>   
    /// 調(diào)用 Transaction 對象的 Commit 方法來完成事務(wù),或調(diào)用 Rollback 方法來取消事務(wù)。
    public static int ExecuteTransaction(string[] SQLStringList,int p)
    {
        using (OracleConnection connection = new OracleConnection(connectionString))
        {
            connection.Open();
            // 為事務(wù)創(chuàng)建一個命令
            OracleCommand cmd = new OracleCommand();
            cmd.Connection = connection;
            OracleTransaction tx = connection.BeginTransaction();// 啟動一個事務(wù)
            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方法來完成事務(wù)
                return count;//
            }
            catch
            {
                tx.Rollback();//出現(xiàn)錯誤,事務(wù)回滾!
                return 0;
            }
            finally
            {
                cmd.Dispose();
                connection.Close();//關(guān)閉連接
            }
        }
    }

    #endregion
    /// summary>
    /// 執(zhí)行存儲過程獲取所需編號(各表主鍵)
    /// /summary>
    /// param name="FlowName">存儲過程參數(shù)/param>
    /// param name="StepLen">存儲過程參數(shù)(默認為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的數(shù)據(jù)庫操作類自己封裝的
  • asp.net下使用DbProviderFactories的數(shù)據(jù)庫操作類
  • ASP.NET對SQLServer的通用數(shù)據(jù)庫訪問類
  • asp.net下Oracle,SQL Server,Access萬能數(shù)據(jù)庫通用類
  • asp.net 數(shù)據(jù)庫連接類代碼(SQL)
  • asp.net 數(shù)據(jù)庫的連接和datatable類
  • ASP.NET封裝的SQL數(shù)據(jù)庫訪問類
  • ASP.NET web.config中數(shù)據(jù)庫連接字符串connectionStrings節(jié)的配置方法
  • asp.net連接查詢SQL數(shù)據(jù)庫并把結(jié)果顯示在網(wǎng)頁上(2種方法)
  • ASP.NET 6種常用數(shù)據(jù)庫的連接方法
  • ASP.NET2.0 SQL Server數(shù)據(jù)庫連接詳解
  • Asp.net把圖片存入數(shù)據(jù)庫和讀取圖片的方法
  • ASP.NET數(shù)據(jù)庫操作類實例

標(biāo)簽:洛陽 周口 朝陽 新鄉(xiāng) 朔州 喀什 臺州 百色

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《asp.net Oracle數(shù)據(jù)庫訪問操作類》,本文關(guān)鍵詞  asp.net,Oracle,數(shù)據(jù)庫,訪問,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《asp.net Oracle數(shù)據(jù)庫訪問操作類》相關(guān)的同類信息!
  • 本頁收集關(guān)于asp.net Oracle數(shù)據(jù)庫訪問操作類的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章