question

natsitkaewsungharn-6683 avatar image
0 Votes"
natsitkaewsungharn-6683 asked ·

Retrieve the uniqueidentifier key value for a record

i have DB field Picture datatype = uniqueidentifier , i want to get picture show on web page asp.net C#

sql-server-generaldotnet-csharp
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered ·

Hello @natsitkaewsungharn-6683

Not knowing how you interact with data let' say it's via a SqlClient connection and command using a where condition uniqueidentifier maps to a string so you can pass a string to represent a uniqueidentifier. Then in your SELECT place the column name for the picture in. Or via Entity Framework similarly use a .Where and .Select.

In either case the picture is a byte array, depending on the column type, image or varbinary(max) you can return a image using the following code by passing in the byte array. If using .NET Core then you need the NuGet package System.Drawing.Common.

 public class ImageHelpers
 {
     // for image column
     public static Image ByteArrayToImage1(byte[] contents)
     {
         using (var ms = new MemoryStream(contents))
         {
             return Image.FromStream(ms);
         }
     }
     // for varbinary(max)
     public static Image ByteArrayToImage(byte[] contents)
     {
         var converter = new ImageConverter();
         var image = (Image)converter.ConvertFrom(contents);
    
         return image;
     }
 }
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.