欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-06 11:27:52
oledbconnection,oracleconnection 或者sqlconnection这种连接,直接执行sql语句。现在的连接方式执行sql语句有了很大的不同,下...

oledbconnection,oracleconnection 或者sqlconnection这种连接,直接执行sql语句。现在的连接方式执行sql语句有了很大的不同,下面先看看简单的单表的增删改查操作,然后再看多表的关联查询,带参数查询等。
一、ado.net entity对单表的增删改查
有一个表,即在工程中是一个实体user,为了测试方便,所有字段为string型。

ADO.NET实体数据模型详细介绍
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<user>(a => a.id.equals("id")); 

                entities.deleteobject(user); 

                entities.savechanges(); 

using (oracleentities entities = new oracleentities())
 {
                user user = entities.user.first<user>(a => a.id.equals("id"));

                entities.deleteobject(user);

                entities.savechanges();
}


3、修改内容
复制代码 代码如下:
 
using (oracleentities entities = new oracleentities()) 
 { 
                user user = entities.user.first<user>(a => a.user.equals("id")); 

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

                entities.savechanges(); 

using (oracleentities entities = new oracleentities())
 {
                user user = entities.user.first<user>(a => a.user.equals("id"));

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

                entities.savechanges();
}


4、查询内容
(1)实体直接查询
[/code] 
using (oracleentities entities = new oracleentities()) 
 { 
                objectquery<user> result = entities.user;//查询所有  

                foreach (user item in result) 
                { 

                } 

using (oracleentities entities = new oracleentities())
 {
                objectquery<user> result = entities.user;//查询所有

                foreach (user item in result)
                {

                }
}
[/code]
(2)esql查询

复制代码 代码如下:

objectquery<dbdatarecord> result = entities.createquery<dbdatarecord>("select value it  from  oracleentities.user as it ");

(3)按条件查
复制代码 代码如下:

var result = entities.user.where(o => o.id.equals("id"));

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

ADO.NET实体数据模型详细介绍
进行查询如下:

复制代码 代码如下:
 
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<dbdatarecord> query = entities.createquery<dbdatarecord>(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<dbdatarecord> query = entities.createquery<dbdatarecord>(esql);

                foreach (dbdatarecord r in query)
                {
                    string ss = r["detail"].tostring();
                }
 }


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