C# Tutorial – How to Save and Retrieve Image from Database | FoxLearn

Welcome to the FoxLearn Today we will learn how to save and retrieve image from database First, you need to create a new database and table name is MyPicture We will save images in the MyPictures table and retrieve again when you click the load button on your application We will set the ID column is primary key and auto increment is true Auto increment allows a unique number to be generated when a new record is inserted into a table. You need to add a ListView, a PictureBox and three Buttons to Form1 when you click Open button an OpenDialog show allow you select an image file Your image selected shows in PictureBox control and when you click the save button your data save in the MyPictures table The load button allows you get all data from MyPictures table. We will use Entity framework to save and retrieve data Entity Framework is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects It eliminates the need for most of the data-access code that developers usually need to write Entity Framework allows you to create a model by writing code or using boxes and lines in the Entity Framework Designer Both of these approaches can be used to target an existing database or create a new database Now we will add a file name item in ListView control. It allows show file name data when we retrieve data from database You need to write function convert image to binary. We used to load an image into MemoryStream and convert MemoryStream to byte array Memory streams created with an unsigned byte array provide a non-resizable stream of the data When using a byte array, you can neither append to nor shrink the stream, although you might be able to modify the existing contents depending on the parameters passed into the constructor To save synchronously you need to use async keyword The method runs synchronously until it reaches its first await expression, at which point the method is suspended until the awaited task is complete If the method that the async keyword modifies doesn’t contain an await expression or statement, the method executes synchronously A compiler warning alerts you to any async methods that don’t contain await, because that situation might indicate an error We will add all MyPicture objects into ListView control We used to For-each loop to retrieve a MyPicture object in the list MyPicture object You need to write function convert binary to image We will read all byte arrays and using the memory stream to convert binary to image type You can see the connection string auto create We always use Connection String in forms that we design, but when you design a big software you should set the connection string for once and use it in many forms In this method you use the name of the Connection String instead of the connection string text Every time you want to change the connection string just change the main connection string in the App Config file By this method you don’t need to change all of the forms in your project, just change the Connection String in the App config Now we will select data from MyPictures table and you can see your data in this table Thank you for watching this Video


Leave a Reply

Your email address will not be published. Required fields are marked *