开发者

Evaluate "image" SQL column in a query

开发者 https://www.devze.com 2023-03-09 04:33 出处:网络
I have a database containing a table with an \"Image\" colum: This column actually contains a long string encoded as HEX byte values.

I have a database containing a table with an "Image" colum:

Evaluate "image" SQL column in a query

This column actually contains a long string encoded as HEX byte values.

I need to select all records where the string encoded by this column contains a certain substring. Pseudocode would be:

Select *
From SomeTable
Where dataColumnofTypeImage.ToString().Contains("somesubstring")

I tried to do this in Linq (LinqPad) using:

from z in Zanus
let p = z.Udata.ToArray() // z.Udata will be of type System.Linq.Binary so I want to     make a byte array out of it...
where System.Text.ASCIIEncoding.ASCII.GetString(p).Contains("EXED")
select new
{
    z.Idnr,
    z.Udatum,
    z.Uzeit,
    z.Unr,
    z.Uart,
    z.Ubediener,
    z.Uzugriff,
    z.Ugr,
    z.Uflags,
 开发者_C百科   z.Usize,
    z.Udata
}

But this does not work at all saying:

NotSupportedException: Method 'Byte[] ToArray()' has no supported translation to SQL.

I just cannot believe it would not be possible to check a binary datatype in a Where clause just as I might check some other datatype...

Can somebody help me out here?


The only way I know of doing this is to use direct sql and the Substring

For example, something like :

string str1 = @"
    select 
        Idnr, 
        Udatum,
        Uzeit, 
        Unr, 
        Uart, 
        Ubediener, 
        Uzugriff, 
        Ugr,
        Uflags, 
        Usize, 
        Udata 
    from  Zanus 
    where Udata is not null 
       and SubString(Udata, 1 , 2147483647) like '%EXED%'" ;

    var query1 = this.ExecuteQuery<Zanus>(str1);
    (from z in query1 
    select new
    {
        z.Idnr,
        z.Udatum,
        z.Uzeit,
        z.Unr,
        z.Uart,
        z.Ubediener,
        z.Uzugriff,
        z.Ugr,
        z.Uflags,
        z.Usize,
        z.Udata
    }    

Note, this will fail if the search string is in a column, but not in the first 2,147,483,647 bytes (but if these are images, I don't think that should be a concern).


where p.Contains(System.Text.ASCIIEncoding.ASCII.GetString("EXED"))

That would presumably be the code for what you're trying to do, whether it would work would depend on your image string

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号