一、准备工具
创建Sqlite数据库test.db,添加表Person:
Create TABLE Person (
Id INTEGER PRIMARY KEY AUTOINCREMENT
UNIQUE
NOT NULL,
Name TEXT NOT NULL,
Age INTEGER NOT NULL
);
二、CYQ.Data开源数据层框架使用示例
1).项目添加引用CYQ.Data.dll和System.Data.SQLite.DLL;
2).app.config添加连接数据库字符串:
<connectionStrings>
<add name="Conn" connectionString="Data Source=F:test.db;failifmissing=false" providerName="System.Data.SQLite"/>
</connectionStrings>
3).使用CYQ.Data辅助工具生成表枚举并添加到项目:
4).C#程序代码:
using System;
using CYQ.Data;
using CYQ.Data.Table;
using CYQ.Entity.main;
namespace ConsoleApplication1
{
public class Program
{
static public void Main(string[] args)
{
using (MAction action = new MAction(TableNames.Person))
{
//新增记录
for (int i = 1; i <= 5; i++)
{
action.Set(Person.Name, "user" + i);
action.Set(Person.Age, i * 10);
action.Insert();
}
//修改记录
action.Set(Person.Name, "mzwu.com");
action.Set(Person.Age, 10);
action.Update("Name='user2'"); //修改Name='user2'的用户
//删除记录
action.Delete("Age=30"); //删除Age=30的用户
//查询记录
MDataTable table = action.Select("Age<50");
for (int i = 0; i < table.Rows.Count; i++)
{
Console.WriteLine("{0},{1}", table.Rows[i]["Name"].Value, table.Rows[i]["Age"].Value);
}
}
Console.ReadKey();
}
}
}