0

I am learning C# including interacting with SQL Server databases. I am stuck however, on adding PictureBox images programmatically to a SQL Server database:

I have created a form with text fields, a picture box, to display/edit/add single records to a database successfully and am successfully using all of the following:

DB Connection = RRConnection

SqlCommand object = playersCommand

SqlDataAdapter object = playersAdapter

dataTable object = playersTable

currencyManager = playerManager

PictureBox1 control = including a button to navigate filesystem and select/load jpeg image into form PictureBox1

Bindings like - txtName.DataBindings.Add("Text", playersTable, "Name") for text fields - I think I need to bind the picturebox1 IMage Byte Array but do not know how to do

When the form closes, the table is loaded into the SQLServer database table tbl_BB into the column named "Image" of type image.

I know I have to convert the image to a byte array to save, but am confused on how to bind the image byte array to the dataTable, allowing it to be uploaded to the database on form close. I've done a lot of googling on it but the solutions do not seem to match the code I am using...

Given the above, can you advise the code necessary for this?

Ankit
  • 5,733
  • 2
  • 22
  • 23
  • If you can't find a solution that fits your code, it would be helpful to include a sufficient sample of that code in your question. – Adrian Aug 11 '17 at 18:55
  • See [here](https://stackoverflow.com/a/35452889/5045688) and [here](https://stackoverflow.com/a/22938707/5045688). – Alexander Petrov Aug 12 '17 at 08:31

3 Answers3

1

Don't use image it will be removed from MS SQL soon. https://learn.microsoft.com/en-us/sql/t-sql/data-types/ntext-text-and-image-transact-sql

Just use varbinary.

0

the general way to do it is:

1) SQL Server will have a varbinary(max) column.

2) Take the jpeg and convert it to a byte array

3) add the byte array as a paramter to your sql command object

4) execute nonquery for insertion.

chris-crush-code
  • 1,114
  • 2
  • 8
  • 17
  • chris-crush-code Thank you for your answer... may I please ask you to give me a code example of: 3) add the byte array as a parameter to your sql command object... My sqlCommand object is named PlayersCommand, and assume the byte array is named playerPhotoArray... – user8441794 Aug 11 '17 at 21:16
  • you'd use PlayersCommand.AddParameterWIthValue("parameter name goes here", playerPhotoArray); if you post your code i can help further – chris-crush-code Aug 12 '17 at 12:55
0

May I suggest you merely save the image on disk directly, and reference the path to the image in the DB instead? Saving images in a db is considered bad practice.

Morten Bork
  • 1,413
  • 11
  • 23
  • in a shared hosting environment you won't have access to the file system so you'll need to save it to the database – chris-crush-code Aug 12 '17 at 12:53
  • You won't "need to". You can get a cloud server, setup your own, contact a hosting agency, as them to set one up for you. There are many options to it. And just as a disclaimer, I won't claim that you can't find a specific scenario where the database would perform better than the filesystem, what I am saying, is that those scenarios are quite specific and far between. You will almost always have an easier time, simply storing the url to an image rather than the image itself, in a db. – Morten Bork Aug 13 '17 at 20:24