1. Create a database MyTestDB and create a table tb_Student under the database. The table field definition is shown in.
2. Create the console application MyTestDB.
3. Create an ADO. NET object data model under the project.
Right-click the project and choose "add"> "new item"> "data"> "ADO. NET"> "input name"> "add ".
Select "generate from database" and click "Next ".
Select database connection and click "Next" (if no database connection exists, create a new connection)
Select a database object, set the model namespace, and click "finish.
View the project file organization structure. There are two more files, App. Config and MyTestDB. edmx.
4. Add test code
Using System; using System. linq; namespace MyTestDB {public class Program {static void Main (string [] args) {AddNewStudent (); // Add a record to the database: wsp 20 1 80 SelectStudent (); // query the record whose classid is 1, and get: wsp 20 1 80 UpdateStudent (); // modify the wsp age to 22 DeleteStudent (); // Delete wsp information }////// Add student records ///Public static void AddNewStudent () {using (MyTestDBEntities myDbEntity = new MyTestDBEntities () {tb_Student tbStudent = new tb_Student (); tbStudent. name = "wsp"; tbStudent. age = 20; tbStudent. classid = 1; tbStudent. score = 80; myDbEntity. addTotb_Student (tbStudent); int count = myDbEntity. saveChanges ();}}////// Delete student records ///Public static void DeleteStudent () {using (MyTestDBEntities myDbEntity = new MyTestDBEntities () {var query = from student in myDbEntity. tb_Student where student. name = "wsp" select student; if (query! = Null) {foreach (var q in query) {myDbEntity. DeleteObject (q);} myDbEntity. SaveChanges ();}}}////// Update student records ///Public static void UpdateStudent () {using (MyTestDBEntities myDbEntity = new MyTestDBEntities () {var query = from student in myDbEntity. tb_Student where student. name = "wsp" select student; if (query! = Null) {foreach (var q in query) {q. age = 22;} myDbEntity. SaveChanges ();}}}////// Query student records ///Public static void SelectStudent () {using (MyTestDBEntities myDbEntity = new MyTestDBEntities () {var query = from student in myDbEntity. tb_Student where student. classid = 1 select student; if (query! = Null) {foreach (var q in query) {Console. writeLine (q. name + "" + q. age + "" + q. classid + "" + q. score );}}}}}}
This instance completes a simple addition, deletion, modification, and query operation, and does not involve the introduction of Ado. Net Entities Framework theoretical knowledge.