1. 程式人生 > >基於Dapper寫的一個sqlhelp適用於多版本數據庫

基於Dapper寫的一個sqlhelp適用於多版本數據庫

insert mar char new delet 新車 pwd dbconnect xxx



ConnectionInit方法用於初始化數據庫連接對象,

只需要修改databasetype參數即可進行適用各個版本的數據庫,

ExecuteNonQuery方法用於執行增、刪、改操作,返回受影響的行數,
ExecuteTableSQL方法用於執行查詢操作,返回datatable格式的數據,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Dapper;

namespace DataManager
{
    public
static class DaHelper { private static IDbConnection ConnectionObject = null;//連接數據類的對象 private static string ConnectionString = "";//連接的字符串 private static int DataBaseType = 0;//數據庫的類型0=sqlserver,1=access,2=oracle,3=mysql /// <summary> /// 設置連接的字符串及數據庫類型
/// </summary> /// <param name="str">連接的字符串</param> /// <param name="_type">數據庫類型0=sqlserver,1=access,2=oracle,3=mysql</param> public static void ConnectionInit(string str, int _type) { ConnectionString = str; DataBaseType = _type; SetConnection(); }
/// <summary> /// 設置連接類的對象 /// </summary> private static void SetConnection() { switch (DataBaseType) { case 0: ConnectionObject = new System.Data.SqlClient.SqlConnection(ConnectionString);//連接sqlserver break; case 1: ConnectionObject = new System.Data.OleDb.OleDbConnection(ConnectionString);//連接access break; case 2: ConnectionObject = new System.Data.OracleClient.OracleConnection(ConnectionString);//連接oracle //處理辦法: //在oracle 安裝目錄下 找到 Oracle.DataAccess.dll添加引用,然後 using Oracle.DataAccess.Client; //其他的都不用動,即可。 //連接字符串中 如有 用的是 user=xxx 就改成user id=xxx //把原來 Using 的System.Data.OracleClient去掉即可 break; case 3: ConnectionObject = new MySql.Data.MySqlClient.MySqlConnection(ConnectionString);//連接mysql break; } } /// <summary> /// 打開數據庫連接 /// </summary> private static void OpenConnection() { if (ConnectionObject.State == System.Data.ConnectionState.Closed) { ConnectionObject.Open(); } } /// <summary> /// 關閉數據庫連接 /// </summary> private static void CloseConnection() { if (ConnectionObject.State == System.Data.ConnectionState.Open) { ConnectionObject.Close(); } } /// <summary> /// 執行sql並且返回受影響的行數 /// </summary> /// <param name="sql"></param> /// <param name="type"></param> /// <param name="para"></param> /// <returns></returns> public static int ExecuteNonQuery(string sql, object _object) { lock (ConnectionObject) { try { OpenConnection(); return ConnectionObject.Execute(sql, _object); } catch { throw; } finally { CloseConnection(); } } } /// <summary> /// 執行查詢的sql語句,並且返回datatable結果 /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <returns></returns> public static DataTable ExecuteTableSQL(string sql, object _object) { lock (ConnectionObject) { try { OpenConnection(); IDataReader idr = ConnectionObject.ExecuteReader(sql, _object); return IDataReaderToDataTable(idr); } catch { throw; } finally { CloseConnection(); } } } /// <summary> /// 把idatareader轉換成datatable /// </summary> /// <param name="reader"></param> /// <returns></returns> private static DataTable IDataReaderToDataTable(IDataReader reader) { DataTable objDataTable = new DataTable(); int intFieldCount = reader.FieldCount; for (int intCounter = 0; intCounter < intFieldCount; ++intCounter) { objDataTable.Columns.Add(reader.GetName(intCounter), typeof(string)); } objDataTable.BeginLoadData(); object[] objValues = new object[intFieldCount]; while (reader.Read()) { reader.GetValues(objValues); objDataTable.LoadDataRow(objValues, true); } reader.Close(); objDataTable.EndLoadData(); return objDataTable; }

初始化例子:

DataManager.DaHelper.ConnectionInit("server=127.0.0.1;uid=root;pwd=;database=TouchDisplay;pooling=false;CharSet=utf8;port=3306;", 3);//連接mysql數據庫

增、刪、改例子:

        /// <summary>
        /// 新增排隊記錄
        /// </summary>
        /// <param name="CarNo">車牌</param>
        /// <param name="QueueNo">排隊號碼</param>
        /// <param name="QueueIndex">排隊序號</param>
        /// <param name="IcCard">Ic卡號</param>
        /// <param name="CargoNo">油品</param>
        /// <param name="ComeTime">登記時間</param>
        /// <param name="StateNo">狀態</param>
        /// <returns></returns>
        public static int AddQueue(string CarNo, string QueueNo, int QueueIndex,string IcCard, string CargoName,DateTime ComeTime, int StateNo)
        {
            string sql = "Insert Into Table_Queue (CarNo,QueueNo,QueueIndex,IcCard,CargoName,ComeTime,StateNo) Values(@CarNo,@QueueNo,@QueueIndex,@IcCard,@CargoName,@ComeTime,@StateNo);";
            object para = new
            {
                CarNo = CarNo,
                QueueNo = QueueNo,
                QueueIndex = QueueIndex,
                IcCard=IcCard,
                CargoName = CargoName,
                ComeTime=ComeTime,
                StateNo = StateNo,
            };
            return DaHelper.ExecuteNonQuery(sql, para);
        }


        /// <summary>
        /// 根據車牌號碼刪除隊列中的車輛
        /// </summary>
        /// <param name="CarNo"></param>
        /// <returns></returns>
        public static int RemoveCarDataByCarNo(string CarNo)
        {
            string sql = "delete from Table_Queue where [email protected] and  StateNo = 0";
            object para = new
            {
                CarNo = CarNo,
            };
            return DaHelper.ExecuteNonQuery(sql, para);
        }

        /// <summary>
        /// 根據車牌號更新車輛信息
        /// </summary>
        /// <param name="CarNo"></param>
        /// <param name="QueueIndex"></param>
        /// <returns></returns>
        public static int UpdateQueueByCarNo(string CarNo, string IcCard, string CargoName)
        {
            string sql = "update Table_Queue set [email protected] ,[email protected] where [email protected] and  StateNo = 0 or [email protected] and  StateNo = 1";
            object para = new
            {
                CarNo = CarNo,
                IcCard = IcCard,
                CargoName = CargoName,
            };
            return DaHelper.ExecuteNonQuery(sql, para);
        }

查詢例子:

        /// <summary>
        /// 獲取排隊列表
        /// </summary>
        /// <returns></returns>
        public static DataTable GetAllQueueing()
        {
            string sql = "select QueueNo ,CarNo ,CargoName  from Table_Queue where StateNo =0 order by QueueIndex";

            DataTable dt = DaHelper.ExecuteTableSQL(sql, null);
            return dt;
        }

the end

基於Dapper寫的一個sqlhelp適用於多版本數據庫