/// 搜索方法
///
/// 要搜索的DataTable
/// 表的标识列
/// HashTable表
///
protected string SearchFiles(DataTable toFindDt,string PKIDColumnName, Hashtable ColumnHt)
{
//行数
string[] ArraryPKID = new string[toFindDt.Rows.Count];
for(int i=0; i {
ArraryPKID[i] = "";
}
string searchKey = "";
string cloumnName = "";
bool bfind;
for(int i=0; i {
//bfind = false;
bfind = true;
寻找各个字段ColumnHt里的是否有关键字#region 寻找各个字段ColumnHt里的是否有关键字
//寻找各个字段ColumnHt里的是否有关键字
foreach (DictionaryEntry myDE in ColumnHt)
{
cloumnName = myDE.Key.ToString();
searchKey = myDE.Value.ToString();
***下面开始对关键詞searchKey进行拆分***#region ***下面开始对关键詞searchKey进行拆分***
string[] arrkey = new string[10];
for(int j=0;j<10;j++)
{
arrkey[j]="";
}
int pos=0;
char[] ch = new char[100];
ch = searchKey.ToCharArray(0,searchKey.Length);
for(int j=0;j {
if(ch[j]!=' '&&ch[j]!=' '&&ch[j]!=',')
{
arrkey[pos] = arrkey[pos]+ch[j].ToString();
}
else
{
if(ch[j+1]!=' '&&ch[j+1]!=' '&&ch[j+1]!=',')
{
pos++;
}
}
if(pos==10)
{
PopMessage("对不起,您输入的关键词不能超过10个!");
return null;
}
}
#endregion
//找到关键词后,开始进行搜索
int len = pos+1;
string findStr = "";
findStr = toFindDt.Rows[i][cloumnName].ToString();
//关键词的数量
for(int j=0;j {
//找到字符串
// if(findStr.IndexOf(arrkey[j],0,findStr.Length) > -1)
// {
// bfind = true;
// }
if(findStr.IndexOf(arrkey[j],0,findStr.Length) == -1)
{
bfind = false;
break;
}
}
}//end foreach
//找到结果,这样找是或的结果
if(bfind == true)
{
ArraryPKID[i] = toFindDt.Rows[i][PKIDColumnName].ToString();
}
#endregion
}//end for
string PKIDS = "";
for(int i=0; i {
if( ArraryPKID[i].ToString() != "")
{
PKIDS += ArraryPKID[i] + ",";
}
}
if(PKIDS.Length > 0)
{
PKIDS = PKIDS.Substring(0,PKIDS.Length-1);
}
return PKIDS;
}
使用示例:
Hashtable columnHt = new Hashtable();
if(txtContent.Text.Trim() != "")
{
columnHt.Add("SerialNum",txtContent.Text.Trim());
}
if(txtTitle.Text.Trim() != "")
{
columnHt.Add("Title",txtTitle.Text.Trim());
}
if(columnHt.Count > 0)
{
//把所有的FileID取到,“,”隔开
string pkids = "";
pkids = this.SearchFiles(Sourcedt,"PKID",columnHt);
if(pkids != "")
{
string sqlStr = String.Format("select * from MyTable where PKID in {0}",pkids);
//取结果……
}
}
本文作者:xiongeee 来源:http://www.cnblogs.com/xiongeee/
CIO之家 www.ciozj.com 微信公众号:imciow