簡體   English   中英

在數據表的所有列中搜索字符串

[英]Search a string in all columns of a data table

我想搜索一個字符串,如果它存在於數據表中並返回行。 例如,我有一個數據表,其中包含用戶名,用戶密碼等字段。 現在,我的搜索文本將搜索所有字段,並向我返回包含字符串的行。

任何人都可以幫忙。
我用谷歌搜索。
我找到了幾種解決方案,其中一種是

Select("Code LIKE '" + lastWord + "%'" );  

但是這里我對“代碼”感到困惑。

問候

嘗試如下:

try
{
  SqlCommand cmd=new SqlCommand("select * from tableName where UserName LIKE '%' + @search + '%'", conn);
  cmd.Parameters.AddWithValue("@search",searchbox.text);
  SqlDataAdapter da=new SqlDataAdapter(cmd,con);
  DataSet ds=new Dataset();
  da.Fill(ds);
  //From here you can extract data in DataSet   
}
catch(exception ex)
{

}

可以嘗試SqlMethods。

做類似SqlMethods.Like(str,"%searchbox.text%");

也可以參考這個答案。

您是否需要類似Narayana Vyas的http://vyaskn.tripod.com/search_all_columns_in_all_tables.htm這樣的內容? 我以前用過它,而且可以用。 它搜索給定數據庫中所有表的所有列。

如果沒有的話

DataRow[] filteredRows =   datatable.Select(string.Format("{0} LIKE '%{1}%'", columnName, value));

或選中在所有DataTable列中查找字符串


解決方案之一就是這樣

for(int i=0;i<MyDataset.Tables[0].Columns.count;i++)
{

DataRow[] MyDR=MyDataset.Tables[0].Select(MyDataSet.Tables[0].Columns[i].ToString()+"='"+MyText+"'");

   if(MyDr.Length>0)
   {
   //You Found the item
   }

}

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM