Help with Images using CSLA and LINQ to SQL

Help with Images using CSLA and LINQ to SQL

Old forum URL: forums.lhotka.net/forums/t/8968.aspx


coommark posted on Friday, May 21, 2010

I am working on an application that requires customer passport photographs to be saved directly in an SQL Server 2005 DB. The field used is of type VARBINAR(MAX).

I am using CSLA 3.8.2.0. In this particular app, i have three layers: The DAL (using LINQ to SQL), the Library Layer (BOs) and a Winforms UI layer.

In this scenario, how do i save and retrieve images from the database? I am new to LINQ and have tried using my ADO.Net knowledge, but no luck.

I have searched several LINQ books, and online too, but couldnt get help on how to save and retrieve images the LINQ way.

Please help me if you can. I'll appreciate very much some code along with your answers. Waiting...

Mark

Russ replied on Friday, May 21, 2010

I hope this helps...

Business Property

private static PropertyInfo<byte[]> IconProperty = RegisterProperty<byte[]>(c => c.Icon, "Icon");

public byte[] Icon

{

  get { return GetProperty(IconProperty); }

  set { SetProperty(IconProperty, value); }

}

 

Data Access

private void DataPortal_Fetch(SingleCriteria<WidgetER, int> criteria)

{

  using (var ctx = ContextManager<Demo.DalLinq.DemoDataContext>.GetManager(Demo.DalLinq.Database.Demo))

  {

    var data = (from r in ctx.DataContext.Widgets

                where r.WidgetPK == criteria.Value

                select r).Single();

    LoadProperty(IdProperty, data.DamageTypePK);

    LoadProperty(NameProperty, data.Name);

    LoadProperty(DescriptionProperty, data.Description);

    LoadProperty(IconProperty, (data.Icon == null ? null : data.Icon.ToArray()));

    ...

  }

}

 

[Transactional(TransactionalTypes.TransactionScope)]

protected override void DataPortal_Insert()

{

  using (var ctx = ContextManager<Demo.DalLinq.DemoDataContext>.GetManager(Demo.DalLinq.Database.Demo))

  {

    int? newId = null;

    DateTime? newCreatedDate = null;

    System.Data.Linq.Binary newTimeStamp = null;

    ctx.DataContext.Widget_Insert(

      ReadProperty(NameProperty),

      ReadProperty(DescriptionProperty),

      (ReadProperty(IconProperty) == null ? new byte[0] : ReadProperty(IconProperty)),

      ...

      ReadProperty(CreatedByProperty),

      ref newId,

      ref newCreatedDate,

      ref newTimeStamp);

    LoadProperty(IdProperty, Convert.ToInt32(newId));

    LoadProperty(CreatedDateProperty, newCreatedDate);

    LoadProperty(Timestamp, newTimeStamp.ToArray());

    // update child objects

    FieldManager.UpdateChildren(this);

  }

  // invalidate cached NVList

  WidgetNVList.InvalidateCache();

}

Copyright (c) Marimer LLC