One of the simplest examples of querying SQL databases using the entity Framework

Source: Internet
Author: User
Tags management studio sql server management sql server management studio

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.

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.