源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

SQLite在C#中的安装与操作技巧

  • 时间:2021-10-07 03:59 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:SQLite在C#中的安装与操作技巧
[b]SQLite 介绍[/b] SQLite,是一款轻型的数据库,用于本地的数据储存。 先说说优点,它占用资源非常的低,在嵌入式设备中需要几百K的内存就够了;作为轻量级数据库,他的处理速度也足够快;支持的的容量级别为T级;独立: 没有额外依赖;开源;支持多种语言; [b]我的用途[/b] 在项目开发中,需要做一次数据数据同步。因为数据库实时数据的同步,需要记录更新时间,系统日志等等数据;当然,你也可以选择写ini和xml等等配置文件来解决,但是都如数据库可读性高不是。 [b]安装[/b] 1. 引用 .NET 驱动 [url=http://system.data.sqlite.org/index.html/doc/trunk/www/index.wiki]http://system.data.sqlite.org/index.html/doc/trunk/www/index.wiki[/url] [img]http://files.jb51.net/file_images/article/201708/201708291002013.png[/img] 这三个文件,在项目中,引用之后就可以进行创建数据库查询数据操作。 2.使用vs提供的包管理工具Nuget进行项目引用。 [img]http://files.jb51.net/file_images/article/201708/201708291002014.png[/img] [b]Nuget包管理工具[/b] [img]http://files.jb51.net/file_images/article/201708/201708291002015.png[/img] 搜索SQLite安装对应的包,下载完成后就自动在项目中引用了。 [b]使用[/b] [b]创建数据库[/b]
 //创建一个数据库
 SQLiteConnection.CreateFile("Database.sqlite");
[b]操作数据库[/b]
//创建连接字符串
SQLiteConnection conn = new SQLiteConnection("Data Source=Database.sqlite;Version=3;");
//这是数据库登录密码
conn.SetPassword("1234");
//打开数据库
conn.Open();
string query = "create table table1 (id INTEGER, name VARCHAR)";
//创建命令
SQLiteCommand cmd = new SQLiteCommand(query, conn);
//执行命令
cmd.ExecuteNonQuery();
//释放资源
conn.Close();
[b]插入数据[/b]
SQLiteConnection conn = new SQLiteConnection("Data Source=Database.sqlite;Version=3;");
conn.Open();
string query = "insert into table1 (id,name) values(1,'小明')";
SQLiteCommand cmd = new SQLiteCommand(query, conn);
cmd.ExecuteNonQuery();
conn.Close();
cmd.Dispose();
[b]查询数据[/b]
using (SQLiteConnection conn = new SQLiteConnection("Data Source=Database.sqlite;Version=3;"))
{
  conn.Open();
  string query = "select * from table1";
  SQLiteCommand cmd = new SQLiteCommand(query, conn);
  SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
  DataTable dt = new DataTable();
  da.Fill(dt);
}
[img]http://files.jb51.net/file_images/article/201708/201708291002026.png[/img] [b]可视化工具[/b]  SQLiteStudio 可视化工具    [url=https://sqlitestudio.pl]https://sqlitestudio.pl[/url] 连接数据库 [img]http://files.jb51.net/file_images/article/201708/201708291002027.png[/img] 查表 [img]http://files.jb51.net/file_images/article/201708/201708291002028.png[/img] 设置主键,已经自增。 主键自增类型必须是 INTEGER类型 [img]http://files.jb51.net/file_images/article/201708/201708291002029.png[/img] [b]其他[/b] 1.SQLite .NET驱动设置数据库读取密码 .net驱动之中,提供了单独设置密码和登录密码
using (SQLiteConnection conn = new SQLiteConnection("Data Source=Database.sqlite;Version=3;"))
{
  conn.Open();
  //设置数据库密码
  conn.ChangePassword("123456");
  conn.Clone();
}
登录带密码的数据库
using (SQLiteConnection conn = new SQLiteConnection("Data Source=Database.sqlite;Version=3;"))
{
  conn.SetPassword("123456");
  conn.Open();
  string query = "select * from table1";
  SQLiteCommand cmd = new SQLiteCommand(query, conn);
  SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
  DataTable dt = new DataTable();
  da.Fill(dt);
  conn.Clone();
}
[img]http://files.jb51.net/file_images/article/201708/2017082910020210.png[/img] 密码正确查询成功 [img]http://files.jb51.net/file_images/article/201708/2017082910020211.png[/img] 密码错误查询异常 还有就是密码设置错误,打开数据库后数据库状态依旧是打开状态,但是查询后出现异常无法查询。 [img]http://files.jb51.net/file_images/article/201708/2017082910020212.png[/img] 使用 dotnet驱动设置密码之后,使用其他框架驱动貌似就无法打开了。 [b]FQA[/b] 1.大量数据频繁Insert特别慢怎么办?   解决办法是使用事务来Insert数据.   SQLite给出的解释是:正常执行Insert,每一次执行都占用一次IO,而使用事务执行,直到Insert结束只占用一次IO;  执行事务Insert代码
private bool QueryTran(List<string> queryList)
{
  SQLiteConnection conn = new SQLiteConnection("Data Source=DataBase;Version=3;");
  SQLiteCommand cmd = conn.CreateCommand();
  conn.Open();
  SQLiteTransaction tran = conn.BeginTransaction();
  bool check = false;
  try
  {
    foreach (string item in queryList)
    {
      cmd.CommandText = item;
      cmd.ExecuteNonQuery();
    }
    tran.Commit();
    check = true;
  }
  catch (Exception ex)
  {
    tran.Rollback();
    check = false;
    throw ex;
  }
  finally
  {
    conn.Close();
  }
  return check;
}
[b]总结[/b] 以上所述是小编给大家介绍的SQLite在C#中的安装与操作技巧,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程素材网网站的支持!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部