• 【转】ado实体数据模型增删改查基本用法


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


    1、增加新记录
    增加一条记录如下:

    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.User.SaveChanges(); 
    } 
     
    
    using (OracleEntities entities = new OracleEntities())
     {
                    User user = entities.User.First<User>(a => a.ID.Equals("id"));
    
                    entities.DeleteObject(user);
    
                    entities.User.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)实体直接查询

    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)
                    {
    
                    }
    }


    (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外键关联。


    进行查询如下:

    代码如下:
     
    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();
                    }
     }

    转自:http://www.jb51.net/article/31982.htm

  • 相关阅读:
    Delphi 2010下安装IOComp
    为自定义控件的某个属性添加像Winform控件中属性的注释
    C#WinForm仿qq窗体拖到windows窗体边上时,自动隐藏C#WinForm
    给图片添加水印,解决GIF添加水印的问题(无法从带有索引像素格式的图像创建 Graphics 对象)
    怎样把Image数据放入数据库
    取消IE“已限制此网页运行可以访问计算机的脚本
    sql 二进制文件的导入导出
    [转载]数据结构笔试题基础
    [转载]搜索算法(含基本搜索算法与深度搜索与广度搜索算法等思想)
    [转载]面试笔试总结
  • 原文地址:https://www.cnblogs.com/baimangguo/p/6639988.html
Copyright © 2020-2023  润新知