当前位置: 首页 > news >正文

C#与Sqlite数据库

1,一般的访问方式。

1.1,连接语句。
//sqlite 连接,支持相对位置,也支持绝对位置
Data Source=../../Database/cater.db// 连接数据库,FailIfMissing=false时若文件不存在会自动创建
string connStr ="DataSource=test.db;Version=3;Pooling=true;FailIfMissing=false;";
1.2,配置文件设置。
//需在配置文件中进行如下配置否则报错
<startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
 1.3,常用语法。
//语法:
select * from AlarmHistory
insert into alarmhistory (AlarmDetails,starttime) values ('abc',getdate())
//获取当前时间
select datetime('now')
SELECT datetime('now', 'localtime');
select CURRENT_TIMESTAMP//插入当前时间
insert into alarmhistory (alarmdetails,starttime) values('',datetime('now','localtime'))//查找为null的数据
select * from alarmhistory where StartTime is null//修改表格序号update sqlite_sequence set seq = 0 where name = 'AlarmHistory'//查询表格主键
select * from
pragma_table_info ('ActualData') where pk=1//查询表格是否存在
select exists(  select * from sqlite_master where type='table' and name='ActualData')//删除表格drop table 'ActualData' //获取和设置时间,时间格式只支持类似yyyy-MM-dd这样用-连接的格式,若用/连接则无效
select datetime('2024-08-22 16:23:55')
SELECT datetime('now', 'localtime');
1.4,SQLite访问dll。

https://download.csdn.net/download/lingxiao16888/89914696

2,基于EntityFramework的ORM数据访问。

2.1,安装Nuget包

这部分比较简单,直接Nuget包中下载即可

  1. System.Data.SQLite
  2. System.Data.SQLite.EF6
  3. System.Data.SQLite.LINQ
  4. SQLite.CodeFirst

2.2,配置文件需要进行如下修改。
<?xml version="1.0" encoding="utf-8"?>
<configuration><configSections><!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --><section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /></configSections><startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /></startup><entityFramework><providers><provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /><provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" /><provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" /></providers></entityFramework><system.data><DbProviderFactories><remove invariant="System.Data.SQLite.EF6" /><add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" /><remove invariant="System.Data.SQLite" /><add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" /></DbProviderFactories></system.data>
</configuration>
2.3,应用。
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApp2
{class Program{static void Main(string[] args){MyDbContext context = new MyDbContext("cater.db");var set = context.Set<MemmberType>();var ss = set.FirstOrDefault();foreach (var item in set){Console.WriteLine($"{item.MemTpName} ; {item.MemType} ; {item.MemTpDesc} ; {item.SubBy} ;{item.DelFlag}");}Console.WriteLine("输出完成!");Console.ReadKey();}}class MyDbContext : DbContext{public MyDbContext(string constr) : base(new SQLiteConnection{ConnectionString = new SQLiteConnectionStringBuilder{DataSource = constr,ForeignKeys = true}.ConnectionString}, true){}//如果查询 MemmberType 表,则该属性不能省略public virtual DbSet<MemmberType>  MemmberType { get; set; }}[Table("MemmberType")]//该特性不能省略class MemmberType{[Key]//如果存在主键该特性不能省略[Column("MemType",TypeName ="INT")]public int MemType { get; set; }//[Column("MemTpName")]public string MemTpName { get; set; }[Column("MemTpDesc")]//可使用 Required 特性指定该列不能为空public string MemTpDesc { get; set; }//[Column("DelFlag")]public int DelFlag { get; set; }//[Column("SubBy")]public int SubBy { get; set; }}
}
2.4,效果。

数据库文件数据。

查询结果。

http://www.lryc.cn/news/469120.html

相关文章:

  • 2019年计算机网络408真题解析
  • 江协科技STM32学习- P21 ADC模数转换器
  • [RK3566-Android11] 使用SPI方式点LED灯带-JE2815/WS2812,实现呼吸/渐变/随音量变化等效果
  • PostgreSQL用load语句加载插件
  • 一文了解:增强图像搜索之图像嵌入
  • yolov9目标检测/分割预测报错AttributeError: ‘list‘ object has no attribute ‘device‘常见汇总
  • 格姗知识圈博客网站开源了!
  • 【C++】深入理解C++中的类型推导:从auto到decltype的应用与实践
  • 使用Prometheus对微服务性能自定义指标监控
  • 深入解析 Lombok 的实现原理:以 @Builder 为例的实战演示(三)
  • SEO基础:什么是SERP?【百度SEO专家】
  • HTML5教程(一)- 网页与开发工具
  • Java进阶篇设计模式之二 ----- 工厂模式
  • 考研篇——数据结构王道3.2.2_队列的顺序实现
  • 从零开始理解 Trie 树:高效字符串存储与查找的利器【自动补全、拼写检查】
  • 关于sse、websocket与流式渲染
  • Python 语法与数据类型详解
  • LeetCode题练习与总结:扁平化嵌套列表迭代器--341
  • 51单片机快速入门之 AD(模数) DA(数模) 转换 2024/10/25
  • Typora 、 Minio and PicGo 图床搭建
  • 【计网】UDP Echo Server与Client实战:从零开始构建简单通信回显程序
  • 微服务网关Zuul
  • BuildCTF线上赛WP
  • 《使用Gin框架构建分布式应用》阅读笔记:p143-p207
  • 华为网络管理配置实例
  • 大语言模型数据处理方法(基于llama模型)
  • 爱奇艺大数据多 AZ 统一调度架构
  • 【C++篇】栈的层叠与队列的流动:在 STL 的韵律中探寻数据结构的优雅之舞
  • 使用 Flask 实现简单的登录注册功能
  • 计算机毕业设计Python+大模型微博情感分析 微博舆情预测 微博爬虫 微博大数据 舆情分析系统 大数据毕业设计 NLP文本分类 机器学习 深度学习 AI