0

So I have this mini project i found online that pretty much is create a Trading card browsing software in C#. I created a database with a table using server explorer in VS Windows Forms called Trading_Cards_DT that has a table called Trading_Cards

Create Table Trading_Card_Info(
    ID int,
    fullName varchar(30),
    HP int,
    Attack int,
    Resources int,
    TypeCreature varchar(30),
    Faction varchar(30),
    Color nvarchar(20),
    Picture Image
);

This is the columns in the table i want to implement and I also started inserting data into the Table as well but I don't know how to insert images and then retrieve all this data back onto a Windows Form layout.

I want to use a listBox in Forms to display only the name of the character in the list and when selected shows all the data about him not only the Display Member, Value Memeber, and Selected Value.

I used this video: https://www.youtube.com/watch?v=arA-3z49g8U To learn the basics but I also wanna do more but I can't find a source that shows me how to store the image initially in the database and then retrieve it in C#.

  • Could you please further elaborate on how you're currently inserting the data, especially if you're using any data access layer frameworks? Also, how do you currently imagine the concept of storing data such as images into a database could be realized? – Zdeněk Jelínek Dec 12 '20 at 20:38
  • 5
    From the [documentation](https://learn.microsoft.com/en-us/sql/t-sql/data-types/ntext-text-and-image-transact-sql?view=sql-server-ver15): "**IMPORTANT!** ... (the) `image` data type(s) will be removed in a future version of SQL Server. Avoid using ... (this) data type(s) in new development work, and plan to modify applications that currently use (it) .... Use ... `varbinary(max)` instead." – sticky bit Dec 12 '20 at 20:53
  • Adding to sticky bit's comment, the `image` data type has been deprecated since SQL Server 2005 - so really don't use it. If you do a simple Google search `"varbinary(max)" "JPEG" site:stackoverflow.com` you'll find about 500 questions demonstrating how to write and read picture data in SQL Server databases. I recommend adding a column describing what format the picture data is (bmp, gif, jpeg, png, etc.) so that you don't have to do signature sniffing on the binary data to find the correct image decoder to use when reading it back. – AlwaysLearning Dec 13 '20 at 00:08
  • @AlwaysLearning You don't need to. e.g., `Image image = Image.FromStream(new MemoryStream([The Byte Array]));`. Now, `image.RawFormat` has the Guid that references the Bitmap format. The `ImageFormat` also has the file extensions information. You can do ~the same thing with a BitmapSource. – Jimi Dec 13 '20 at 06:23
  • @Jimi System.Drawing.Image has extremely limited graphic format support. It doesn't even support JPEG-2000 and HEIF, let alone "obscure" formats like FITS (astronomy) and TGA (still common in game creation). – AlwaysLearning Dec 13 '20 at 10:45
  • @AlwaysLearning The question is tagged `winforms`, not just generic `sql-server`: it's assumed the OP wants to present the Images in a WinForms app, so `System.Drawing.Image` (usually). Or other formats, with external packages. The point is that the object that contains the Image (a `System.Drawing.Bitmap`, here), *knows* its format. As a consequence, direct or indirect, also the extension to apply to a file, if/when needed. – Jimi Dec 13 '20 at 16:45

1 Answers1

0

Image datatype in SQL Server is deprecated and will be removed. Therefore you should avoid using it.

One of the most common ways is to store the path and file name of the image and keep the files in the file system. If you really must store the image file in a table, the alternative is to store it as a VARBINARY data type. I suggest you to check this post out, which also links to a Microsoft Research paper on this topic.

Selim Balci
  • 940
  • 2
  • 11
  • 26