SqlSugar操作Sqlite数据库

news/2024/9/22 5:43:47

SqlSugar操作Sqlite数据库

SqlSugar官网

.net core和.net5/.net6/.net7/.net8/.net9/.net10

  安装SqlSugarCore。

net framework4.6+ 

  安装SqlSugar

以下代码都在一个SqlSugarMethod类中。

获得数据库对象:

  这里要注意的是FilePath路径为生成程序的目录\bin\Debug\net8.0内,而不是代码目录内。

 private string FilePath = Environment.CurrentDirectory + @"\SqlSugarDB\SugarSQL.db";private SqlSugarClient db = null;/// <summary>/// 获得数据库对象/// </summary>/// <returns></returns>public void GetSql(){db = new SqlSugarClient(new ConnectionConfig(){ConnectionString = $"Data Source={FilePath}", // SQLite 连接字符串DbType = DbType.Sqlite, // 指定数据库类型为 SQLiteIsAutoCloseConnection = true, // 自动关闭连接InitKeyType = InitKeyType.Attribute//从实体特性中读取主键自增列信息});}

创建数据库:

  /// <summary>/// 创建数据库/// </summary>public bool CreateBase(){return db.DbMaintenance.CreateDatabase();}

创建数据表:

/// <summary>
/// 创建数据表
/// </summary>
public void CreateTable()
{//两种方法都能创建表//db.CodeFirst.InitTables(typeof(Student));db.CodeFirst.InitTables<Student>();
}//实体与数据库结构一样
public class Student
{//数据是自增需要加上IsIdentity//数据库是主键需要加上IsPrimaryKey//注意:要完全和数据库一致2个属性[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]public int Id { get; set; }public int? SchoolId { get; set; }public string? Name { get; set; }
}

查询一个表中全部数据:

/// <summary>
/// 查询一个表全部数据
/// </summary>
/// <returns></returns>
public List<Student> QueryAll()
{//查询表的所有return db.Queryable<Student>().ToList();
}

 

根据某些条件查询: 

 /// <summary>/// 查询/// </summary>/// <param name="a"></param>/// <param name="b"></param>/// <param name="c"></param>public void Query(out Student a, out List<Student> b, out List<Student> c){// 跟据主键查询:id为参数int id = 2;a = db.Queryable<Student>().InSingle(id);//根据某个数据查询:name为方法参数string name="Tom";b = db.Queryable<Student>().Where(s => s.Name == name).ToList();//模糊查询:方法参数key为关键字string key = "o";c = db.Queryable<Student>().Where(s => s.Name.Contains(key)).ToList();}

插入数据:

 /// <summary>/// 插入数据/// </summary>public void InsertData(){var Tom = new Student(){Id = 2,SchoolId = 3,Name = "Tom"};db.Insertable(Tom).ExecuteCommand();}

 

 更新数据:

 /// <summary>/// 更新数据/// </summary>public void UpdateData(){var Tom = new Student(){Id = 2,SchoolId = 3505,Name = "Tom"};db.Updateable(Tom).ExecuteCommand();}

 

删除数据:

public void DeleteData()
{//根据主键db.Deleteable<Student>().Where(it => it.Id == 1).ExecuteCommand();//根据主键数组db.Deleteable<Student>().In(new int[] { 1, 2 }).ExecuteCommand();
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/47145.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

呼吁 《上海市卫生健康信息技术应用创新白皮书》改正 C# 被认定为A 组件是错误认知

近日,《上海市卫生健康“信息技术应用创新”白皮书》(以下简称《白皮书》)正式发布,介绍了“医疗信创核心应用适配方法、公立医院信息系统及全民健康信息平台信创设计思路”, 其中发现了一个错误的认知,C#/.NET 被认定为A 组件, 具体详见下图:C#/.NET 平台需要被区分为两…

【译】VisualStudio.Extensibility 17.10:用 Diagnostics Explorer 调试您的扩展

VisualStudio. Extensibility 帮助您构建在主 IDE 进程之外运行的扩展,以提高性能和可靠性。它还提供了一个时尚而直观的基于 .NET 8 的 API 和全面且维护良好的文档,可以帮助您开发出色的扩展。想象一下,创建的扩展比以往任何时候都运行得更快、更流畅!如果您最近还没有跟…

一个非侵入式跟踪分析程序

otel-profiling-agent为elastic开源项目,用于对生产环境中的应用程序进行性能分析,帮助开发者理解程序的运行情况,识别瓶颈,优化性能。用于包括CPU和内存使用情况的分析、调用跟踪、性能指标收集等功能。它是一个用于Linux的全系统、跨语言的性能分析器,通过eBPF(Extende…

2024CISCN华东南-WEB-gxngxngxn

WEB 差不多2小时就把web给ak了,没想到华东南强度最大的不是题目,而是场地连续8小时的30多度高温(welcomesubmit-BREAKsubmit-FIX 修复点在upload.php <?php // $path = "./uploads"; error_reporting(0); $path = "./uploads"; $name=$_FILES[myfile…

openai开源 shap-e 根据文本或图像生成 3D 模型,支持导入blender

OpenAI开源Shap-E,部署与使用详细教程。文本图片可生成3D动画和建模文件。介绍 github仓库地址:https://github.com/openai/shap-e 技术原理论文:https://arxiv.org/abs/2305.02463 官方生成效果示例:部署安装 打开colab:https://colab.research.google.com/ 新建一个笔记,…

山东大学项目实训-基于LLM的中文法律文书生成系统(十九)- RAG(5)

项目代码解读 # 官方库 import os import shutil import time from datetime import datetime import logging import pickle from glob import glob from typing import List# 第三方库 # langchain库 from langchain.document_loaders import UnstructuredPowerPointLoader, …

GitHub上狂揽62Kstars的程序员做饭指南

写在前面 作为一名程序员,我们经常会在全球最大的同性交友网站 GitHub上寻找一些优质的开源项目来学习,以提升自己的专业技能。 今天给大家推荐的是一个GitHub上狂揽61K⭐⭐的开源项目。项目名叫做HowToCook,只听名字,大家应该能想到这是一个教程序员怎么做饭的项目。 作为一…

VueX Vue: Could not find a declaration file for module vuex.

vuex4.0.2 webstorm报如下错误 Vue: Could not find a declaration file for module vuex.解决办法 在src目录下新建一个vuex.d.ts 文件内容如下 declare module "vuex" {export * from "vuex/types/index.d.ts";export * from "vuex/types/helpers.d…