搜索部分单词

时间:2012-09-15 22:32:53

标签: c# asp.net sql sqldatasource

所以我在sql 2008服务器上有一个Customers表,我在我的应用程序中绑定到gridview。搜索按钮使用以下代码重新绑定表:

ControlParameter ctrlParam = new ControlParameter();
ctrlParam.ControlID = "SearchTextBox";
ctrlParam.PropertyName = "Text";
ctrlParam.Name = "Search";
QueryStringParameter ctrlParam2 = new QueryStringParameter();
ctrlParam2.DefaultValue = "Canceled";
ctrlParam2.QueryStringField = "Canceled";

ctrlParam2.Name = "CustomerStatus";
ctrlParam2.Type = TypeCode.String;
SqlDataSource1.SelectParameters.Clear();
SqlDataSource1.SelectParameters.Add(ctrlParam);
SqlDataSource1.SelectParameters.Add(ctrlParam2);
SqlDataSource1.SelectCommand = "SELECT FullText_for_Customers.RANK, [CustomerId], [CustomerStatus], [CustomerGroup], [CustomerFirstName], [CustomerLastName], [CustomerFullName], [CompanyName], [CustomerAddress], [CustomerCity], [CustomerState], [CustomerZipcode], [CustomerFullAddress], [CustomerEmail], [CustomerEmail2], [CustomerPhoneNumber], [CustomerPhoneNumberExt], [CustomerType], [CustomerSubscriptionType], [CustomerCost], [CustomerPaymentMethod], [CustomerPickUpDay], [CustomerPickUpDay2], [CustomerSignUpDate], [CustomerNotes], [CustomerDriver] FROM [Customers] INNER JOIN FREETEXTTABLE(Customers, (CustomerFirstName, CustomerLastName, CustomerFullName, CompanyName, CustomerAddress, CustomerCity, CustomerState, CustomerZipcode, CustomerFullAddress, CustomerEmail, CustomerEmail2, CustomerPhoneNumber, CustomerNotes), @Search) as FullText_for_Customers ON FullText_for_Customers.[KEY]=Customers.CustomerId WHERE ([CustomerStatus] <> @CustomerStatus) ORDER BY FullText_for_Customers.RANK DESC";

现在GridView分为CustomerID, CustomerFirstName, CustomerLastName, CustomerPickUpDay, and CustomerSignUpDate

当我搜索时,我只搜索名字,但我必须输入完整的名字或姓氏,我不能只输入其中的一部分;比如我的姓,Bonilla,我必须搜索Bonilla以显示结果,我不能只搜索Bon或Bo或illa。我怎么能有这个工作?

1 个答案:

答案 0 :(得分:1)

自由文本搜索并不像那样。但是,您可以得到近似值。使用CONTAINSTABLE而不是FREETEXTTABLE并在搜索词中添加星号(*)。这样,您将获得以“Bo”或“Bon”开头的名称。但是,没有办法让名字以“illa”结尾。

(另外,通过使用FREETEXTTABLE,您要求数据库将您的搜索词放在同义词库中,对于名称来说,这可能是一个坏主意。)