Welcome to FoxLearn
In this video, I'll show you how to insert and retrieve images from sql server database.
After you finish creating a new Windows Forms project, you need to create a local database
A local database is a database just for your local application
It uses SDF data file, and no need to install a server to access SDF database
Next, You need to connect to your local database, then you can create a simple table that will allow you to save the images to your sql database
And don't forget to set your ID as primary key and auto increment
After you finish designing the database, you need to open your form designer, and then drag the Button, PictureBox, Label,
TextBox and DataGridView controls from the Visual Studio toolbox into your form designer
You can design simple UI allows you to select an image, then insert and retrieve images from sql database
You can use DataGridView to display images data, then convert data to image type, and display your image to the PictureBox control
You can add a binding source to your DataGridView to help you automatically add the connection string to the app.config file
You should store your connection string in the app.config file to make it easy for you to change the connection string without modifying your source code
You can create an Insert method allows you to insert an image into sql database
You can use the SqlConnection class to make a connection to your database
And don't forget to add a reference to the System.Configuration library to your project
This library helps you to read your connection string from the app.config file
You should check your sql connection status
If the connection is closed you need to open it
The SqlConnection is opened and set as the Connection for the SqlCommand
Next, use the SqlCommand class to execute your SQL query
You can easily add query parameters to SqlCommand using the AddWithValue method
It represents a Transact-SQL statement or stored procedure to execute against a SQL Server database
You can create a LoadData method that allows you to retrieve data from your sql database, then display your data to the DataGridView
You can create a new DataTable to help you store the data returned from the sql query
The DataTable is a central object in the ADO.NET library
Other objects that use the DataTable include the DataSet and the DataView
You can use SqlDataAdapter class to read the data and fill in your DataTable object, and then bind it to your DataGridView
The SqlDataAdapter, serves as a bridge between a DataSet and SQL Server for retrieving and saving data
The SqlDataAdapter provides this bridge by mapping Fill, which changes the data in the DataSet to match the data in the data source, and Update,
which changes the data in the data source to match the data in the DataSet, using the appropriate Transact-SQL statements against the data source
Finally, You need to create methods that allows you to convert the image into a byte array, and vice versa
Because the image you save to the sql database as byte array
You can use the OpenFileDialog class to help you open a Windows dialog box for browsing and selecting files
Next, We will display the image that we have selected into the PictureBox, then save your image to the sql database
You can easily find your local database in the debug directory
You should copy your local database to another directory, because each time you rebuild your project all data in the debug folder will be deleted
Thank you for watching this video and don't forget to subscribe to my channel