一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

SQLLite的使用

 昵稱20855414 2014-12-08
namespace SQLLiteHelper
{
    /// <summary>
    /// 數(shù)據(jù)庫操作抽象
    /// </summary>
    public interface IDataAccess
    {
        /// <summary>
        /// 打開
        /// </summary>
        void Open();
        /// <summary>
        /// 關(guān)閉
        /// </summary>
        void Close();

        /// <summary>
        /// 執(zhí)行 增 刪 改 查
        /// </summary>
        /// <param name="sql">Sql語句</param>
        /// <param name="queryParameter">參數(shù)</param>
        int ExecuteNonQuery(string sql, QueryParameter[] queryParameter);

        /// <summary>
        /// 查詢某一個(gè)值
        /// </summary>
        /// <param name="sql">Sql語句</param>
        /// <param name="parameters">參數(shù)</param>
        /// <returns>Object</returns>
        object GetScalar(string sql, QueryParameter[] parameters);

        /// <summary>
        /// 查詢多條條數(shù)據(jù)
        /// </summary>
        /// <param name="sql">Sql語句</param>
        /// <param name="dt">DataTabel </param>
        /// <param name="parameters">參數(shù)</param>
        /// <returns>DataTable</returns>
        DataTable GetTable(string sql, DataTable dt, QueryParameter[] parameters);

        /// <summary>
        /// 查詢多條值
        /// </summary>
        /// <param name="sql">Sql語句</param>
        /// <param name="parameters">參數(shù)</param>
        /// <returns>DataReader</returns>
        IDataReader GetReader(string sql, QueryParameter[] parameters);

        /// <summary>
        /// 開始事務(wù)
        /// </summary>
        void BeginTran();
        /// <summary>
        /// 提交事務(wù)
        /// </summary>
        void CommitTran();
        /// <summary>
        ///  回滾事務(wù)
        /// </summary>
        void RollBackTran();

    }
}

復(fù)制代碼



復(fù)制代碼

 public  class DataAccessFactory
    {
      
        public static IDataAccess CreateDataAccess()
        {
            string conStr = System.Configuration.ConfigurationManager.AppSettings["conStr"];
            string conType = System.Configuration.ConfigurationManager.AppSettings["conType"]; 
            switch (conType)
            {
                case "SQLLite":return new SqlLiteDalHelper(conStr);
                    break;
                case "SQLServer":
                    return new SqlLiteDalHelper(conStr);
                    break;

                default:throw new Exception("該數(shù)據(jù)庫不支持!");
            }
        }

    }

復(fù)制代碼



復(fù)制代碼

 1     public class QueryParameter
 2     {
 3 
 4         public string Name
 5         {
 6             get;
 7             set;
 8         }
 9         public object Value
10         {
11             get;
12             set;
13         }
14         public DbType DbType
15         {
16             get;
17             set;
18         }
19 
20         private ParameterDirection _driection = ParameterDirection.Input;
21 
22         public ParameterDirection Driection
23         {
24             get { return _driection; }
25             set { _driection = value; }
26         }
27         public QueryParameter(string name, object value, DbType dbType)
28         {
29             this.DbType = dbType;
30             this.Value = value;
31             this.Name = name;
32         }
33     }

復(fù)制代碼



復(fù)制代碼

  1  public class SqlLiteDalHelper : IDataAccess
  2     {
  3         private readonly SQLiteConnection _connection;
  4         private SQLiteTransaction _tran;
  5         public SqlLiteDalHelper(string constr)
  6         {
  7             _connection = new SQLiteConnection(constr);
  8         }
  9 
 10         public void Open()
 11         {
 12             if (this._connection == null || this._connection.State == ConnectionState.Closed)
 13             {
 14                 try
 15                 {
 16                     _connection.Open();
 17                 }
 18                 catch (ArgumentException)
 19                 {
 20                     throw;
 21                 }
 22               }
 23         }
 24 
 25         public void Close()
 26         {
 27             if (this._connection != null || _tran != null)
 28             {
 29                 _connection.Close();
 30             }
 31         }
 32 
 33         public int ExecuteNonQuery(string sql, QueryParameter[] queryParameter)
 34         {
 35             SQLiteCommand cmd = new SQLiteCommand();
 36             PrepareCommond(cmd, CommandType.Text, queryParameter, sql);
 37             int i = cmd.ExecuteNonQuery();
 38             cmd.Parameters.Clear();
 39             return i;
 40         }
 41 
 42         public object GetScalar(string sql, QueryParameter[] parameters)
 43         {
 44             SQLiteCommand cmd = new SQLiteCommand();
 45             PrepareCommond(cmd, CommandType.Text, parameters, sql);
 46             object obj = cmd.ExecuteScalar();
 47             cmd.Parameters.Clear();
 48             return obj;
 49         }
 50 
 51         public System.Data.DataTable GetTable(string sql, DataTable dt, QueryParameter[] parameters)
 52         {
 53             SQLiteCommand cmd = new SQLiteCommand();
 54             PrepareCommond(cmd, CommandType.Text, parameters, sql);
 55             SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
 56             sda.Fill(dt);
 57             cmd.Parameters.Clear();
 58             return dt;
 59         }
 60 
 61         public System.Data.IDataReader GetReader(string sql, QueryParameter[] parameters)
 62         {
 63             SQLiteCommand cmd = new SQLiteCommand();
 64             PrepareCommond(cmd, CommandType.Text, parameters, sql);
 65             SQLiteDataReader dr = cmd.ExecuteReader();
 66             cmd.Parameters.Clear();
 67             return dr;
 68         }
 69 
 70         public void BeginTran()
 71         {
 72             this._tran = this._connection.BeginTransaction();
 73         }
 74 
 75         public void CommitTran()
 76         {
 77             this._tran.Commit();
 78         }
 79 
 80         public void RollBackTran()
 81         {
 82             this._tran.Rollback();
 83         }
 84 
 85         private void PrepareCommond(SQLiteCommand cmd, CommandType commandType, QueryParameter[] parameters, string commandtext)
 86         {
 87             cmd.CommandType = commandType;
 88             cmd.CommandText = commandtext;
 89             cmd.Connection = _connection;
 90             cmd.Transaction = this._tran;
 91             if (parameters != null && parameters.Length > 0)
 92             {
 93                 for (int i = 0; i < parameters.Length; i++)
 94                 {
 95                     cmd.Parameters.AddWithValue(parameters[i].Name, parameters[i].Value);
 96                 }
 97             }
 98         }
 99 
100 
101     }

復(fù)制代碼


 


使用方法:



復(fù)制代碼

    public DataTable   FindAllTRoleOperator()
    {
        try
        {
            string sql;
            sql="SELECT COMMENTARY,OPERATORID,ROLEID from T_ROLE_OPERATOR";
            DataTable dt = new DataTable("MINORITY");
            data.GetTable(sql, dt, null);
            return dt;
        }
        catch(Exception e)
        {
            throw e;
        }
    }

復(fù)制代碼



復(fù)制代碼

public DataTable   FindAllTRoleOperator()
    {
        IDataAccess dataAccess  =null;
            try
            {
                dataAccess  =  DataAccessFactory.CreateDataAccess();
                dataAccess.Open();
                RoleOperatorService dal  =  new RoleOperatorService(dataAccess);
                return dal.FindAllTRoleOperator();
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (dataAccess != null) dataAccess.Close();
            }
       }

復(fù)制代碼


運(yùn)行結(jié)果:



    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    日本人妻中出在线观看| 亚洲熟妇av一区二区三区色堂| 午夜国产精品国自产拍av| 高跟丝袜av在线一区二区三区| 婷婷开心五月亚洲综合| 香蕉久久夜色精品国产尤物| 欧美日韩成人在线一区| 久久国产精品亚州精品毛片| 狠狠干狠狠操亚洲综合| 国产精品大秀视频日韩精品| 91日韩欧美在线视频| 国产又粗又猛又大爽又黄| 欧美国产日产在线观看| 亚洲高清一区二区高清| 亚洲综合日韩精品欧美综合区| 欧美乱码精品一区二区三| 美女露小粉嫩91精品久久久| 台湾综合熟女一区二区| 亚洲av日韩一区二区三区四区| 久一视频这里只有精品| av在线免费观看一区二区三区| 丝袜视频日本成人午夜视频| 亚洲女同一区二区另类| 国产不卡在线免费观看视频| 免费观看在线午夜视频| 九九热精彩视频在线播放| 天堂av一区一区一区| 日本一区二区三区久久娇喘| 欧美日韩国产综合特黄| 国产日产欧美精品视频| av一区二区三区天堂| 精品一区二区三区不卡少妇av| 五月激情综合在线视频| 激情国产白嫩美女在线观看| 亚洲国产精品久久琪琪| 五月婷婷缴情七月丁香| 国产欧美日韩在线精品一二区 | 久久热在线免费视频精品| 国产超薄黑色肉色丝袜| 亚洲精品国男人在线视频| 日韩高清中文字幕亚洲|