简体   繁体   English

在SQL Server 2008表中搜索uniqueidentifier值

[英]Search uniqueidentifier value in SQL Server 2008 table

I have a table in SQL Server 2008 with uniqueidentifier field. 我在SQL Server 2008中有一个带有uniqueidentifier字段的表。

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. 在我的应用程序(C#,WPF)中,我有一个文本框,该文本框用于通过GUID搜索某些数据,用户可以通过键入,ctrl + v等输入guid,因此他/她会出错。

What is the best and fastest approach to make a search by this field using linq2sql ignoring whitespaces, "-"... any other characters? 使用linq2sql忽略空格,“-” ...其他任何字符来通过该字段进行搜索的最佳,最快的方法是什么?

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. TapeCode在SQL Server 2008中是uniqueidentifier ,在dbml文件中是Guid。 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: 相反,您必须将输入字符串转换为Guid并进行比较:

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). (请注意, Guid.Parse方法接受不带破折号的32个十六进制数字作为有效输入)。

If it's a uniqueidentifier field in SQL, I'd expect native support in LINQ to SQL. 如果它是SQL中的uniqueidentifier字段,那么我希望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. 现在,如果text不代表有效的GUID,那当然会引发异常。 If you're using .NET 4, you can use Guid.TryParse in the same way you would for parsing integers etc: 如果您使用的是.NET 4,则可以使用与解析整数等相同的方式来使用Guid.TryParse

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. 您不应该尝试在SQL字段上执行.Replace,因为这实际上将在服务器上执行,这非常慢。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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