OleDbConnection,OracleConnection 或者SqlConnection这种连接,直接执行sql语句。现在的连接方式执行sql语句有了很大的不同,下面先看看简单的单表的增删改查操作,然后再看多表的关联查询,带参数查询等。
一、ADO.NET Entity对单表的增删改查
有一个表,即在工程中是一个实体user,为了测试方便,所有字段为string型。
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.User.Equals("id"));
user.Remarks = "修改了内容";
entities.SaveChanges();
}
using (OracleEntities entities = new OracleEntities())
{
ObjectQuery<User> result = entities.User;//查询所有
foreach (User item in result)
{
}
}
[/code]
(2)Esql查询
进行查询如下:
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.VeVB.COm
ObjectQuery<DbDataRecord> query = entities.CreateQuery<DbDataRecord>(esql);
foreach (DbDataRecord r in query)
{
string ss = r["detail"].ToString();
}
}
新闻热点
疑难解答