0

This is my first question on StackOverflow!

I am using NHibernate 2 in my vb.net project I also use NHibernate.JetDriver to access a MS Access database

I have a table named tblPeople and it has a field named 'PersonImage' witch it is of the 'attachment' field type

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    namespace="BusinessModel" assembly="NHibernateDemo">
  <class name="clsPeople" table="tblPeople">
    <id name="ID">
      <column name="[ID]" sql-type="int" not-null="true" />
    </id>
    ....
    <property name="Image">
      <column name="[PersonImage]" sql-type="ntext/nvarchar/varbinary/?????"   />
      <!--varbinary(max)-->
    </property>
  </class>
</hibernate-mapping>

Here is the class

Namespace BusinessModel

    Public Class clsPeople 

       Public Overridable Overloads Property ID() As Integer


       Public Overridable Overloads Property Image() As [byte()/String/String/**?????**]

    End Class

End Namespace

Any Ideas on what data types to choose for the mappings in order to get the actual images from the table ?

Any help in the right direction will make me huppy!

I even tried with a hibernate 3 and Custom Compilled JetDriver but i also whatever i do i keep getting errors Could not cast the value in field column of type String to the Type BinaryBlobType or Byte[] or whatever except if i put String where i get a ; seperated list of filenames but nothing more!

hazzik
  • 13,019
  • 9
  • 47
  • 86
Angelos
  • 29
  • 3
  • i found this but i do not get it http://stackoverflow.com/questions/2141149/nhibernate-lazy-loaded-properties – Angelos Jul 07 '13 at 22:50
  • i also found this but i do not understand if i should use it http://www.martinwilley.com/net/code/nhibernate/usertype.html – Angelos Jul 08 '13 at 12:32

2 Answers2

0

You don't have to specify sql-type unless it is really necessary. Instead you can specify the "type" as "String" or "AnsiString".

Secondly I highly recommend you to use the last version (3.3.3 as of this moment) of NHibernate. Also you can consider other mapping types like Mapping By Code or attributes (but stay away from fluent :)
PS. I found your question from freenode IRC

emperon
  • 369
  • 3
  • 11
  • The tricky thing is here how to convert from string to image ? I am not sure on how to compile the jetdriver with the 3.3.3 version but i am now downloading the repositories to find out. – Angelos Jul 08 '13 at 09:07
  • i compiled the latest nhebernate 3.3.3GA with the JetDriver with no luck either. whatever i do i keep getting errors "Could not cast the value in field column of type String to the Type BinaryBlobType" or "Byte[]" or "whatever" except if i put String where i get a ; seperated list of filenames but nothing more! seems that this happens : http://stackoverflow.com//questions/6895523/need-c-sharp-code-for-reading-multiple-attachments-from-microsoft-access-attachm – Angelos Jul 08 '13 at 15:22
0

ok i think i am close enough!

i use this property in my hbm.xml

  <property name="FileData">
      <column name="Image.FileData" />
    </property>

i use this property in my class clsPeople

Private _FileData As Byte() Public Overridable Overloads Property FileData As Byte() Get Return _FileData End Get Set(value As Byte())

            If value IsNot Nothing Then

                Try
                    'get offset to data
                    Dim offsetbytes As Byte()
                    offsetbytes = SubArray(Of Byte)(value, 0, 4)

                    Dim offset = BitConverter.ToInt32(offsetbytes, 0)
                    Console.WriteLine("offset : " & offset)

                    Dim headerBytes() As Byte = SubArray(Of Byte)(value, 0, offset)
                    'Dim allbytes() As Byte = CType(value, Byte())
                    Dim header = BitConverter.ToString(headerBytes, 0)

                    Dim databytes(0 To (value.Length - offset) - 1) As Byte
                    Array.ConstrainedCopy(value, offset, databytes, 0, value.Length - offset)
                    Dim img As Image = ObjToImg(databytes)
                    img.Save("c:\" & Κωδικός_Ακινήτου & ".jpg", ImageFormat.Jpeg)
                Catch ex As Exception

                End Try

            End If
            _FileData = value
        End Set
    End Property

i will keep you posted on how this works on one / more than one attached files

Angelos
  • 29
  • 3