柚子快報(bào)邀請(qǐng)碼778899分享:C#與Sqlite數(shù)據(jù)庫(kù)
柚子快報(bào)邀請(qǐng)碼778899分享:C#與Sqlite數(shù)據(jù)庫(kù)
1,一般的訪問方式。
1.1,連接語(yǔ)句。
//sqlite 連接,支持相對(duì)位置,也支持絕對(duì)位置
Data Source=../../Database/cater.db
// 連接數(shù)據(jù)庫(kù),F(xiàn)ailIfMissing=false時(shí)若文件不存在會(huì)自動(dòng)創(chuàng)建
string connStr ="DataSource=test.db;Version=3;Pooling=true;FailIfMissing=false;";
1.2,配置文件設(shè)置。
//需在配置文件中進(jìn)行如下配置否則報(bào)錯(cuò)
?1.3,常用語(yǔ)法。
//語(yǔ)法:
select * from AlarmHistory
insert into alarmhistory (AlarmDetails,starttime) values ('abc',getdate())
//獲取當(dāng)前時(shí)間
select datetime('now')
SELECT datetime('now', 'localtime');
select CURRENT_TIMESTAMP
//插入當(dāng)前時(shí)間
insert into alarmhistory (alarmdetails,starttime) values('',datetime('now','localtime'))
//查找為null的數(shù)據(jù)
select * from alarmhistory where StartTime is null
//修改表格序號(hào)
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'
//獲取和設(shè)置時(shí)間,時(shí)間格式只支持類似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數(shù)據(jù)訪問。
2.1,安裝Nuget包
這部分比較簡(jiǎn)單,直接Nuget包中下載即可
System.Data.SQLiteSystem.Data.SQLite.EF6System.Data.SQLite.LINQSQLite.CodeFirst
2.2,配置文件需要進(jìn)行如下修改。
2.3,應(yīng)用。
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
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
}
[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,效果。
數(shù)據(jù)庫(kù)文件數(shù)據(jù)。
查詢結(jié)果。
柚子快報(bào)邀請(qǐng)碼778899分享:C#與Sqlite數(shù)據(jù)庫(kù)
推薦鏈接
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。