C # improve the knowledge of ADO. NET Object Data Model

Source: Internet
Author: User

OleDbConnection, OracleConnection, or SqlConnection directly executes SQL statements. The current method of connecting to execute SQL statements is quite different. Next let's take a look at the simple addition, deletion, modification, and query operations for a single table, and then look at multi-Table Association queries and parameter queries.
1. add, delete, modify, and query a single table using ADO. NET Entity
There is a table that is an entity user in the project. For test convenience, all fields are of the string type.


1. Add new records
Add a record as follows:

[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. Delete content

[Csharp]
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. Modified content

[Csharp]
Using (OracleEntities entities = new OracleEntities ())
{
User user = entities. User. First <User> (a => a. User. Equals ("id "));
 
User. Remarks = "modified content ";
 
Entities. SaveChanges ();
}

Using (OracleEntities entities = new OracleEntities ())
{
User user = entities. User. First <User> (a => a. User. Equals ("id "));

User. Remarks = "modified content ";

Entities. SaveChanges ();
} 4. query content
(1) Direct object query

[Csharp]
Using (OracleEntities entities = new OracleEntities ())
{
ObjectQuery <User> result = entities. User; // query all
 
Foreach (User item in result)
{
 
}
}

Using (OracleEntities entities = new OracleEntities ())
{
ObjectQuery <User> result = entities. User; // query all

Foreach (User item in result)
{

}
} (2) Esql Query
ObjectQuery <DbDataRecord> result = entities. CreateQuery <DbDataRecord> ("select value it from OracleEntities. User as it ");
(3) query by conditions
Var result = entities. User. Where (o => o. id. Equals ("id "));
2. join query
For example, another table Other is associated with the User foreign key.


The query is as follows:

[Csharp]
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.2cto.com

ObjectQuery <DbDataRecord> query = entities. CreateQuery <DbDataRecord> (esql );

Foreach (DbDataRecord r in query)
{
String ss = r ["detail"]. ToString ();
}
} Of course, these are the most basic usage, because the usage of Linq and ESql is described in detail later in the usage of Linq and ESql.


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.