C#与Sqlite数据库

devtools/2024/10/23 8:31:11/

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.ppmy.cn/devtools/128115.html

相关文章

Pr 视频效果:视频限制器

视频效果/颜色校正/视频限制器 Color Correction/Video Limiter 视频限制器 Video Limiter效果可用于确保视频信号符合广播和电视标准。它可以防止视频的亮度和色彩超出指定的范围&#xff0c;避免在播放时出现过曝、过暗或色彩失真等问题。 ◆ ◆ ◆ 效果选项说明 此效果也称…

Ubuntu配置FTP

Ubuntu配置FTP 切换root用户 sudo -i安装vsftpd软件包 apt update apt install vsftpd -y启动vsftp服务并设置自启动 systemctl start vsftpd systemctl enable vsftpd关闭防火墙 ufw disable ufw status创建FTP用户 useradd -m ftpuser passwd ftpuser设置用户的主目录为…

nnUnet 大模型学习笔记(续):训练网络(3d_fullres)以及数据集标签的处理

目录 1. 数据集处理 1.1 实现脚本 1.2 json文件 2. 设置读取路径 2.1 设置路径 2.2 数据集转换 2.3 数据集预处理 2.4 训练&#xff08;3d_fullres) 3. 训练结果展示 关于nnUnet 数据集的处理和环境搭建&#xff0c;参考上文&#xff1a;第四章&#xff1a;nnUnet大模…

Java实现html填充导出pdf

Java实现html填充导出pdf 1.依赖添加和pom修改 <!-- Thymeleaf 模板引擎 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><!-- OpenPDF 库 -…

线性可分支持向量机的原理推导 9-32线性分类超平面的位置 公式解析

本文是将文章《线性可分支持向量机的原理推导》中的公式单独拿出来做一个详细的解析&#xff0c;便于初学者更好的理解。 公式 9-32 是线性可分支持向量机&#xff08;SVM&#xff09;中的一个关键公式&#xff0c;用于表达线性分类超平面的位置。通过这个公式&#xff0c;我们…

深度学习:Yolo系列 V1和V2的对比

YOLO V1和V2的对比&#xff1a; BatchNorm V2版本舍弃dropout&#xff0c;卷积后全部加入batch normalization网络的每一层的输入都做了归一化&#xff0c;收敛相对更容易&#xff0c;经过batch normalization处理后的网络会提升2%的map&#xff0c;从现在的角度来看&#xff…

六万字77道Spring面试题总结(2024版)

文章目录 问题1&#xff1a;什么是Spring?问题2&#xff1a;Spring的两大核心概念是什么&#xff1f;问题3&#xff1a;Spring框架的设计目标、设计理念和核心是什么&#xff1f;问题4&#xff1a;Spring的优缺点是什么&#xff1f;问题5&#xff1a;Spring有哪些应用场景&…

数据结构与算法分析:你真的理解排序算法吗——中值排序(万字长文+代码详解)

一、算法描述 在计算机科学中&#xff0c;分治是一种非常常见的方法&#xff0c;它将一个问题分成两个独立的子问题&#xff0c;每个子问题的规模是原始问题规模的一般。将两个原始数组细分成两个不同的子数组&#xff0c;每个子数组的大小是原始大小的一半&#xff0c;这两个…