How to Save an Image in the MySQL Database Using C#
Submitted by janobe on Monday, November 12, 2018 - 21:31.
This one is the continuation of my previous tutorial which is How to Load an Image from the Local Directory into the PictureBox in C#. This tutorial will show you how to save an image in the mysql database using c#. In this method you will be able to save any image that you like that is available from the Local Directory to MySQL Database. See the procedure below.
For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.
Creating Database
Create a database and named it “dbsaveimage”. Create a table in the database that you have createdCreating Application
Step 1
Open the file How to Load an Image from the Local Directory into the PictureBox in C#.Step 2
Add a Button in the Form.
Step 3
Double click the “Save Image” button to fire theclick event handler
of it and do the following codes for saving an image in the MySQL database.
- string strconnection = "Server=localhost;User Id=root;password=janobe;database=dbsaveimage;sslMode=none;";
- try
- {
- con.Open();
- {
- string path = openFileDialog1.FileName;
- byte[] imageData = File.ReadAllBytes(@path);
- cmd.Parameters.AddWithValue("@IM", imageData);
- cmd.ExecuteNonQuery();
- }
- MessageBox.Show("Image has been saved in the database.");
- }
- catch(Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- finally
- {
- con.Close();
- }
Add new comment
- 1316 views