c oracle实体模型,ADO.NET实体数据模型详细介绍

OleDbConnection,OracleConnection 或者SqlConnection这种连接,直接执行sql语句。现在的连接方式执行sql语句有了很大的不同,下面先看看简单的单表的增删改查操作,然后再看多表的关联查询,带参数查询等。

一、ADO.NET Entity对单表的增删改查

有一个表,即在工程中是一个实体user,为了测试方便,所有字段为string型。

9c78c220573186799247b89bee5c574d.png

1、增加新记录

增加一条记录如下:

[csharp]

using (OracleEntities entities = new OracleEntities())

{

User uer = User.CreateUser("id", "name", "age", "1");

entities.User.AddObject(user);

entities.SaveChanges();

using (OracleEntities entities = new OracleEntities())

{

User uer = User.CreateUser("id", "name", "age", "1");

entities.User.AddObject(user);

entities.SaveChanges();

}

2、删除内容

using (OracleEntities entities = new OracleEntities())

{

User user = entities.User.First(a => a.ID.Equals("id"));

entities.DeleteObject(user);

entities.SaveChanges();

}

using (OracleEntities entities = new OracleEntities())

{

User user = entities.User.First(a => a.ID.Equals("id"));

entities.DeleteObject(user);

entities.SaveChanges();

}

3、修改内容

using (OracleEntities entities = new OracleEntities())

{

User user = entities.User.First(a => a.User.Equals("id"));

user.Remarks = "修改了内容";

entities.SaveChanges();

}

using (OracleEntities entities = new OracleEntities())

{

User user = entities.User.First(a => a.User.Equals("id"));

user.Remarks = "修改了内容";

entities.SaveChanges();

}

4、查询内容

(1)实体直接查询

[/code]

using (OracleEntities entities = new OracleEntities())

{

ObjectQuery result = entities.User;//查询所有

foreach (User item in result)

{

}

}

using (OracleEntities entities = new OracleEntities())

{

ObjectQuery result = entities.User;//查询所有

foreach (User item in result)

{

}

}

[/code]

(2)Esql查询

ObjectQuery result = entities.CreateQuery("select value it  from  OracleEntities.User as it ");

(3)按条件查

var result = entities.User.Where(o => o.id.Equals("id"));

二、关联查询比如还有个表Other与User外键关联。

b8643ea9a7450968a926dc57d57541ff.png

进行查询如下:

using (OracleEntities entities = new OracleEntities())

{

string esql = "SELECT b.detail FROM OracleEntities.User as a,OracleEntities.Other as b where a.otherid = b.id and a.id='id'";

ObjectQuery query = entities.CreateQuery(esql);

foreach (DbDataRecord r in query)

{

string ss = r["detail"].ToString();

}

}

using (OracleEntities entities = new OracleEntities())

{

string esql = "SELECT b.detail FROM OracleEntities.User as a,OracleEntities.Other as b where a.otherid = b.id and a.id='id'";  www.jb51.net

ObjectQuery query = entities.CreateQuery(esql);

foreach (DbDataRecord r in query)

{

string ss = r["detail"].ToString();

}

}

当然这些都是最基本的用法,因为使用的Linq和ESql,在后面再Linq和ESql的用法里再详细说明。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值