Winforms: How to upload an image to a SQL Server database using C #

I want to upload images to a SQL Server database. I have two buttons, one picture. Using the browse button, I can select a file from disk, and it is displayed in the image window.

The problem is that I cannot save the image from the picturebox to the database.

Please help me with the code. Appreciate it.

+4
source share
2 answers

You can save the image directly from its path (you already have it).

Try the following:

byte[] img = File.ReadAllBytes("your image path"); mySqlCommand = "INSERT INTO MyTable(Image) VALUES(@Image)";//mySqlCommand is a SqlCommand, and @Image is a parameter mySqlCommand.Parameters.AddWithValue("@Image", img); mySqlCommand.ExecuteNonQuery(); 
+5
source

Source: https://habr.com/ru/post/1303155/


All Articles