Need help with ICriteria interface within NHibernate (C#)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • moorcroft
    New Member
    • Mar 2008
    • 57

    Need help with ICriteria interface within NHibernate (C#)

    Hi, I'm pretty new to the NHibernate framework and am trying to use it to query my database. I have a lookup table set up (image.hbm.xml) with the properties set out as follows:

    Code:
        <property name="ForeignLink" column="FORNBLG" />
        <property name="IncludeInMerge" column="INCLUDE_IN_MERGE" />
        <property name="ReferenceNumber" column="REF_NO" type="AnsiString"/>
        <property name="FileName" column="FILENAME" type="AnsiString"/>
        <property name="Notes" column="NOTES" type="AnsiString"/>
    I then also have a class set up representing the lookup table (image.cs), and the name of the table in the database is IMAGE.

    OK, now what I am doing is passing a string into a web service for example "2523-ATTACHMENT-57925", and the web service parses the number at the end of this string as an Integer which represents the primary key of the IMAGE table (field name LINK). I need to use this primary key as my search criteria and return the value from the field NOTES and return it as a string.

    I have the following code set up and am not too sure how to continue with it to return the string..

    Code:
    using (ISession session = mSessionFactory.OpenSession())
                        {
                            try
                            {
                                if (myString.IndexOf("-ATTACHMENT-") >= 0)
                                {
                                    //get primary key from myString (number after -ATTACHMENT-)
                                    int pk = int.Parse(myString.Substring((myString.IndexOf("-ATTACHMENT-") + 12)));
    
                                    ICriteria criteria = session.CreateCriteria(typeof(Image));
                                    criteria.Add(new EqExpression("Link", pk, true));
                                    Image image = (Image)criteria.UniqueResult();
                                   return ..............
    
    etc...
                                }
  • moorcroft
    New Member
    • Mar 2008
    • 57

    #2
    I've just realised it's easier than I thought, simply now have to say:

    Code:
    return image.Notes
    :D

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      I'm glad that you were able to solve your problem.
      Thanks for sharing your solution, it'll probably help others facing the same problem :)

      Just a note though: NHibernate is not native to the .NET environment. It is free as open source software that is mainly used to map .NET classes to database tables (from what I understand).

      Because it's not part of the .NET Framework (it's open source software developed a third party), you will probably not be able to find help for it here in the .NET forum....unless you're lucky and another member has used it before.

      -Frinny

      Comment

      Working...