为什么选择Dos.ORM(原Hxj.Data)?
[原地址](http://www.cnblogs.com/BookCode/p/5292859.html)
上手简单,0学习成本。使用方便,按照sql书写习惯编写C#.NET代码。功能强大
高性能(与Dapper媲美,接近手写Sql)
体积小(不到150kb,仅一个dll)
完美支持Sql Server(2000至最新版),MySql,Oracle,Access,Sqlite等数据库
支持大量Lambda表达式写法,国产ORM支持度最高,开源中国ORM排行前三
不需要像NHibernate的XML配置,不需要像EF的各种数据库连接驱动
众多成熟企业软件、互联网项目已应用此框架
遵循MIT开源协议,除不允许改名,其它随意定制修改
Dos团队持续更新升级,任何Bug反馈都会立即得到解决
1.首先·在 App.config文件中配置连接数据库字符串。或者在程序中指定
<connectionStrings>
<add name="School" connectionString="Data Source=.;Initial Catalog=School;User
ID=sa;Pwd=123;"></add>
</connectionStrings>
直接上示例代码不废话
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hxj.Data;
using Hxj.Data.Sqlite;
using System.Data;
namespace cn.School
{
class Test
{
static void Main(string[] args)
{
Catalog=School;User ID=sa;Pwd=
123;
"></add>
DbSession dbs2 = new DbSession(DatabaseType.SqlServer,
"Data
Source=.;Initial Catalog=School;User ID=sa;Pwd=
123;
");
assistmethod();
}
public static void TestSelDB()
{
Student st = DbSession.Default.From<Student>()
Products._.ProductName.GroupBy)
Products._.SupplierID)
LeftJoin RightJoin 同理
Products._.UnitPrice >
1)
|| Products._.CategoryID == 2)//设置条件
ProductName包含”apple”并
且UnitPrice>
1 或者CategoryID =
2
().Select(Products._.ProductID))
.ToFirst();
DbSession.Default.From<Student>()
.Select(Student._.Stu_ID, Student._.Stu_name)
.ToDataTable();
DbSession.Default.From<Student>()
.Select(Student._.Stu_ID, Student._.Stu_name.As(
"pname"))
.ToDataTable();
DataTable dt = DbSession.Default.From<Student>
().OrderBy(Student._.Stu_ID.Desc).ToDataTable();
}
public static void demoSelet()
{
DbSession.Default.From<Student>().Where(Student._.Stu_ID.Contain(
41500));
DbSession.Default.From<Student>().Where(Student._.Stu_ID.BeginWith(
41500));
DbSession.Default.From<Student>().Where(Student._.Stu_ID.EndWith(
41500));
DbSession.Default.From<Student>()
.Where(Student._.Stu_ID.SelectIn(
1,
2,
3))
.ToList();
DbSession.Default.From<Student>()
.Where(Student._.Stu_ID.SelectNotIn<int>(
1,
2,
3))
.ToList();
DbSession.Default.From<Student>()
.Where(Student._.Stu_ID
.SubQueryEqual(DbSession.Default.From<Student>().Where(Student._.Stu_ID
==
"Produce").Select(Student._.Stu_ID).Top(
1)))
.ToList();
}
public static void likeSel()
{
DbSession.Default.From<Student>()
.InnerJoin<Gread>(Student._.gr_id == Gread._.gr_id)
.ToDataTable();
DbSession.Default.From<Student>()
.LeftJoin<Gread>(Student._.gr_id == Gread._.gr_id)
.Where(Student._.gr_id ==
1)
.ToDataTable();
DbSession.Default.From<Student>().Where(Student._.gr_id ==
4522)
.UnionAll(DbSession.Default.From<Gread>().Where(Gread._.gr_id ==
1))
.ToList();
}
public static void addTestDB()
{
Student stu = new Student();
stu.Stu_name =
"小黑";
stu.stu_phon =
"1254555";
stu.stu_Sex =
"男";
stu.stu_Age =
25;
stu.gr_id =
1;
stu.Attach();
int result = DbSession.Default.Insert<Student>(stu);
List<Student> listStu = DbSession.Default.From<Student>
().Where(Student._.Stu_ID == result).ToList();
}
public static void Updata()
{
Student stu = DbSession.Default.From<Student>
().Where(Student._.Stu_ID.Contain(
41500)).ToFirst();
stu.Attach();
stu.Stu_name =
"王五";
List<ModifyField> list = stu.GetModifyFields();
int num = DbSession.Default.Update<Student>(stu);
Student._.Stu_ID ==
41501);
==
41501);
}
public static void DelData()
{
int returnValue = DbSession.Default.Delete<Student>(Student._.Stu_ID ==
41504);
}
public static void sqlFrom()
{
DataTable dt = DbSession.Default.FromSql(
"select * from
Student
").ToDataTable();
stu_id=id
").AddInParameter("id
", DbType.Int32, 41500).ToDataTable();
stu_id=id or stu_name=name
")
}
public static void ProcDemo()
{
DataTable dt = DbSession.Default.FromProc(
"ProcName").ToDataTable();
DataTable dt1 = DbSession.Default.FromProc(
"ProcName")
.AddInParameter(
"parameterName", DbType.DateTime,
"1995-01-01")
.AddInParameter(
"parameterName1", DbType.DateTime,
"1996-12-01")
.ToDataTable();
ProcSection proc = DbSession.Default.FromProc(
"testoutstore")
.AddInParameter(
"in1", System.Data.DbType.Int32,
1)
.AddOutParameter(
"out2", System.Data.DbType.String,
100);
proc.ExecuteNonQuery();
Dictionary<string, object> returnValue = proc.GetReturnValues();
foreach (KeyValuePair<string, object> kv in returnValue)
{
Console.WriteLine(
"ParameterName:" + kv.Key +
" ;ReturnValue:" +
Convert.ToString(kv.Value));
}
}
public static void assistmethod()
{
int? sum = (int?)DbSession.Default.Sum<Student>(Student._.gr_id,
Student._.Stu_name ==
"小黑");
DbSession.Default.Avg<Student>(Student._.Stu_ID, Student._.Stu_ID ==
2);
DbSession.Default.Count<Student>(Student._.Stu_ID, Student._.Stu_ID ==
2);
DbSession.Default.Max<Student>(Student._.Stu_ID, Student._.Stu_ID ==
2);
DbSession.Default.Min<Student>(Student._.Stu_ID, Student._.Stu_ID ==
2);
}
public static void TestTrans()
{
DbTrans trans = DbSession.Default.BeginTransaction();
try
{
DbSession.Default.Update<Student>(Student._.Stu_name,
"apple",
Student._.Stu_ID ==
1, trans);
DbSession.Default.Update<Student>(Student._.Stu_name,
"egg",
Student._.Stu_ID ==
2, trans);
trans.Commit();
}
catch
{
trans.Rollback();
}
finally
{
trans.Close();
}
DbTrans trans1 = DbSession.Default.BeginTransaction();
DbSession.Default.FromProc(
"ProcName").SetDbTransaction(trans);
}
public static void batingTest()
{
using (DbBatch batch = DbSession.Default.BeginBatchConnection())
{
batch.Update<Student>(Student._.Stu_name,
"apple", Student._.Stu_ID ==
1);
batch.Update<Student>(Student._.Stu_name,
"pear", Student._.Stu_ID ==
2);
batch.Update<Student>(Student._.Stu_name,
"orange", Student._.Stu_ID ==
3);
}
}
public static void SetCacheTimeOutDemo() {
DbSession.Default.From<Student>().Where(Student._.Stu_ID ==
1).SetCacheTimeOut(
180).ToFirst();
}
}
}