91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

asp.net 中怎么實現一個Oracle數據庫訪問操作類

發布時間:2021-07-16 13:45:14 來源:億速云 閱讀:145 作者:Leah 欄目:開發技術

這篇文章將為大家詳細講解有關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(List<String> 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 中怎么實現一個Oracle數據庫訪問操作類就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

普安县| 莱阳市| 邵东县| 大英县| 新蔡县| 禹城市| 马鞍山市| 睢宁县| 黄平县| 孝感市| 永和县| 贵定县| 武安市| 开封县| 金沙县| 琼结县| 吐鲁番市| 辽阳市| 龙南县| 龙州县| 剑河县| 水城县| 什邡市| 普定县| 淄博市| 福清市| 莫力| 嘉峪关市| 和田市| 长沙县| 溧水县| 白水县| 安岳县| 奉化市| 犍为县| 平湖市| 惠来县| 原平市| 团风县| 兴仁县| 东乌珠穆沁旗|