程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#封裝的Sqlite拜訪類實例

C#封裝的Sqlite拜訪類實例

編輯:C#入門知識

C#封裝的Sqlite拜訪類實例。本站提示廣大學習愛好者:(C#封裝的Sqlite拜訪類實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#封裝的Sqlite拜訪類實例正文


本文實例講述了C#封裝的Sqlite拜訪類。分享給年夜家供年夜家參考。詳細剖析以下:

C#封裝的Sqlite拜訪類,要拜訪Sqlite這下簡略了,直接挪用此類中的辦法便可

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Configuration;
using System.Data.SQLite;
namespace DAL
{
 internal abstract class DbHelperSQLite
 {  
  public static string connectionString = "Data Source=" + AppDomain.CurrentDomain.BaseDirectory + @"dataleaf.db;Version=3;";
  public DbHelperSQLite()
  {
  }
  #region 公用辦法
  public static int GetMaxID(string FieldName, string TableName)
  {
   string strsql = "select max(" + FieldName + ")+1 from " + TableName;
   object obj = GetSingle(strsql);
   if (obj == null)
   {
    return 1;
   }
   else
   {
    return int.Parse(obj.ToString());
   }
  }
  public static bool Exists(string strSql)
  {
   object obj = GetSingle(strSql);
   int cmdresult;
   if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
   {
    cmdresult = 0;
   }
   else
   {
    cmdresult = int.Parse(obj.ToString());
   }
   if (cmdresult == 0)
   {
    return false;
   }
   else
   {
    return true;
   }
  }
  public static bool Exists(string strSql, params SQLiteParameter[] cmdParms)
  {
   object obj = GetSingle(strSql, cmdParms);
   int cmdresult;
   if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
   {
    cmdresult = 0;
   }
   else
   {
    cmdresult = int.Parse(obj.ToString());
   }
   if (cmdresult == 0)
   {
    return false;
   }
   else
   {
    return true;
   }
  }
  #endregion
  #region 履行簡略SQL語句
  /// <summary>
  /// 履行SQL語句,前往影響的記載數
  /// </summary>
  /// <param name="SQLString">SQL語句</param>
  /// <returns>影響的記載數</returns>
  public static int ExecuteSql(string SQLString)
  {
   using (SQLiteConnection connection = new SQLiteConnection(connectionString))
   {
    using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
    {
     try
     {
      connection.Open();
      int rows = cmd.ExecuteNonQuery();
      return rows;
     }
     catch (System.Data.SQLite.SQLiteException E)
     {
      connection.Close();
      throw new Exception(E.Message);
     }
    }
   }
  }
  /// <summary>
  /// 履行多條SQL語句,完成數據庫事務。
  /// </summary>
  /// <param name="SQLStringList">多條SQL語句</param>  
  public static void ExecuteSqlTran(ArrayList SQLStringList)
  {
   using (SQLiteConnection conn = new SQLiteConnection(connectionString))
   {
    conn.Open();
    SQLiteCommand cmd = new SQLiteCommand();
    cmd.Connection = conn;
    SQLiteTransaction tx = conn.BeginTransaction();
    cmd.Transaction = tx;
    try
    {
     for (int n = 0; n < SQLStringList.Count; n++)
     {
      string strsql = SQLStringList[n].ToString();
      if (strsql.Trim().Length > 1)
      {
       cmd.CommandText = strsql;
       cmd.ExecuteNonQuery();
      }
     }
     tx.Commit();
    }
    catch (System.Data.SQLite.SQLiteException E)
    {
     tx.Rollback();
     throw new Exception(E.Message);
    }
   }
  }
  /// <summary>
  /// 履行帶一個存儲進程參數的的SQL語句。
  /// </summary>
  /// <param name="SQLString">SQL語句</param>
  /// <param name="content">參數內容,好比一個字段是格局龐雜的文章,有特別符號,可以經由過程這個方法添加</param>
  /// <returns>影響的記載數</returns>
  public static int ExecuteSql(string SQLString, string content)
  {
   using (SQLiteConnection connection = new SQLiteConnection(connectionString))
   {
    SQLiteCommand cmd = new SQLiteCommand(SQLString, connection);
    SQLiteParameter myParameter = new SQLiteParameter("@content", DbType.String);
    myParameter.Value = content;
    cmd.Parameters.Add(myParameter);
    try
    {
     connection.Open();
     int rows = cmd.ExecuteNonQuery();
     return rows;
    }
    catch (System.Data.SQLite.SQLiteException E)
    {
     throw new Exception(E.Message);
    }
    finally
    {
     cmd.Dispose();
     connection.Close();
    }
   }
  }
  /// <summary>
  /// 向數據庫裡拔出圖象格局的字段(和下面情形相似的另外一種實例)
  /// </summary>
  /// <param name="strSQL">SQL語句</param>
  /// <param name="fs">圖象字節,數據庫的字段類型為image的情形</param>
  /// <returns>影響的記載數</returns>
  public static int ExecuteSqlInsertImg(string strSQL, byte[] fs)
  {
   using (SQLiteConnection connection = new SQLiteConnection(connectionString))
   {
    SQLiteCommand cmd = new SQLiteCommand(strSQL, connection);
    SQLiteParameter myParameter = new SQLiteParameter("@fs", DbType.Binary);
    myParameter.Value = fs;
    cmd.Parameters.Add(myParameter);
    try
    {
     connection.Open();
     int rows = cmd.ExecuteNonQuery();
     return rows;
    }
    catch (System.Data.SQLite.SQLiteException E)
    {
     throw new Exception(E.Message);
    }
    finally
    {
     cmd.Dispose();
     connection.Close();
    }
   }
  }
  /// <summary>
  /// 履行一條盤算查詢成果語句,前往查詢成果(object)。
  /// </summary>
  /// <param name="SQLString">盤算查詢成果語句</param>
  /// <returns>查詢成果(object)</returns>
  public static object GetSingle(string SQLString)
  {
   using (SQLiteConnection connection = new SQLiteConnection(connectionString))
   {
    using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
    {
     try
     {
      connection.Open();
      object obj = cmd.ExecuteScalar();
      if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
      {
       return null;
      }
      else
      {
       return obj;
      }
     }
     catch (System.Data.SQLite.SQLiteException e)
     {
      connection.Close();
      throw new Exception(e.Message);
     }
    }
   }
  }
  /// <summary>
  /// 履行查詢語句,前往SQLiteDataReader
  /// </summary>
  /// <param name="strSQL">查詢語句</param>
  /// <returns>SQLiteDataReader</returns>
  public static SQLiteDataReader ExecuteReader(string strSQL)
  {
   SQLiteConnection connection = new SQLiteConnection(connectionString);
   SQLiteCommand cmd = new SQLiteCommand(strSQL, connection);
   try
   {
    connection.Open();
    SQLiteDataReader myReader = cmd.ExecuteReader();
    return myReader;
   }
   catch (System.Data.SQLite.SQLiteException e)
   {
    throw new Exception(e.Message);
   }
  }
  /// <summary>
  /// 履行查詢語句,前往DataSet
  /// </summary>
  /// <param name="SQLString">查詢語句</param>
  /// <returns>DataSet</returns>
  public static DataSet Query(string SQLString)
  {
   using (SQLiteConnection connection = new SQLiteConnection(connectionString))
   {
    DataSet ds = new DataSet();
    try
    {
     connection.Open();
     SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
     command.Fill(ds, "ds");
    }
    catch (System.Data.SQLite.SQLiteException ex)
    {
     throw new Exception(ex.Message);
    }
    return ds;
   }
  }
  #endregion
  #region 履行帶參數的SQL語句
  /// <summary>
  /// 履行SQL語句,前往影響的記載數
  /// </summary>
  /// <param name="SQLString">SQL語句</param>
  /// <returns>影響的記載數</returns>
  public static int ExecuteSql(string SQLString, params SQLiteParameter[] cmdParms)
  {
   using (SQLiteConnection connection = new SQLiteConnection(connectionString))
   {
    using (SQLiteCommand cmd = new SQLiteCommand())
    {
     try
     {
      PrepareCommand(cmd, connection, null, SQLString, cmdParms);
      int rows = cmd.ExecuteNonQuery();
      cmd.Parameters.Clear();
      return rows;
     }
     catch (System.Data.SQLite.SQLiteException E)
     {
      throw new Exception(E.Message);
     }
    }
   }
  }
  /// <summary>
  /// 履行多條SQL語句,完成數據庫事務。
  /// </summary>
  /// <param name="SQLStringList">SQL語句的哈希表(key為sql語句,value是該語句的SQLiteParameter[])</param>
  public static void ExecuteSqlTran(Hashtable SQLStringList)
  {
   using (SQLiteConnection conn = new SQLiteConnection(connectionString))
   {
    conn.Open();
    using (SQLiteTransaction trans = conn.BeginTransaction())
    {
     SQLiteCommand cmd = new SQLiteCommand();
     try
     {
      //輪回
      foreach (DictionaryEntry myDE in SQLStringList)
      {
       string cmdText = myDE.Key.ToString();
       SQLiteParameter[] cmdParms = (SQLiteParameter[])myDE.Value;
       PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
       int val = cmd.ExecuteNonQuery();
       cmd.Parameters.Clear();
       trans.Commit();
      }
     }
     catch
     {
      trans.Rollback();
      throw;
     }
    }
   }
  }
  /// <summary>
  /// 履行一條盤算查詢成果語句,前往查詢成果(object)。
  /// </summary>
  /// <param name="SQLString">盤算查詢成果語句</param>
  /// <returns>查詢成果(object)</returns>
  public static object GetSingle(string SQLString, params SQLiteParameter[] cmdParms)
  {
   using (SQLiteConnection connection = new SQLiteConnection(connectionString))
   {
    using (SQLiteCommand cmd = new SQLiteCommand())
    {
     try
     {
      PrepareCommand(cmd, connection, null, SQLString, cmdParms);
      object obj = cmd.ExecuteScalar();
      cmd.Parameters.Clear();
      if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
      {
       return null;
      }
      else
      {
       return obj;
      }
     }
     catch (System.Data.SQLite.SQLiteException e)
     {
      throw new Exception(e.Message);
     }
    }
   }
  }
  /// <summary>
  /// 履行查詢語句,前往SQLiteDataReader
  /// </summary>
  /// <param name="strSQL">查詢語句</param>
  /// <returns>SQLiteDataReader</returns>
  public static SQLiteDataReader ExecuteReader(string SQLString, params SQLiteParameter[] cmdParms)
  {
   SQLiteConnection connection = new SQLiteConnection(connectionString);
   SQLiteCommand cmd = new SQLiteCommand();
   try
   {
    PrepareCommand(cmd, connection, null, SQLString, cmdParms);
    SQLiteDataReader myReader = cmd.ExecuteReader();
    cmd.Parameters.Clear();
    return myReader;
   }
   catch (System.Data.SQLite.SQLiteException e)
   {
    throw new Exception(e.Message);
   }
  }
  /// <summary>
  /// 履行查詢語句,前往DataSet
  /// </summary>
  /// <param name="SQLString">查詢語句</param>
  /// <returns>DataSet</returns>
  public static DataSet Query(string SQLString, params SQLiteParameter[] cmdParms)
  {
   using (SQLiteConnection connection = new SQLiteConnection(connectionString))
   {
    SQLiteCommand cmd = new SQLiteCommand();
    PrepareCommand(cmd, connection, null, SQLString, cmdParms);
    using (SQLiteDataAdapter da = new SQLiteDataAdapter(cmd))
    {
     DataSet ds = new DataSet();
     try
     {
      da.Fill(ds, "ds");
      cmd.Parameters.Clear();
     }
     catch (System.Data.SQLite.SQLiteException ex)
     {
      throw new Exception(ex.Message);
     }
     return ds;
    }
   }
  }
  private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, SQLiteTransaction trans, string cmdText, SQLiteParameter[] cmdParms)
  {
   if (conn.State != ConnectionState.Open)
    conn.Open();
   cmd.Connection = conn;
   cmd.CommandText = cmdText;
   if (trans != null)
    cmd.Transaction = trans;
   cmd.CommandType = CommandType.Text;//cmdType;
   if (cmdParms != null)
   {
    foreach (SQLiteParameter parm in cmdParms)
     cmd.Parameters.Add(parm);
   }
  }
  #endregion
 }
}

願望本文所述對年夜家的C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved