热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C#SQLite数据库操作封装

C#操作sqllite数据库常用操作:publicclassSQLiteDatabase{FieldsprivateSystem.Data.SQLite.SQLite

  C#操作sqllite数据库常用操作:

public class SQLiteDatabase {// Fieldsprivate System.Data.SQLite.SQLiteConnection m_Connection;private string m_strFileName;// Methodspublic SQLiteDatabase(string strFileName, string strPassword, bool bReadOnly = false, bool b加载数据到内存 = false){if (b加载数据到内存 == false){m_strFileName = strFileName;if (bReadOnly){try{this.OpenReadOnly(strFileName, strPassword);}catch{this.OpenReadWrite(strFileName, strPassword);}}else{this.OpenReadWrite(strFileName, strPassword);}}else{m_Connection = new System.Data.SQLite.SQLiteConnection("Data Source=:memory:");m_Connection.Open();}}public string FileName{get{return m_strFileName;}}public static void CreateDBFile(string strFileName){try{if (System.IO.File.Exists(strFileName)) System.IO.File.Delete(strFileName);System.Data.SQLite.SQLiteConnection.CreateFile(strFileName);}catch{}}private void ChangeNewConnection(System.Data.SQLite.SQLiteConnection m_NewConnection){m_Connection.Clone();m_Connection = m_NewConnection;}public void CloseConnection(){m_Connection.ReleaseMemory();m_Connection.Close();}public void ExecuteSQL(string strSQL){ System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand(null, m_Connection);cmd.CommandType = System.Data.CommandType.Text;cmd.CommandText = strSQL;cmd.ExecuteNonQuery();cmd.Dispose();}public void ExecuteSQL(List listSql)//, System.Data.SQLite.SQLiteTransaction tran)
{System.Data.SQLite.SQLiteCommand cmd &#61; new System.Data.SQLite.SQLiteCommand(null, m_Connection);cmd.Transaction &#61; m_Connection.BeginTransaction(); ;// tran;foreach (string strSQL in listSql){cmd.CommandType &#61; CommandType.Text;cmd.CommandText &#61; strSQL;cmd.ExecuteNonQuery();}cmd.Transaction.Commit();cmd.Dispose();}public bool FieldExists(string strTableName, string strFieldName){DataTable table &#61; this.GetTableBySQL(String.Format("select * from &#39;{0}&#39; limit 0,1", strTableName));if (table.Columns.Contains(strFieldName))return true;return false;}public int GetLastID(){System.Data.SQLite.SQLiteCommand command &#61; new System.Data.SQLite.SQLiteCommand(null, m_Connection){CommandType &#61; CommandType.Text,CommandText &#61; "select last_insert_rowid()"};return Convert.ToInt32(command.ExecuteScalar());}public DataTable GetTableBySQL(string strSQL, bool bAddWithKey &#61; false){System.Data.SQLite.SQLiteCommand selectCommand &#61; new System.Data.SQLite.SQLiteCommand(null, m_Connection){CommandType &#61; CommandType.Text,CommandText &#61; strSQL};System.Data.SQLite.SQLiteDataAdapter adapter &#61; new System.Data.SQLite.SQLiteDataAdapter(selectCommand);if (bAddWithKey) adapter.MissingSchemaAction &#61; MissingSchemaAction.AddWithKey;DataTable dataTable &#61; new DataTable();adapter.Fill(dataTable);return dataTable;}public List<string> GetTableName(){List<string> list &#61; new List<string>();foreach (DataRow row in this.GetTableBySQL("select * from sqlite_master where type&#61;&#39;table&#39;").Rows){list.Add(row["name"].ToString().ToUpper());}return list;}public System.Data.Common.DbCommand NewCommand(string strCommandText){return new System.Data.SQLite.SQLiteCommand(strCommandText, m_Connection);}public void OpenConnection(){m_Connection.Open();}private void OpenReadOnly(string strFileName, string strPassword){m_Connection &#61; new System.Data.SQLite.SQLiteConnection();System.Data.SQLite.SQLiteConnectionStringBuilder connstr &#61; new System.Data.SQLite.SQLiteConnectionStringBuilder();connstr.DataSource &#61; strFileName;connstr.Password &#61; strPassword;//设置密码&#xff0c;SQLite ADO.NET实现了数据库密码保护connstr.ReadOnly &#61; true;m_Connection.ConnectionString &#61; connstr.ToString();m_Connection.Open();}private void OpenReadWrite(string strFileName, string strPassword){m_Connection &#61; new System.Data.SQLite.SQLiteConnection();System.Data.SQLite.SQLiteConnectionStringBuilder connstr &#61; new System.Data.SQLite.SQLiteConnectionStringBuilder();connstr.DataSource &#61; strFileName;connstr.Password &#61; strPassword;connstr.ReadOnly &#61; false;m_Connection.ConnectionString &#61; connstr.ToString();m_Connection.Open();}public System.Data.SQLite.SQLiteTransaction BeginTransaction(){return m_Connection.BeginTransaction();}public void ExecuteNonQueryTransaction(string sql, List listSQLiteParameter, System.Data.SQLite.SQLiteTransaction targetTransaction, int commandTimeout &#61; 600){System.Data.SQLite.SQLiteCommand sqliteCommand &#61; new System.Data.SQLite.SQLiteCommand(sql, m_Connection, targetTransaction);sqliteCommand.CommandTimeout &#61; commandTimeout;sqliteCommand.Parameters.AddRange(listSQLiteParameter.ToArray());sqliteCommand.ExecuteNonQuery();sqliteCommand.Dispose();}public DataRow GetRowBySQL(string strSQL){DataTable dTable &#61; GetTableBySQL(strSQL);if (dTable.Rows.Count &#61;&#61; 0)return null;elsereturn dTable.Rows[0];}public bool TableExists(string strTableName){List<string> listTableName &#61; GetTableName();foreach (string s in listTableName){if (string.Compare(s, strTableName, true) &#61;&#61; 0)return true;}return false;}public DataTable GetColumnTable(string strTableName){throw new NotImplementedException();}//备份数据库到另一个连接public Truelore.Common2.Database.SQLiteDatabase CreateNewMemeoyBackupDatabase(){Truelore.Common2.Database.SQLiteDatabase newSQLiteDatabase &#61; new Truelore.Common2.Database.SQLiteDatabase("", "", false, true); System.Data.SQLite.SQLiteConnection m_ConnectionInMemeoy &#61; new System.Data.SQLite.SQLiteConnection("Data Source&#61;:memory:");m_ConnectionInMemeoy.Open();m_Connection.BackupDatabase(m_ConnectionInMemeoy, "main", "main", -1, null, 0);newSQLiteDatabase.ChangeNewConnection(m_ConnectionInMemeoy);return newSQLiteDatabase;}}

 

转:https://www.cnblogs.com/huashengdoujiao/p/9779936.html



推荐阅读
  • 本文讨论了在使用sp_msforeachdb执行动态SQL命令时,当发生错误时如何捕获数据库名称。提供了两种解决方案,并介绍了如何正确使用'?'来显示数据库名称。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • 解决.net项目中未注册“microsoft.ACE.oledb.12.0”提供程序的方法
    在开发.net项目中,通过microsoft.ACE.oledb读取excel文件信息时,报错“未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序”。本文提供了解决这个问题的方法,包括错误描述和代码示例。通过注册提供程序和修改连接字符串,可以成功读取excel文件信息。 ... [详细]
  • 本文详细介绍了SQL日志收缩的方法,包括截断日志和删除不需要的旧日志记录。通过备份日志和使用DBCC SHRINKFILE命令可以实现日志的收缩。同时,还介绍了截断日志的原理和注意事项,包括不能截断事务日志的活动部分和MinLSN的确定方法。通过本文的方法,可以有效减小逻辑日志的大小,提高数据库的性能。 ... [详细]
  • Oracle Database 10g许可授予信息及高级功能详解
    本文介绍了Oracle Database 10g许可授予信息及其中的高级功能,包括数据库优化数据包、SQL访问指导、SQL优化指导、SQL优化集和重组对象。同时提供了详细说明,指导用户在Oracle Database 10g中如何使用这些功能。 ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • 解决VS写C#项目导入MySQL数据源报错“You have a usable connection already”问题的正确方法
    本文介绍了在VS写C#项目导入MySQL数据源时出现报错“You have a usable connection already”的问题,并给出了正确的解决方法。详细描述了问题的出现情况和报错信息,并提供了解决该问题的步骤和注意事项。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 本文讨论了在数据库打开和关闭状态下,重新命名或移动数据文件和日志文件的情况。针对性能和维护原因,需要将数据库文件移动到不同的磁盘上或重新分配到新的磁盘上的情况,以及在操作系统级别移动或重命名数据文件但未在数据库层进行重命名导致报错的情况。通过三个方面进行讨论。 ... [详细]
  • 本文详细介绍了如何使用MySQL来显示SQL语句的执行时间,并通过MySQL Query Profiler获取CPU和内存使用量以及系统锁和表锁的时间。同时介绍了效能分析的三种方法:瓶颈分析、工作负载分析和基于比率的分析。 ... [详细]
  • 本文介绍了将mysql从5.6.15升级到5.7.15的详细步骤,包括关闭访问、备份旧库、备份权限、配置文件备份、关闭旧数据库、安装二进制、替换配置文件以及启动新数据库等操作。 ... [详细]
  • WhenIusepythontoapplythepymysqlmoduletoaddafieldtoatableinthemysqldatabase,itdo ... [详细]
  • 我们有(据我所知)星型模式SQL数据库中的数据文件。该数据库有5个不同的文件,扩展名为 ... [详细]
  • 在Oracle11g以前版本中的的DataGuard物理备用数据库,可以以只读的方式打开数据库,但此时MediaRecovery利用日志进行数据同步的过 ... [详细]
author-avatar
手机用户2602886175
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有