The 1.ado.net 3.5 Entity framework is published with the. NET Framework 3.5, confirming that the development environment version is greater than or equal to version 3.5
2. Confirm that the ADO 3.5 Entity Framework and the ADO 3.5 Entity Framework Tools have been installed: Right-click the created WinForm or WPF program, select add| New Item to see if the Templates list box has the ADO. Data Model, if not, download and install it at this URL: http://www.microsoft.com/en-us/download/ Default.aspx
3. Create an example SQL database. Run the file that created the initial database in SQL Server Management Studio, and change the database file path in the SQL file to your own path: Http://files.cnblogs.com/andyzeng/CSE_DEPT.zip
4. Create a WPF application, WinForm can also. and add a ListBox, a button, such as:
5. Create the Entity Data Model:
Right-click on the project created in step 4th and select "Add| New Item, in the list box, select the ADO Entity Data Model.
Get the following EDM Wizard, select Generate from Database, click "Next"
6. For example, follow the steps to select the previously generated database.
7. When the database is selected, a connection string is generated, specifying a name for the connection: cse_deptentities
This name will be used when creating a database connection, click Next
8. Select Create Mapping Relationship table, view, stored procedure, etc., click "Finish"
9. Generate the following mappings and add references and EDM files to the project.
10. Select a table right-click-〉mapping details to see the detailed field mappings.
11. Before the preparation is complete, the following code to write data.
There are three entity classes used: Entityconnection,entitycommand, EntityDataReader
Private voidButton1_Click (Objectsender, RoutedEventArgs e) {EntityConnection conn=NewEntityConnection ("name=cse_deptentities"); Conn. Open (); EntityCommand cmd=Conn. CreateCommand (); Cmd.commandtext="SELECT fname.faculty_name from cse_deptentities.faculty as fname"; EntityDataReader Rd=cmd. ExecuteReader (commandbehavior.sequentialaccess); This. Lsb_faculty. Items.clear (); while(Rd. Read ()) { This. lsb_faculty. Items.Add (rd["Faculty_name"]); }
Conn. Close ();
}
The results of the operation are as follows:
Complete:
Andy Zeng
Reference: "Practical Database Programming with Visual c#.net"
Any form of reprint is welcome, but please be sure to indicate the source.