none
Linq To SQL Serialization RRS feed

  • Question

  • Hi All. My question is simply.

    I have a  Photos Table in SQL Server, the field of it are the follow

    [ PhotoID int,

    EmployeeID int,

    Photo IMAGE,

    LastChanged timestamp ]

    Now, When i drag and drop the table onto desginer

    the type of data IMAGE of the field Photo is converted to Linq.Binary.

    but i want that Linq Designerto converts it in Sistem.Byte() (an array of byte)

    I select the Photo table  in the designer and i can change manually the type to Byte() (System.Byte[]) So far so good.

    But i can´t modify stored procedures. Tha Linq Desinger shows me the follow sp:

    spInsertPhoto (ByRef photoID As System.Int32, employeID As System.Int32, photo As System.Data.Linq.Binary , LastChanged As System.Data.Linq.Binary)

    So I need to search in MyDbLinq.Designer.vb, the code of spInsertPhoto and spUpdatePhoto and change manually System.Data.Linq.Binary to System.Byte[].

    Does anybody know how to set the environment variables of Visual Studio 2008 to Linq designer serializes the type image to System.Byte[] intead of System.Data.Linq.Binary?

    In my db I have a lot of fields image (these fields are imported from all db), but i prefer to use VarBinary(Max) so i need to Linq Designer converts VarBinary(Max) and image data type to System.Byte[].

    Thanks Luis



    Saturday, September 25, 2010 2:11 AM

Answers

  • Hi Luisxvarg,

    First, do not change the auto-generated code. Changes to that codes may cause incorrect behavior and will be lost if the code is regenerated.

    Second, I think you can pass System.Byte[] directly to thephoto” parameter of spInsertPhoto because the array of bytes would be converted into an instance of the Binary type implicitly.

    We can see this from the Reflector.

    public static implicit operator Binary(byte[] value)
    {
      return new Binary(value);
    }
    
    


    So you can do something like this:

    byte[] byteArray = … ;
    spInsertPhoto (, ,
    byteArray,);
    ...

    Best regards,
    Alex Liang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, September 27, 2010 9:57 AM
    Moderator
  • In the spInsert or spUpdate i pass a byte() parameter of my class but the designer accepts a binary data (i would believe that this data is strongly typed)

    how can the linq designer take a byte() if its parameter is defined like binary by the sp? (the linq designer should throw an exception.)

     

    Hi Luisxvarg,

    As I have mentioned, when you pass a byte() to the parameter of the sp which needs a binary, the array of bytes would be converted into an instance of the Binary type implicitly, so it will be no problem when you use byte() directly.

    Best regards,
    Alex Liang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, September 28, 2010 7:42 AM
    Moderator

All replies

  • Hi Luisxvarg,

    First, do not change the auto-generated code. Changes to that codes may cause incorrect behavior and will be lost if the code is regenerated.

    Second, I think you can pass System.Byte[] directly to thephoto” parameter of spInsertPhoto because the array of bytes would be converted into an instance of the Binary type implicitly.

    We can see this from the Reflector.

    public static implicit operator Binary(byte[] value)
    {
      return new Binary(value);
    }
    
    


    So you can do something like this:

    byte[] byteArray = … ;
    spInsertPhoto (, ,
    byteArray,);
    ...

    Best regards,
    Alex Liang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, September 27, 2010 9:57 AM
    Moderator
  • Thanks for your reply Alex.

    I use a safe datareader so I need to convert system.Linq.Binary field to array, for example

     

    Code 1:

    MyDbLinqDesigner.Photo is System.Linq.Data.Binary

    Private Sub FetchMethod

    ...... here load prop for database

    LoadProperty(of Byte())(PhotoProperty, MyDbLinqDesigner.Photo .TOARRAY)

    ......

    End Sub

     

    Private Sub InsertMethod

    callSpInsert(...,READPROPERTY(of Byte(), photoProperty .ToARRAY,...)

    End Sub

     

    I GET an exception when MyDbLinqDesigner.Photo is NULL because my safedatareader is nullable of BYTE() not BYTE. So If  I set the property in LinqDesigner like System.BYTE() my code works fine in fetch method but i have to change manually all generated code for all sp , by this way, the correct code in my class is the same of above without TOARRAY, so looks like this:

     

    Code2 :

    Private Sub FetchMethod

    ...... here load prop for database

    LoadProperty(of Byte())(PhotoProperty, MyDbLinqDesigner.Photo )

    ......

    End Sub

     

    Private Sub InsertMethod

    callSpInsert(...,READPROPERTY(of Byte(), photoProperty ,...)

    End Sub

    To Resume I fix the Fetch method by setting manually System.data.LinqBinary to System.Byte() but it is a nightmare to change it for all sp (insert,update), so i need to change the generated code of the designer and works fine, but if you say that the behavior of this change is dangerous, i may to opt to verify if MyDbLinqDesigner.Photo is not null and then converts to array, and use Code 1 (pro bug), but i prefer to use code 2 because is straight, simple and not pro bug.

    Sorry for my basic english

    Thanks

     

     

     

    Monday, September 27, 2010 9:27 PM
  • Hi Alex, you fix my problem, THANKS.

    I don't need to change the stored procedures.

    I change binary to byte() and works fine, but i don´t understand anything.

    When I change binary to system.byte my fetch method accept byte(), it is ok.

    In the spInsert or spUpdate i pass a byte() parameter of my class but the designer accepts a binary data (i would believe that this data is strongly typed)

    Please, just a question.

    how can the linq designer take a byte() if its parameter is defined like binary by the sp? (the linq designer should throw an exception.)

     

     

    Tuesday, September 28, 2010 7:00 AM
  • In the spInsert or spUpdate i pass a byte() parameter of my class but the designer accepts a binary data (i would believe that this data is strongly typed)

    how can the linq designer take a byte() if its parameter is defined like binary by the sp? (the linq designer should throw an exception.)

     

    Hi Luisxvarg,

    As I have mentioned, when you pass a byte() to the parameter of the sp which needs a binary, the array of bytes would be converted into an instance of the Binary type implicitly, so it will be no problem when you use byte() directly.

    Best regards,
    Alex Liang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, September 28, 2010 7:42 AM
    Moderator
  • Thanks Alex, thanks you very much
    Tuesday, September 28, 2010 6:51 PM