用于sqlite数据库的SqlLiteHelper.cs 的类

前端之家收集整理的这篇文章主要介绍了用于sqlite数据库的SqlLiteHelper.cs 的类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
需要引用System.Data.sqlite.DLL文件,下载地址 http://good.gd/1936755.htm
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.sqlite;
using System.Data.Common;

namespace JonseTest
{
   public abstract class sqlLiteHelper
    {
       public static string ConnsqlLiteDbPath = string.Empty;
       public static string ConnString
       {
           get
           {
               return string.Format(@"Data Source={0}",ConnsqlLiteDbPath);
           }
       }

       // 取datatable
       public static DataTable GetDataTable(out string sError,string ssql)
       {
           DataTable dt = null;
           sError = string.Empty;

           try
           {
               sqliteConnection conn = new sqliteConnection(ConnString);
               conn.Open();
               sqliteCommand cmd = new sqliteCommand();
               cmd.CommandText = ssql;
               cmd.Connection = conn;
               sqliteDataAdapter dao = new sqliteDataAdapter(cmd);
               dt = new DataTable();
               dao.Fill(dt);
           }
           catch (Exception ex)
           {
               sError = ex.Message;
           }

           return dt;
       }

       // 取dataset
       public static DataSet GetDataSet(out string sError,string ssql)
       {
           DataSet ds = null;
           sError = string.Empty;

           try
           {
               sqliteConnection conn = new sqliteConnection(ConnString);
               conn.Open();
               sqliteCommand cmd = new sqliteCommand();
               cmd.CommandText = ssql;
               cmd.Connection = conn;
               sqliteDataAdapter dao = new sqliteDataAdapter(cmd);
               ds = new DataSet();
               dao.Fill(ds);
           }
           catch (Exception ex)
           {
               sError = ex.Message;
           }

           return ds;
       }

       // 取某个单一的元素
       public static object GetSingle(out string sError,string ssql)
       {
           DataTable dt = GetDataTable(out sError,ssql);
           if (dt != null && dt.Rows.Count > 0)
           {
               return dt.Rows[0][0];
           }

           return null;
       }

       // 取最大的ID
       public static Int32 GetMaxID(out string sError,string sKeyField,string sTableName)
       {
           DataTable dt = GetDataTable(out sError,"select ifnull(max([" + sKeyField + "]),0) as MaxID from [" + sTableName + "]");
           if (dt != null && dt.Rows.Count > 0)
           {
               return Convert.ToInt32(dt.Rows[0][0].ToString());
           }

           return 0;
       }

       // 执行 insert,update,delete 动作,也可以使用事务
       public static bool UpdateData(out string sError,string ssql,bool bUseTransaction=false)
       {
           int iResult = 0;
           sError = string.Empty;

           if (!bUseTransaction)
           {
               try
               {
                   sqliteConnection conn = new sqliteConnection(ConnString);
                   conn.Open();
                   sqliteCommand comm = new sqliteCommand(conn);
                   comm.CommandText = ssql;
                   iResult = comm.ExecuteNonQuery();
               }
               catch (Exception ex)
               {
                   sError = ex.Message;
                   iResult = -1;
               }
           }
           else // 使用事务
           {
               DbTransaction trans =null;
               try
               {
                   sqliteConnection conn = new sqliteConnection(ConnString);
                   conn.Open();
                   trans = conn.BeginTransaction();
                   sqliteCommand comm = new sqliteCommand(conn);
                   comm.CommandText = ssql;
                   iResult = comm.ExecuteNonQuery();
                   trans.Commit();
               }
               catch (Exception ex)
               {
                   sError = ex.Message;
                   iResult = -1;
                   trans.Rollback();
               }
           }

           return iResult >0;
       }

    }
}

 


关于具体如何调用 sqlLiteHelper ,请参考我的博文:

C# 使用sqlite 轻量级数据库

网址: http://www.jb51.cc/article/p-pocobmnt-eq.html

希望对大家有所帮助。

原文链接:https://www.f2er.com/sqlite/202475.html

猜你在找的Sqlite相关文章