In the first article, I have briefly introduced LINQ to you. I also mentioned that LINQ is a feature in C #3.0. However, when you install the LINQ priview, you will find that it provides a lot of vb linq code. According to the current situation, LINQ will be transplanted to. NET Framework. In this article, I will begin to introduce dlinq in more detail. At the same time, we will also introduce practical skills and methods. Before that, you need to install s2008 beta2. You can
Download the http://msdn2.microsoft.com/en-us/vstudio/aa700831.aspx. If your machine is in the English version of vs2005, you also need to install a version of LINQ preview. You can download it at the address below.
Http://www.microsoft.com/downloads/details.aspx? Familyid = 1e902c21-340c-4d13-9f04-70eb5e3dceea & displaylang = en
In the subsequent sections, I will use this version to explain and describe the examples provided in it in detail.
After installing LINQ preview, you can find the example provided by the installer in the installation directory. C # should be in the c: \ Program Files \ LINQ preview \ C # \ samples \ samplequeries directory. Open the project. Because dlinq is mainly used to operate databases, you also need to prepare databases. The database provided by the routine is implemented by. \ sqlexpress. To be honest, this version of database is really difficult to use. I suggest you use SQL2000 or sql2005. you can find a database file named northwnd in the c: \ Program Files \ LINQ preview \ data directory ., Attach it to your server. Because northwnd is an example database attached to SQL Server, you may need to delete the original database before appending it.
You can also go to the http://msdn2.microsoft.com/en-us/bb330936.aspx to download the sample for beta2. The data file path is linqsamples \ samplequeries \ data.
When you attach this database, you may encounter the problem that SQL2000 cannot be attached. You can leave a message for me. I have SQL2000 database backup here. After attaching the database, you need to modify the program file so that the samplequeries program can use the database you just attached. Open the samplequeries program, find the dlinqsamples. CS file in Solution Explorer, open it, and find 24 lines. The following code. Private readonly static string connstring = "attachdbfilename = '" + dbpath + "'; server = '" + sqlserverinstance + "'; Integrated Security = sspi; enlist = false ";
Replace it
Private readonly static string connstring = "Server = yoursever; database = northwind; user = yourname; Password = yourpassword ";
In this way, you can use the database you just attached. Maybe you have to ask why you have to use this new database, and why not use the SQL server. That's because many new things have been added to this new database. Pai_^
After everything is ready, let's take a look at this example. I am not interested in explaining how samplequeries is designed, nor how samplequeries obtains code. Here we only talk about dlinq. Let's take a look at a general interface.
The interface is beautiful and concise. Select the directory tree on the left, and the first main point is the 101 example of LINQ. Previously I also told you that the main function of LINQ is for memory operations. Dlinq is used for database operations. Our focus is still on dlinq ., Select the first in the where contact, and the code section on the right will be displayed. Click RUN sample !, The following shows the SQL statement translated by the program, followed by the returned dataset.
Before officially explaining the dlinq syntax, I may want to briefly explain the relationships between tables and tables in the northwind database.
The relationship between several tables is as follows. Below are some common examples. The first is the customer table, the orders table is the order table, the products table is the product table, and the last is the Order Details table, which is the Order Details table. Because northwind is a database that everyone is familiar with, I will not go into details. In the next chapter, I will still prepare the most critical link for you, the photo and video files.