個人資料庫幫助類

来源:http://www.cnblogs.com/gfjin/archive/2017/12/21/8079465.html
-Advertisement-
Play Games

個人資料庫幫助類:提供兩種訪問方式OleDb(需安裝Oracle客戶端)和 Oracle.ManagedDataAccess.Client(需Oracle.ManagedDataAccess.dll) 多說無益上代碼: #region Info/* ************************* ...


個人資料庫幫助類:提供兩種訪問方式OleDb(需安裝Oracle客戶端)和 Oracle.ManagedDataAccess.Client(需Oracle.ManagedDataAccess.dll)

多說無益上代碼:

#region Info
/*
 ******************************【此類描述】*******************************
 *         
 *作者:高發金                                                                  
 *                                                                      
 *創建時間:2017/11/7 10:20:35                                                       
 *                                                                      
 *文件名稱:DbHelper                                                
 *                                                                      
 *版本信息:V1.0.0                                                       
 *         
 *修改者: 高發金                 修改時間:2017/11/7 10:20:35                        
 *                                                                      
 *當前的CLR版本:4.0.30319.42000                                            
 *                                                                      
 *修改說明:                      
 *         
 *                          
 *         
 *************************************************************************
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Oracle.ManagedDataAccess.Client;
using Oracle.ManagedDataAccess.Types;
using System.Data;
using System.Data.OleDb;
using System.Data.Common;
using Gaofajin.Attribute;

namespace Gaofajin.Data
{
  
    public class DbHelper
    {
        public bool IsOledb { get; set; } = false;
      
        DbConnection connection;
        DbCommand cmd;
        DbCommandBuilder cmb;
        DbDataAdapter adp;
        DbTransaction transaction;
        public String DBVersion
        {
            get
            {
                if (connection != null)
                    return connection.ServerVersion;
                return "未知";
            }
        }
        public DbConnection GetConnection() => connection;
        string Try_catch(Action a)
        {
            try
            {
                a();
                return "操作成功!";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public void SetConnectionString(string constr)
        {
            if (IsOledb)
                connection = new OleDbConnection();
            else connection = new OracleConnection();
                connection.ConnectionString = constr;
        }
        public string Open()=> Try_catch(connection.Open);
        public string Close()=>Try_catch(connection.Close);
        public DbResult GetData(string sql)
        {
            try
            {
                if (IsOledb)
                {
                    cmd = new OleDbCommand(sql, (OleDbConnection)connection);
                    adp = new OleDbDataAdapter((OleDbCommand)cmd);
                }
                else
                {
                    cmd = new OracleCommand(sql, (OracleConnection)connection);
                    adp = new OracleDataAdapter((OracleCommand)cmd);
                }
                DataTable dt = new DataTable();
                int m = adp.Fill(dt);
                return new DbResult(m, "執行查詢成功!", dt);
            }
            catch (Exception ex)
            {
                return new DbResult(-1, ex.Message, null);
            }
        }
        public DbResult GetDataforUpdate(string sql)
        {
            try
            {
                if (IsOledb)
                {
                    cmd = new OleDbCommand(sql, (OleDbConnection)connection);
                    adp = new OleDbDataAdapter((OleDbCommand)cmd);
                    cmb = new OleDbCommandBuilder((OleDbDataAdapter)adp);
                }
                else
                {
                    cmd = new OracleCommand(sql, (OracleConnection)connection);
                    adp = new OracleDataAdapter((OracleCommand)cmd);
                    cmb = new OracleCommandBuilder((OracleDataAdapter)adp);
                }
                DataTable dt = new DataTable();
                int m = adp.Fill(dt);
                return new DbResult(m, "執行查詢成功!", dt);
            }
            catch (Exception ex)
            {
                return new DbResult(-1, ex.Message, null);
            }
        }
        public void UpdateFrom(DataTable dt)
        {
            if (adp != null)
                adp.Update(dt);
        }
        public DbResult ExeSql(string sql)
        {
            try
            {
                if (IsOledb)
                {
                    cmd = new OleDbCommand(sql, (OleDbConnection)connection);
                }
                else
                {
                    cmd = new OracleCommand(sql, (OracleConnection)connection);
                }
                int m = cmd.ExecuteNonQuery();
                return new DbResult(m, "執行SQL成功!", null);
            }
            catch (Exception ex)
            {
                return new DbResult(-1, ex.Message, null);
            }
        }
        public DbResult Proc(string ProcName, IDataParameter[] paras)
        {
            try
            {
                if (IsOledb)
                {
                    cmd = new OleDbCommand
                    {
                        Connection = (OleDbConnection)connection,
                        CommandText = ProcName,
                        CommandType = CommandType.StoredProcedure
                    };
                }
                else
                {
                    cmd = new OracleCommand
                    {
                        Connection = (OracleConnection)connection,
                        CommandText = ProcName,
                        CommandType = CommandType.StoredProcedure
                    };
                }
                foreach (var para in paras)
                {
                    cmd.Parameters.Add(para);
                }
                int m = cmd.ExecuteNonQuery();
                return new DbResult(m, "執行存儲過程成功!", null);
            }
            catch (Exception ex)
            {
                return new DbResult(-1, ex.Message, null);
            }
        }
        public DbResult Procselect(string ProcName, IDataParameter[] paras)
        {
            try
            {
                if (IsOledb)
                {
                    adp = new OleDbDataAdapter
                    {
                        SelectCommand = new OleDbCommand
                        {
                            Connection = (OleDbConnection)connection,
                            CommandText = ProcName,
                            CommandType = CommandType.StoredProcedure
                        }
                    };
                }
                else
                {
                    adp = new OracleDataAdapter
                    {
                        SelectCommand = new OracleCommand
                        {
                            Connection = (OracleConnection)connection,
                            CommandText = ProcName,
                            CommandType = CommandType.StoredProcedure
                        }
                    };
                }
                adp.SelectCommand.Parameters.AddRange(paras);
                DataTable dt = new DataTable();
                int m = adp.Fill(dt);
                return new DbResult(m, "執行存儲過程成功!", dt);
            }
            catch (Exception ex)
            {
                return new DbResult(-1, ex.Message, null);
            }
        }
        public void BeginTrans()
        {
            transaction = connection.BeginTransaction();
        }
        public void CommitTrans()
        {
            if (transaction != null)
                transaction.Commit();
        }
        public void RollBack()
        {
            if (transaction != null)
                transaction.Rollback();
        }
        public List<string> GetAllTableName(string schemaName, String CollName, string User)
        {
            DataTable dt = connection.GetSchema(schemaName);
            List<string> Tablesname = new List<string>();
            if (dt != null)
            {
                DataRow[] drs = dt.Select(string.Format("TABLE_SCHEMA='{0}'and TABLE_TYPE LIKE'%{1}%'", User, CollName));
                foreach (var dr in drs)
                {
                    Tablesname.Add(dr["TABLE_NAME"].ToString());
                }
            }
            return Tablesname;
        }
        string GetConnecttionString(string host, string port, string service_name, string user, string pwd)
        {
            if (string.IsNullOrEmpty(service_name))
            {
                System.Windows.Forms.MessageBox.Show("Service_Name不能為空或者null", "錯誤", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                return null;
            }
            if (string.IsNullOrEmpty(user))
            {
                System.Windows.Forms.MessageBox.Show("userName不能為空或者null", "錯誤", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                return null;
            }
            if (string.IsNullOrEmpty(pwd))
            {
                System.Windows.Forms.MessageBox.Show("password不能為空或者null", "錯誤", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                return null;
            }
            string str = "DATA SOURCE=\"(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = {0})(PORT = {1})))(CONNECT_DATA = (SERVICE_NAME = {2})))\";USER ID={3};PASSWORD={4}";
            return string.Format(str, (string.IsNullOrEmpty(host) ? "localhost" : host), (string.IsNullOrEmpty(port)? 1521 :int.Parse(port)), service_name, user, pwd);
        }
        string GetConnecttionString(string Name, string user, string pwd)
        {
            if (string.IsNullOrEmpty(Name))
            {
                System.Windows.Forms.MessageBox.Show("Name不能為空或者null", "錯誤", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                return null;
            }
            if (string.IsNullOrEmpty(user))
            {
                System.Windows.Forms.MessageBox.Show("user不能為空或者null", "錯誤", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                return null;
            }
            if (string.IsNullOrEmpty(pwd))
            {
                System.Windows.Forms.MessageBox.Show("password不能為空或者null", "錯誤", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                return null;
            }
            string str = "Provider = MSDAORA.1; User ID = {0}; password ={1}; Data Source = {2}";
            return string.Format(str, user, pwd, Name);
        }
        public string GetConnecttionString(params string[] str) => (IsOledb == true) ? GetConnecttionString(str[0], str[1], str[2]) : GetConnecttionString(str[0], str[1], str[2], str[3], str[4]);
    }
    public class DbResult
    {
        public int ErrCode { get; }
        public string ErrMsg { get; }
        public DataTable Data { get; }
        public DbResult(int ecode, string emsg, DataTable dt)
        {
            ErrCode = ecode;
            ErrMsg = emsg;
            Data = dt;
        }
    }
}

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 語句是過程式編程語言的基礎構造,對應於程式命令,通常按照指定順序執行。S#語句與C#語句基本相同,如有var, eval, if, switch, for, foreach, do, while, return, continue, break, load, using等語句。C/C++/Java/... ...
  • 一Socket介紹: 網路上的兩個程式通過一個雙向的通信連接實現數據的交換,這個連接的一端稱為一個socket。 建立網路通信連接至少要一對埠號(socket)。socket本質是編程介面(API),對TCP/IP的封裝,TCP/IP也要提供可供程式員做網路開發所用的介面,這就是Socket編程接 ...
  • using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Remoting.Messaging; using System.Text; using System.Threading;... ...
  • 三層架構,我們一般說的三層架構通常指的是:1、表現層(UI):就是展現給用戶的界面,無論是網站前臺還是應用程式界面;2、業務邏輯層(BLL):針對具體問題的邏輯操作;3、數據訪問層(DAL):對數據進行操作。其他的層級基本都是在這三層之上的補充。 UI(User Interface)表示層: 就是我 ...
  • 本示例學習如何閱讀有多個await方法方法時,程式的實際流程是怎麼樣的,理解await的非同步調用 。 ...
  • I was reading a post about some common C# interview questions, and thought I’d share some of mine. These are questions that I asked in interviews, or ... ...
  • 在Excel中如果能夠將具有多級明細的數據進行分組顯示,可以清晰地展示數據表格的整體結構,使整個文檔具有一定層次感。根據需要設置顯示或者隱藏分類數據下的詳細信息,在便於數據查看、管理的同時也使文檔更具美觀性。那麼,在C#中如何來創建Excel數據的多級分組顯示呢?下麵將進行詳細闡述。方法中使用了免費 ...
  • Highcharts .NET allows developers to make charts using Highcharts API with the Microsoft .NET Framework (ASP.NET MVC 4+). ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...