Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ADO.NET Entity Framework to build a data access layer

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use ADO.NET Entity Framework to build a data access layer". In daily operation, I believe many people have doubts about how to use ADO.NET Entity Framework to build a data access layer. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use ADO.NET Entity Framework to build a data access layer". Next, please follow the editor to study!

With the development of ADO.NET for a long time, many users know ADO.NET Entity Framework very well. Here, we mainly introduce the steps of using ADO.NET Entity Framework to access pictures in the database. The pictures in the website can be saved to the file system, that is, a special directory for the storage of pictures, such storage coupled with the image path information saved in the database, I believe that many websites choose this way. Because the storage and management information are separated, the advantage of this collocation is that it will not have much impact on the performance of the database, but it is precisely because the directory structure of this separate website can not be changed easily. in addition, the backup of image data requires the database and file directories to be synchronized. Another way to store and manage pictures is to store them in the database, which is quite suitable if the number of pictures on the site is not too many and the pictures are not too large. In this article, we look at how to use ADO.NET Entity Framework to build a data access layer to store and retrieve pictures from a database.

Preliminary work: create data table + build data access layer

The first step is to create a data table

A field is required to save the image in the table. The type is set to image, and the code is as follows:

CREATE TABLE [dbo]. [images] ([id] [int] IDENTITY (1) NOT NULL, [imagefile] [image] NOT NULL)

Second, build the data access layer

The insertion of this type of data is different from the general basic SQL type, but programmers working on the .net 3.5 platform do not need to think too much about the specific code to access the database. We can choose ADO.NET Entity Framework and Linq to SQL. Here we take the former as an example. The method is briefly introduced as follows:

New in the project, select ADO.NET Entity Data Model, establish a connection to the database according to the wizard, and select the table images just established in the database according to the wizard. After the wizard ends, an edmx type file will be added to the project. The automatically generated entity class diagram will open in the default interface. All the tables we select in the wizard will correspond to an entity class with the same default name as the data table. The entity class corresponding to images here is also images, but if it is used by default, it will cause ambiguity. We need to modify it manually. Click on the images class to change the Name attribute to image,Entity Set Name in the properties dialog box. You can change it to imageSet or leave images unchanged to represent the image collection. After that, the corresponding class name in the entity class diagram also becomes image. This assumes that the database where the images are stored is named XXX, then the generated management class, which we use most frequently later, is called XXXEntities.

Save the picture to the SQL server database

First declare a variable of type image. Call it img.

Image img = new image ()

Then create a XXXEntities object

XXXEntities XXX = new XXXEntities ()

(note the using namespace and add references to System.Data.Entity)

Add a FileUpload control to the asp.net web page, which provides an attribute PostedFile that provides an input and output stream for the server, which allows us to read the selected image into the server's memory. The code is as follows:

System.IO.Stream stream = FileUpload1.PostedFile.InputStream; byte [] buffer = new byte [stream.Length]; stream.Read (buffer, 0, (int) stream.Length); stream.Close (); img.imagefile = buffer

The next step is to store the byte array representing the picture in the database, as follows:

XXX.AddToimageSet (t); XXX.SaveChanges ()

In this way, the operation of storing the picture in the database is complete.

At this point, the study on "how to use ADO.NET Entity Framework to build a data access layer" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report