逐字搜索以从数据库列中完成字符串 [英] Searching Word by word to complete string from data base column

查看:71
本文介绍了逐字搜索以从数据库列中完成字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
我想搜索Google搜索引擎。当我将一个字符串拆分为多个单词并通过foreach语句连接时,结果未绑定到gridview。



这里是我的代码: -

  var  xts = Key.Split(' '); 
string vals;
foreach string asd in xts)
{
vals = asd.Trim();
cmd = new SqlCommand( SEL_SearchCompany ,cnn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add( @ KeyWord,SqlDbType.VarChar).Value =瓦尔斯;
da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
foreach (DataRow rw in dt.Rows)
{
DataRow rows1 = mainTable.NewRow();
rows1 = rw;
mainTable.Rows.Add(rows1);
// 尝试
/ / {mainTable.Rows.Add(rw);
// < span class =code-comment> mainTable.ImportRow(rw);
// }
// catch {};
}
}
GridView1.DataSource = mainTable;
GridView1.DataBind();





但这段代码没有绑定,因为发生了异常,这已经是数据表中的行。

解决方案

您正在寻找的东西很可能是自动完成文本框或下拉列表。

请参阅以下链接以获取概述和知识库:

自动完成文本框 [ ^ ]

在ASP.NET MVC 3中创建多列自动完成/组合框 [ ^ ]





--Amit


10个单词,你将调用数据库查询10倍。尝试这种替代方法:



将文本传递给存储过程并将其拆分。使用以下方法:



  CREATE   FUNCTION  SplitString( @ String   varchar  8000 ), @Delimiter   char  1 ))
返回 @ temptable TABLE (items varchar 8000 ))
as
begin
声明 @ idx int
声明 @ slice varchar 8000
选择 < span class =code-sdkkeyword> @ idx = 1
if len( @ String )< 1 @ String null return
while @idx!= 0
开始
set @ idx = charindex( @Delimiter @ String
if @idx!= 0
set @ slice = left @ String @ idx - 1
else
set @ slice = @String
if (len( @ slice )> 0)
插入 进入 @ temptable (项目) @ slice
set @ String = right @ String ,len( @String ) - @ idx
if len( @ String )= 0 break
end
return
end





您可以使用以下行获取单词列表:

  SELECT  i.items  FROM  dbo.SplitString( @ a ' ' AS  i 





现在你有了一个单词列表,你可以使用这个项目列表在你的表中查询不同的记录(你有在前端编写太多代码)。将返回包含搜索结果的单个结果集。检查它是否适合你。


首先,创建mainTable的表结构,然后添加如下行:



< pre lang =c#> foreach (DataRow rw in dt.Rows)
{
mainTable.Rows.Add(rw.ItemArray);
}


hi I want to search like Google search engine. When I split a string to many word and connecting through foreach statement the result was not bind to gridview.

here is my code:-

var xts = Key.Split(' ');
               string vals;
               foreach (string asd in xts)
               {
                   vals = asd.Trim();
                   cmd = new SqlCommand("SEL_SearchCompany", cnn);
                   cmd.CommandType = CommandType.StoredProcedure;
                   cmd.Parameters.Add("@KeyWord", SqlDbType.VarChar).Value = vals;
                   da = new SqlDataAdapter(cmd);
                   DataTable dt = new DataTable();
                   da.Fill(dt);
                   foreach (DataRow rw in dt.Rows)
                   {
                       DataRow rows1 = mainTable.NewRow();
                       rows1 = rw;
                       mainTable.Rows.Add(rows1);
                       //try
                       //{mainTable.Rows.Add(rw);
                       //mainTable.ImportRow(rw);
                       //}
                       //catch { };
                   }
               }
               GridView1.DataSource = mainTable;
               GridView1.DataBind();



but this code no bind because exception is occurs which is row already in data table.

解决方案

The thing which you are looking for is probable an AutoComplete textbox or dropdown.
Refer the links below for the overview and knowledge base:
AutoComplete Textbox[^]
Create Multiple Column Autocomplete/Combobox in ASP.NET MVC 3[^]


--Amit


for 10 words you will call the database query 10 times. Try this alternative method:

Pass your text to your stored procedure and split it there. Use the following method for that:

CREATE FUNCTION SplitString(@String varchar(8000), @Delimiter char(1))       
returns @temptable TABLE (items varchar(8000))       
as       
begin       
    declare @idx int       
    declare @slice varchar(8000)       
    select @idx = 1       
        if len(@String)<1 or @String is null  return       
    while @idx!= 0       
    begin       
        set @idx = charindex(@Delimiter,@String)       
        if @idx!=0       
            set @slice = left(@String,@idx - 1)       
        else       
            set @slice = @String       
        if(len(@slice)>0)  
            insert into @temptable(Items) values(@slice)       
        set @String = right(@String,len(@String) - @idx)       
        if len(@String) = 0 break       
    end   
return      
end



You can get the list of words using the following line:

SELECT i.items FROM dbo.SplitString(@a,' ') AS i



Now you have list of words you can query your table for different records using this list of items(for which you have to write too much of code in front-end). A single result set is returned with search result. Check if it works for you.


First, create the table structure of mainTable and then add rows like this:

foreach (DataRow rw in dt.Rows)
{
   mainTable.Rows.Add(rw.ItemArray);
}


这篇关于逐字搜索以从数据库列中完成字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆