Retrieve the uniqueidentifier key value for a record

natsit kaewsungharn 1 Reputation point
2020-12-18T06:20:22.04+00:00

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

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,203 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,574 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,291 Reputation points
    2020-12-18T11:46:42.61+00:00

    Hello @natsit kaewsungharn

    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;  
        }  
    }  
    
    0 comments No comments