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.