一、sqlite的库表结构
class="p0" style="margin-bottom: 0pt; margin-top: 0pt;">即:班级、 学生、分数
CREATE TABLE [Student] (
[ID] INTEGER NOT NULL PRIMARY KEY ON CONFLICT
ROLLBACK,
[Name] VARCHAR(
50)
NOT NULL,
[Age] INT NOT NULL,
[BirthDay] DATETIME NOT NULL,
[Sex] BOOLEAN
NOT NULL,
[Class_ID] INTEGER NOT NULL REFERENCES [Class](
[ID]));
CREATE TABLE [Class] (
[ID] INTEGER NOT NULL PRIMARY KEY ON CONFLICT
ROLLBACK,
[ClassName] VARCHAR(
50)
NOT NULL,
[ClassLevel] INT NOT NULL);
CREATE TABLE [Score] (
[ID] INTEGER NOT NULL PRIMARY KEY ON CONFLICT
ROLLBACK,
[Score] INT NOT NULL,
[Student_ID] INTEGER NOT NULL REFERENCES [Student](
[ID]));
二、使用Sqlite的步骤
1)配置连接
2)生成实体层
3)引用实体层
4)配置项目的配置文件,使用
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=Moon_Sqlite.db;UseUTF16Encoding=True;" providerName="Moon.Orm,Moon.Orm.Sqlite" />
</connectionStrings>
</configuration>
using System;
using Moon.Orm;
using sqlite;
using Entity;
using Moon.Orm.Util;
namespace TestMoon
{
class Program
{
public static void Main(
string[] args)
{
//添加数据
using (
var db=Db.
CreateDefaultDb()) {
//开启事务功能
db.TransactionEnabled=
true;
//清空数据表
/*
db.Remove<ScoreSet>();
db.Remove<StudentSet>();
db.Remove<ClassSet>();
*/
//添加数据
Class cl=
new Class();
cl.ClassName=
"班级"+DateTime.Now.
ToString();
cl.ClassLevel=
1;
db.
Add(cl);
Console.
WriteLine(
"添加[班级]成功,ID={0}",cl.ID);
Student stu=
new Student();
stu.Age=
12;
stu.BirthDay=DateTime.Now;
stu.ClassID=cl.ID;
stu.Name=
"张三"+DateTime.Now;
stu.Sex=
true;
db.
Add(stu);
Console.
WriteLine(
"添加[学生]成功,ID={0}",stu.ID);
Score sco=
new Score();
sco.Score_=
98;
sco.StudentID=stu.ID;
db.
Add(sco);
Console.
WriteLine(
"添加[分数]成功,ID={0}",stu.ID);
//跟新数据
Score update=
new Score();
update.Score_=
100;
update.WhereExpression=ScoreSet.ID.
Equal(sco.ID);
db.
Update(update);
}
//查询数据
using (
var db=Db.
CreateDefaultDb()) {
//连接查询
var mqlJoin=ScoreSet.
SelectAll()
.
InnerJoin(StudentSet.
Select(StudentSet.Name))
.
InnerJoin(ClassSet.
Select(ClassSet.ID.
AS(
"ClassID"),ClassSet.ClassName))
.
ON(ScoreSet.StudentID.
Equal(StudentSet.ID)
.
And(StudentSet.ClassID.
Equal(ClassSet.ID) )
);
//查看当前sql
var sql=mqlJoin.
ToDebugSQL();
var list=db.
GetDictionaryList(mqlJoin);
//展示数据
DictionaryListHelper.
ShowDictionaryListInConsole(list);
//让我们无需实体类
string sql2=mqlJoin.
ToParametersSQL();
dynamic dlist=db.
GetDynamicList(sql2,
"自定义类名");
foreach (
dynamic entity
in dlist) {
Console.
WriteLine(entity.ID+
" "+entity.Score+
" "+entity.Student_ID+
" "+entity.Name+
" "+entity.ClassID+
" "+entity.ClassName);
}
//获取实体集
var entityList=db.GetEntities<Score>(ScoreSet.
SelectAll().
Where(ScoreSet.ID.
BiggerThan(
0)));
}
Console.
Write(
"Press any key to continue . . . ");
Console.
ReadKey(
true);
}
}
}
运行效果:
代码生成器、moon最新版本、此运行demo(可直接运行)
下载地址:http://pan.baidu.com/share/link?shareid=393699976&uk=353229276
既然下载,请支持
Moon.Orm,性能优异、使用便捷、多数据库数据源支持、2.0原生支持、智能感知.免费且提供代码生成器.
请加入第二个群
225656797 Moon.ORM5.0总目录
Moon.ORM5.0社区版代码生成器
Moon.ORM5.0的配置方法
Moon.Orm帮助文档(跟踪发布),Moon在线问答、咨询
Moon.Orm 5.0 (MQL版) 版本维护及下载(跟踪发布)