服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C# SQLite数据库入门使用说明

C# SQLite数据库入门使用说明

2022-03-05 17:06任前程 C#

这篇文章主要给大家介绍了关于C#中SQLite数据库入门使用的相关资料,文中通过图文以及示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

前言

我们在开发应用是经常会需要用到一些数据的存储,存储的方式有多种,使用数据库是一种比较受大家欢迎的方式。但是对于一些小型的应用,如一些移动app,通常的数据库过于庞大,而轻便的sqlite则能解决这一问题。不但操作方便,而且只需要要一个文件即可,在这里我们来说一说使用c#语言操作sqlite数据库。

一、nuget引入sqlite库

在vs菜单:工具→nuget包管理器→管理解决方案的nuget程序包 打开nuget解决方案窗口。

搜索 sqlite,选择官方的库安装到指定的项目中。:

C# SQLite数据库入门使用说明

提示:system.data.sqlite 分为 x86 和 x64 版本,这里推荐使用nuget自动安装。使用 any cpu 编译的时候,会自动拷贝32位和64位 interop dll文件到子目录中。程序运行的时候会根据电脑的运行环境自动选择合适的dll。

C# SQLite数据库入门使用说明

二、dbhelper类库

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using system.collections.generic;
using system.data;
using system.data.sqlite;
using system.configuration;
using system.data.sqlclient;
//using mysql.data.mysqlclient;
 
namespace consoleapp5
{
 public class dbhelper
 {
  private readonly static string connstr = configurationmanager.connectionstrings["data source=mesclient.sqlite;version=3"].connectionstring;
 
  //获取 appsetting 设置的值
  //private readonly static string appstr = configurationmanager.appsettings["testkey"];
 
  //获取 connection 对象
  public static idbconnection createconnection()
  {
   idbconnection conn = new sqliteconnection(connstr);//mysqlconnection //sqlconnection
   conn.open();
   return conn;
  }
 
  //执行非查询语句
  public static int executenonquery(idbconnection conn, string sql, dictionary<string, object> parameters)
  {
   using (idbcommand cmd = conn.createcommand())
   {
    cmd.commandtext = sql;
    foreach (keyvaluepair<string, object> keyvaluepair in parameters)
    {
     idbdataparameter parameter = cmd.createparameter();
     parameter.parametername = keyvaluepair.key;
     parameter.value = keyvaluepair.value;
     cmd.parameters.add(parameter);
    }
    return cmd.executenonquery();
   }
  }
 
  //执行非查询语句-独立连接
  public static int executenonquery(string sql, dictionary<string, object> parameters)
  {
   using (idbconnection conn = createconnection())
   {
    return executenonquery(conn, sql, parameters);
   }
  }
 
  //查询首行首列
  public static object executescalar(idbconnection conn, string sql, dictionary<string, object> parameters)
  {
   using (idbcommand cmd = conn.createcommand())
   {
    cmd.commandtext = sql;
    foreach (keyvaluepair<string, object> keyvaluepair in parameters)
    {
     idbdataparameter parameter = cmd.createparameter();
     parameter.parametername = keyvaluepair.key;
     parameter.value = keyvaluepair.value;
     cmd.parameters.add(parameter);
    }
    return cmd.executescalar();
   }
  }
 
  //查询首行首列-独立连接
  public static object executescalar(string sql, dictionary<string, object> parameters)
  {
   using (idbconnection conn = createconnection())
   {
    return executescalar(conn, sql, parameters);
   }
  }
 
  //查询表
  public static datatable executequery(idbconnection conn, string sql, dictionary<string, object> parameters)
  {
   datatable dt = new datatable();
   using (idbcommand cmd = conn.createcommand())
   {
    cmd.commandtext = sql;
    foreach (keyvaluepair<string, object> keyvaluepair in parameters)
    {
     idbdataparameter parameter = cmd.createparameter();
     parameter.parametername = keyvaluepair.key;
     parameter.value = keyvaluepair.value;
     cmd.parameters.add(parameter);
    }
    using (idatareader reader = cmd.executereader())
    {
     dt.load(reader);
    }
   }
 
   return dt;
  }
 
  //查询表--独立连接
  public static datatable executequery(string sql, dictionary<string, object> parameters)
  {
   using (idbconnection conn = createconnection())
   {
    return executequery(conn, sql, parameters);
   }
  }
 }
}

三、基本使用

1. 判断数据文件是否存在

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/// <summary>
/// 检查数据库是否存在不存在创建
/// </summary>
/// <returns></returns>
public static bool checkdatabase()
{
 try
 {
  //判断数据文件是否存在
  bool dbexist = file.exists("mesclient.sqlite");
  if (!dbexist)
  {
   sqliteconnection.createfile("mesclient.sqlite");
  }
 
 
  return true;
 }
 catch (exception)
 {
  return false;
 }
}

2. 判断表是否存在

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/// <summary>
/// 检查数据表是否存在,不存在创建
/// </summary>
/// <returns></returns>
public static bool checkdatatable(string connstr)
{
 try
 {
  using (sqliteconnection conn = new sqliteconnection(connstr))
  using (sqlitecommand cmd = conn.createcommand())
  {
   conn.open();
   cmd.commandtext = "select count(*) from sqlite_master where type = 'table' and name = 'serverinfo'";
   object ob = cmd.executescalar();
   long tablecount = convert.toint64(ob);
   if (tablecount == 0)
   {
    //创建表
    cmd.commandtext = @"
   begin;
    create table serverinfo
    (id integer primary key autoincrement,name text,
    url text,delaytime integer,usagecounter integer,
     status integer,createtime datetime);
    create unique index idx_serverinfo on serverinfo (name);
   commit;
   ";
    //此语句返回结果为0
    int rowcount = cmd.executenonquery();
    return true;
   }
   else if (tablecount > 1)
   {
    return false;
   }
   else
   {
    return true;
   }
  }
 }
 catch (exception ex)
 {
  return false;
 }
}

3. 查询

?
1
2
3
4
5
6
7
8
9
10
11
string sql = "select * from serverinfo where name =@servername and url = @url and date(createtime)=date(@date);";
dictionary<string, object> parameters = new dictionary<string, object>();
parameters.add("servername",endpointelement.name);
parameters.add("url", endpointelement.address);
parameters.add("date", datetime.now.tostring("yyyy-mm-dd"));
datatable dt=sqlitehelper.executequery(connstr, sql, parameters);
if (dt.rows.count>0)
{
 usagecounter = dt.rows[0].field<long>("usagecounter");
 gettime = dt.rows[0].field<datetime>("createtime");
}

4. 新增/修改

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//存在更新,不存在插入
string updatesql = "replace into serverinfo(name,url,delaytime,usagecounter, status,createtime) values(@name,@url,@delaytime,@usagecounter,@status, @createtime)";
dictionary<string, object> ups = new dictionary<string, object>();
ups.add("name", name);
ups.add("url", url);
ups.add("delaytime", delaytime);
ups.add("usagecounter", usagecounter);
ups.add("status", status);
ups.add("createtime", datetime.now.tostring("yyyy-mm-dd hh:mm:ss"));
int count= sqlitehelper.executenonquery(connstr, updatesql, ups);
if (count>0)
{
 return true;
}
else
{
 return false;
}

5. 删除

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//删除记录
string updatesql =
 "delete from serverinfo where content=@content and flag=@flag;";
dictionary<string, object> updateparameters = new dictionary<string, object>();
updateparameters.add("content", content);
updateparameters.add("flag", flag);
int count = sqlitehelper.executenonquery(connstr, updatesql, updateparameters);
if (count > 0)
{
 return true;
}
else
{
 return false;
}

四、参考文章

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:https://renqiancheng.com/2018/11/C-SQLite-数据库使用说明.html

延伸 · 阅读

精彩推荐
  • C#浅谈C# winForm 窗体闪烁的问题

    浅谈C# winForm 窗体闪烁的问题

    下面小编就为大家带来一篇浅谈C# winForm 窗体闪烁的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网7962021-12-21
  • C#Unity3D UGUI实现缩放循环拖动卡牌展示效果

    Unity3D UGUI实现缩放循环拖动卡牌展示效果

    这篇文章主要为大家详细介绍了Unity3D UGUI实现缩放循环拖动展示卡牌效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参...

    诗远3662022-03-11
  • C#C#直线的最小二乘法线性回归运算实例

    C#直线的最小二乘法线性回归运算实例

    这篇文章主要介绍了C#直线的最小二乘法线性回归运算方法,实例分析了给定一组点,用最小二乘法进行线性回归运算的实现技巧,具有一定参考借鉴价值,需要...

    北风其凉8912021-10-18
  • C#C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    这篇文章主要介绍了C#实现的文件操作封装类,结合完整实例形式分析了C#封装文件的删除,移动,复制,重命名等操作相关实现技巧,需要的朋友可以参考下...

    Rising_Sun3892021-12-28
  • C#C#基础之泛型

    C#基础之泛型

    泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能。接下来通过本文给大家介绍c#基础之泛型,感兴趣的朋友一起学习吧...

    方小白7732021-12-03
  • C#c#学习之30分钟学会XAML

    c#学习之30分钟学会XAML

    一个界面程序的核心,无疑就是界面和后台代码,而xaml就是微软为构建应用程序界面而创建的一种描述性语言,也就是说,这东西是搞界面的...

    C#教程网8812021-12-10
  • C#C# 后台处理图片的几种方法

    C# 后台处理图片的几种方法

    本篇文章主要介绍了C# 后台处理图片的几种方法,非常具有实用价值,需要的朋友可以参考下。...

    IT小伙儿10162021-12-08
  • C#聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题,新手速来围观,一个通俗易懂的例子帮助大家更好的理解C#接口问题,感兴趣的小伙伴们可以参考一下...

    zenkey7072021-12-03