Escolar Documentos
Profissional Documentos
Cultura Documentos
http://msdn.microsoft.com/en-us/library/bb386940
Note
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings. This walkthrough was written by using Visual C# Development Settings.
Prerequisites
This walkthrough uses a dedicated folder ("c:\linqtest5") to hold files. Create this folder before you begin the walkthrough. This walkthrough requires the Northwind sample database. If you do not have this database on your development computer, you can download it from the Microsoft download site. For instructions, see Downloading Sample Databases (LINQ to SQL). After you have downloaded the database, copy the file to the c:\linqtest5 folder.
Overview
This walkthrough consists of six main tasks: Creating a LINQ to SQL solution in Visual Studio. Mapping a class to a database table. Designating properties on the class to represent database columns. Specifying the connection to the Northwind database. Creating a simple query to run against the database. Executing the query and observing the results.
1 of 6
5/25/2012 11:39 AM
http://msdn.microsoft.com/en-us/library/bb386940
2. In the Project types pane of the New Project dialog box, click Visual C#. 3. In the Templates pane, click Console Application. 4. In the Name box, type LinqConsoleApp. 5. In the Location box, verify where you want to store your project files. 6. Click OK.
To add System.Data.Linq
1. In Solution Explorer, right-click References, and then click Add Reference. 2. In the Add Reference dialog box, click .NET, click the System.Data.Linq assembly, and then click OK. The assembly is added to the project. 3. Add the following directives at the top of Program.cs:
2 of 6
5/25/2012 11:39 AM
Walkthrough: Simple Object Model and Query (C#) (LINQ to SQL) In this step, you accomplish several tasks.
http://msdn.microsoft.com/en-us/library/bb386940
You use the ColumnAttribute attribute to designate CustomerID and City properties on the entity class as representing columns in the database table. You designate the CustomerID property as representing a primary key column in the database. You designate _CustomerID and _City fields for private storage. LINQ to SQL can then store and retrieve values directly, instead of using public accessors that might include business logic.
private string _CustomerID; [Column(IsPrimaryKey=true, Storage="_CustomerID")] public string CustomerID { get { return this._CustomerID; } set { this._CustomerID = value; } } private string _City; [Column(Storage="_City")] public string City { get { return this._City; } set { this._City=value; } }
3 of 6
5/25/2012 11:39 AM
http://msdn.microsoft.com/en-us/library/bb386940
You also declare a Table<Customer> to act as the logical, typed table for your queries against the Customers table in the database. You will create and execute these queries in later steps.
// Use a connection string. DataContext db = new DataContext (@"c:\linqtest5\northwnd.mdf"); // Get a typed table to run queries. Table<Customer> Customers = db.GetTable<Customer>();
// Attach the log to show generated SQL. db.Log = Console.Out; // Query for customers in London. IQueryable<Customer> custQuery = from cust in Customers where cust.City == "London" select cust;
4 of 6
5/25/2012 11:39 AM
http://msdn.microsoft.com/en-us/library/bb386940
foreach (Customer cust in custQuery) { Console.WriteLine("ID={0}, City={1}", cust.CustomerID, cust.City); } // Prevent console window from closing. Console.ReadLine();
Note
If your application generates a run-time error, see the Troubleshooting section of Learning by Walkthroughs (LINQ to SQL). The query results in the console window should appear as follows:
ID=AROUT, City=London ID=BSBEV, City=London ID=CONSH, City=London ID=EASTC, City=London ID=NORTS, City=London ID=SEVES, City=London
3. Press Enter in the console window to close the application.
Next Steps
The Walkthrough: Querying Across Relationships (C#) (LINQ to SQL) topic continues where this walkthrough ends. The Query Across Relationships walkthrough demonstrates how LINQ to SQL can query across tables, similar to joins in a relational database. If you want to do the Query Across Relationships walkthrough, make sure to save the solution for the walkthrough you have just completed, which is a prerequisite.
See Also
Concepts Learning by Walkthroughs (LINQ to SQL)
Yes
No
5 of 6
5/25/2012 11:39 AM
http://msdn.microsoft.com/en-us/library/bb386940
6 of 6
5/25/2012 11:39 AM