Ado. NET Entity Data Model detailed Introduction _c# tutorial

Source: Internet
Author: User

Oledbconnection,oracleconnection or SqlConnection this connection, execute the SQL statement directly. Now the connection way to execute the SQL statement has a very big difference, the following first look at a simple list of additions and deletions to check the operation, and then look at the multiple table of the associated query, with parameter inquiries.
First, ado.net entity to change and check the deletion of Tanku
There is a table, that is, an entity user in the project, and for testing convenience, all fields are 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

Copy Code code as follows:

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, modify the content
Copy Code code as follows:

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, inquiry content
(1) Entity Direct inquiry
[/code]
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)
{

}
}
[/code]
(2) ESQL query

Copy Code code as follows:

objectquery<dbdatarecord> result = entities. Createquery<dbdatarecord> ("Select value it from Oracleentities.user as it");

(3) Check according to the condition
Copy Code code as follows:

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

second, related query
For example, there is another table other than the User Foreign Key association.


The query is as follows:

Copy Code code as follows:

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 ();
}
}


These are, of course, the most basic usage, because the use of LINQ and eSQL is described later in the usage of LINQ and ESQL.

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.