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