convert system.data.linq.binary to byte[]

前端 未结 4 827
执念已碎
执念已碎 2020-12-20 11:57

I am storing bytes in a database table. When I retrieve it with Linq 2 sql I get the return type in system.data.linq.Binary.

I am not able to convert th

相关标签:
4条回答
  • 2020-12-20 12:13
    (byte[])linqBinaryField.ToArray()
    
    0 讨论(0)
  • 2020-12-20 12:23

    You can try MemoryStream. I wrote a function in my project to convert an image to byte array like the following:

        public static byte[] Image2ByteArr(string filename)
        {            
            Bitmap bm = new Bitmap(getPath(filename));
            MemoryStream ms = new MemoryStream();
            bm.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return ms.ToArray();
        }
    

    Hope that helpful for you!

    0 讨论(0)
  • 2020-12-20 12:32

    Have you tried calling ToArray() on i.data?

    var img = from i in db.images
          where i.id == key
          select i.data.ToArray();
    

    System.Data.Linq.Binary has a ToArray method just for that purpose.

    0 讨论(0)
  • 2020-12-20 12:37

    Probably its too late by now but may help others :)

    //testTable PK:ID, binaryData :binary(32)
    
    public void insertDummyData()
    {
        DBML.testTable v = new DBML.testTable ();
        v.ID = 1;
    
        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
        v.binaryData = new System.Data.Linq.Binary(encoding.GetBytes("11111111000000001111111100000000"));                                                                    
    
        db.testTable.InsertOnSubmit(v);
        db.SubmitChanges();
    }
    

    Or else, Click on the Binary field from .dbml file, open properties and then change the field type from Binary to byte[] as found here

    0 讨论(0)
提交回复
热议问题