Main Page | Report this Page

 




 

 

.NET DotNet Forum Index  »  ADO .NET Forum  »  EF or LINQtoSQL and VarBinary Max?...
Page 1 of 1    

EF or LINQtoSQL and VarBinary Max?...

Author Message
SnapDive...
Posted: Tue Aug 25, 2009 7:20 am
Guest
I have a MSSQL 2005 database and need to store images in a
VerBinary(MAX) column. I would like to use LINQtoSQL or Entity
Framework on .NET 3.5-SP1 to do CRUD on a table with this kind of
data. Where can I find a sample?

Thanks.
 
Gregory A. Beamer...
Posted: Tue Aug 25, 2009 10:05 am
Guest
SnapDive <SnapDive at (no spam) community.nospam> wrote in
news:g7p795hoa8nu1cvqfu440lsnugocl09ij3 at (no spam) 4ax.com:

Quote:


I have a MSSQL 2005 database and need to store images in a
VerBinary(MAX) column. I would like to use LINQtoSQL or Entity
Framework on .NET 3.5-SP1 to do CRUD on a table with this kind of
data. Where can I find a sample?

Thanks.


According to the mapping doc for LINQ:
http://msdn.microsoft.com/en-us/library/bb386947.aspx

You should use byte[] or Binary to store the value. Other than that, it is
like binding anything else with LINQ. Searching the binary field is a pain,
however.

With EF, it should bind without issue. Searching still a pain.

On the search bits, it is rare to search binary fields, so that is probably
not an issue.

There are some issues with inserting binary via EF. I would Google this, as
I don't have a solution (have not saved binary). I am not sure about saving
LINQ.

Peace and Grace,


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: at (no spam) gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Lingzhi Sun [MSFT]...
Posted: Wed Aug 26, 2009 12:28 am
Guest
Hi SnapDive,

Gregory is correct that in LINQ to SQL, varbinary(max) is bound to
System.Data.Linq.Binary or byte[], while in LINQ to Entities, it is
generally bound to byte[]. To do CRUD operations on such tables with
varbinary(max) via LINQ to SQL and LINQ to Entities, you can see the
following codes for references:

Here is a helper method to read images into byte[]:
===================================================================
private static byte[] ReadImage(string path)
{
try
{
// Open the image file
using (FileStream fs = new FileStream(path, FileMode.Open,
FileAccess.Read))
{
// Create an array of bytes
byte[] bPicture = new byte[fs.Length];

// Read the image file
fs.Read(bPicture, 0, Convert.ToInt32(fs.Length));

return bPicture;
}
}
catch (Exception ex)
{
Console.WriteLine(
"The application throws the error: {0}", ex.Message);
return null;
}
}
===================================================================

LINQ to SQL: (for Binary, if mapping to byte[], the codes is similar with
the LINQ to Entities part)
===================================================================
Insert:
MyDataContext db = new MyDataContext();
MyImageTable myImage = new MyImageTable()
{
ImageID = 2,
MyImage = ReadImage("C#.jpg")
};
db.MyImageTables.InsertOnSubmit(myImage);
db.SubmitChanges();

Read:
var myImage = db.MyImageTables.Where(i => i.ImageID == 2).Single();
MemoryStream stream = new MemoryStream();
stream.Write(myImage.MyImage.ToArray(), 0, myImage.MyImage.Length);
Image image = Image.FromStream(stream);

Search:
byte[] imageBytes = ReadImage("C#.jpg");
var query = from i in db.MyImageTables
where i.MyImage == imageBytes
select i;
MessageBox.Show(query.First().MyImage.Length.ToString());

Delete:
db.MyImageTables.DeleteOnSubmit(myImage);
db.SubmitChanges();
===================================================================

LINQ to Entities:
===================================================================
Insert:
MyEntities entities = new MyEntities();
MyImageTable myImage = new MyImageTable();
myImage.ImageID = 2;
myImage.MyImage = ReadImage("C#.jpg");
entities.AddToMyImageTable(image);
entities.SaveChanges();

Read:
MemoryStream stream = new MemoryStream();
MyImageTable myImage = entities.MyImageTable.First();
stream.Write(myImage.MyImage, 0, myImage.MyImage.Length);
Image image = Image.FromStream(stream);

Search:
byte[] imageBytes = ReadImage("C#.jpg");
var query = from i in entities.MyImageTable
where i.MyImage == imageBytes
select i;

Delete:
entities.DeleteObject(myImage);
entities.SaveChanges();
===================================================================

If you encounter any problems, please feel free to let me know.

Have a nice day!

Best Regards,
Lingzhi Sun
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg at (no spam) microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

MSDN Managed Newsgroup support offering is for non-urgent issues where an
initial response from the community or a Microsoft Support Engineer within
2 business day is acceptable. Please note that each follow up response may
take approximately 2 business days as the support professional working with
you may need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations that require urgent,
real-time or phone-based interactions. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Lingzhi Sun [MSFT]...
Posted: Fri Aug 28, 2009 4:45 am
Guest
Hi SnapDive,

I am writing to check the status of the issue on your side. Would you mind
letting me know the result of the suggestions? If you need further
assistance, feel free to let me know. I will be more than happy to be of
assistance.

Have a nice weekend!

Best Regards,
Lingzhi Sun
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg at (no spam) microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 
 
Page 1 of 1    
All times are GMT - 5 Hours
The time now is Sat Jul 31, 2010 4:19 pm