简体   繁体   中英

Search uniqueidentifier value in SQL Server 2008 table

I have a table in SQL Server 2008 with uniqueidentifier field.

In my application (C#, WPF) I have a textbox which intended for search some data by GUID, user can enter guid by typing, ctrl+v, etc, so he/she can make an error.

What is the best and fastest approach to make a search by this field using linq2sql ignoring whitespaces, "-"... any other characters?

var d = from status in dc.FolderStatus.Where(status => status.Date <= DateTime.Now 
                    && status.Folder.TapeCode.ToString().Replace("-", "") == tapeCode.ToUpper().Replace("-", "").Replace(" ", "")
                    )

TapeCode is uniqueidentifier in SQL Server 2008 and Guid in dbml file. Is it ok?

No, that will be extremely slow (it'll have to do a table scan, convert each identifier to a string and compare the string). Instead, you'll have to convert the input string to a Guid and compare that:

tapeCode = Regex.Replace(tapeCode, "[^0-9a-zA-Z]+", "");
Guid id = Guid.Parse(tapeCode);
var d = from status in dc.FolderStatus
        where status.Date <= DateTime.Now 
           && status.Folder.TapeCode == id
        select status;

(Note that Guid.Parse method accepts 32 hexadecimal digits without dashes as a valid input).

If it's a uniqueidentifier field in SQL, I'd expect native support in LINQ to SQL. I believe you should be able to do:

Guid tapeCode = new Guid(text);
var d = from status in dc.FolderStatus
        where status.Date <= DateTime.Now && status.Folder.TapeCode == tapeCode
        select ...;

Now of course that will throw an exception if text doesn't represent a valid GUID. If you're using .NET 4, you can use Guid.TryParse in the same way you would for parsing integers etc:

Guid tapeCode;
if (Guid.TryParse(text, out tapeCode))
{
    var d = from status in dc.FolderStatus
            where status.Date <= DateTime.Now &&
                  status.Folder.TapeCode == tapeCode
            select ...;
}
else
{
    // Handle invalid input
}

You shouldn't try and do .Replace on the SQL field, as this will actually get executed on the server, which is very slow.

You should massage the data coming in to your method.

Ie

public void GetFoo(string guid)
{
     string sqlGuid = guid.ToString().Trim().Replace("-", "");

     var d = from status in dc.FolderStatus.Where(status => status.Date <= DateTime.Now &&
                                                            status.Folder.TapeCode == sqlGuid);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM