By reza.mohseni


2019-03-14 20:05:02 8 Comments

I save images in my database in VARBINARY(max) with this procedure

ALTER proc [dbo].[User_Create]
(@BirthCertificateImage varbinary(max),
@BookletImage varbinary(max),
@GreenCardImage varbinary(max))
as
insert into [User]
(BirthCertificateImage,BookletImage,GreenCardImage)
values
(@BirthCertificateImage,@BookletImage,@GreenCardImage)

And I'm extracting a field with this procedure

ALTER proc [dbo].[User_GetById]
(@UserId int)
as
select * from [User]
where
Id = @UserId

and I use User_GetById procedure in Entity framework with this code

var user = await db.Database.SqlQuery<UserModel>("User_GetById @UserId"
, new SqlParameter("UserId",id)).SingleOrDefaultAsync();

and it's my code for showing the binary image in PictureBox

var birthCertificateMs = new 
MemoryStream(response.Entity.BirthCertificateImage);
birthCertificatePictureBox.Image = Image.FromStream(birthCertificateMs);
birthCertificateMs.Close();

also, my property's type for Maintenance the image is byte[]

So how can I manage it if it was null I won't get in trouble?

consider it works correctly when it is not null

1 comments

@Steve Py 2019-03-15 03:02:05

I would recommend explicitly checking for null/empty before attempting to use the data.

if(response.Entity.BirthCertificateImage != null && response.Entity.BirthCertificateImage.Length > 0)
{
    using(var birthCertificateMs = new MemoryStream(response.Entity.BirthCertificateImage))
    {
        birthCertificatePictureBox.Image = Image.FromStream(birthCertificateMs);
    }
}

and I would opt to wrap all of that in a Try/Catch block to handle where the byte data doesn't contain an image. (It happens, especially as systems mature, someone supports chucking a PDF or such in there.)

One other bit of advice: for large binary data such as images, I would recommend updating the schema to move these off into a separate table linked in a 1-0..1 relationship with User. (So a new table called UserImage with a PK of UserId, then User can be configured with a HasOptional on UserImage) The reason for this is that this data is likely not going to be accessed that frequently, where you might Join/Select on "User" fairly often. If you use User as a reference and code eager/lazy-loads a User with those larger blobs, it will have a performance impact. If the user image data is in a separate related table, you can reference User without a performance cost of that expensive data until it is explicitly needed.

Related Questions

Sponsored Content

12 Answered Questions

[SOLVED] How can I add new array elements at the beginning of an array in Javascript?

  • 2011-11-10 00:35:22
  • Moon
  • 680056 View
  • 1369 Score
  • 12 Answer
  • Tags:   javascript arrays

79 Answered Questions

[SOLVED] How do I remove a particular element from an array in JavaScript?

  • 2011-04-23 22:17:18
  • Walker
  • 5720296 View
  • 7117 Score
  • 79 Answer
  • Tags:   javascript arrays

44 Answered Questions

[SOLVED] How to check if an object is an array?

25 Answered Questions

[SOLVED] How do I determine whether an array contains a particular value in Java?

  • 2009-07-15 00:03:21
  • Mike Sickler
  • 1787966 View
  • 2092 Score
  • 25 Answer
  • Tags:   java arrays

46 Answered Questions

18 Answered Questions

[SOLVED] How do I empty an array in JavaScript?

  • 2009-08-05 09:08:39
  • akano1
  • 2216591 View
  • 2199 Score
  • 18 Answer
  • Tags:   javascript arrays

30 Answered Questions

[SOLVED] How to append something to an array?

14 Answered Questions

[SOLVED] How to insert an item into an array at a specific index (JavaScript)?

38 Answered Questions

39 Answered Questions

[SOLVED] How do you convert a byte array to a hexadecimal string, and vice versa?

  • 2008-11-22 10:03:13
  • alextansc
  • 755699 View
  • 1254 Score
  • 39 Answer
  • Tags:   c# arrays hex

Sponsored Content