In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to map a class to a two-dimensional table in the database in Linq". The content in the article is simple and clear, and it is easy to learn and understand. Please follow Xiaobian's train of thought to study and learn "how to map a class to a two-dimensional table in the database in Linq".
one。 Create a Linq project
1. Click start | Program | Microsoft Visual Studio 2008 Beta 2 | Microsoft Visual Studio 2008 Beta 2 menu command.
two。 After opening Microsoft Visual Studio, click File | New | Project... Menu command
3. In the New Project dialog box, in Project types, click Other Languages | Visual Basic
4. In the Templates on the right side of the dialog box, select Console Application
5. Fill in "Linq To Sql VB 2" in the Name field to provide a name for your solution
6. Click OK
two。 Add a reference to the System.Data.Linq assembly
1. In Microsoft Visual Studio, click Project | Add Reference... Menu command
two。 Open the Add Reference dialog box and make sure the .NET label is selected
3. Select the System.Data.Linq assembly
4. Click OK
Introduce the namespace System.Data.Linq in Module1.vb, and remember to place it before the module definition:
three。 Map Northwind Customers table
1. Enter the following code in Module1.vb to create an entity class to map the Northwind database (immediately put the Customer class definition before the Module1 definition)
Public Class Customer < Column (IsPrimaryKey:=True) > _ Public CustomerID As String End Class
The Table attribute maps a class to a table in the database. The Column attribute then maps each field to a row in the table. In the Customers table, CustomerID is the primary key; and when the IsPrimaryKey property is set to true, it will be used as the identity of the mapping object. Through a * key, an object will become an entity in the database. In this example, the instances of the Customers class are all entities.
two。 Add the following code to define a mapping of the City attribute:
< Table (Name:= "Customers") > Public Class Customer Private _ City As String < Column (IsPrimaryKey:=True) > Public CustomerID As String < Column (Storage:= "_ City") > Public Property City () As String Get Return _ City End Get Set (ByVal value As String) _ City = value End Set End Property End Class
In the previous step, we saw that fields can be mapped to rows in a data table, but in most scenarios, programmers prefer Property to fields to store this data. Whenever a public mapping Property is defined, the private field used to store the data must be specified using the Storage parameter of the Column property.
3. Add the following code to the Main method, specify access to the Northwind database between the basic database and the code-built data structure, and establish a data connection:
Sub Main () 'uses a standard data connection string. You can change Dim db As New DataContext _ ("Data Source=." sqlexpress; Initial Catalog=Northwind ")' to get a Table object Dim Customers As Table (Of Customer) Customers = db.GetTable (Of Customer) () End Sub for strongly typed queries as needed.
The Customers object (Table (Of T)) provides strongly typed query support. It does not contain all the physical rows of the underlying table, but it provides support for strongly typed queries. At this point, it behaves like a proxy object.
Thank you for your reading, the above is "how to map a class in Linq to the two-dimensional table in the database". After the study of this article, I believe you have a deeper understanding of how to map a class in Linq to the two-dimensional table in the database, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.