/// <summary>
        
/// 搜索方法
        
/// </summary>
        
/// <param name="toFindDt">要搜索的DataTable</param>
        
/// <param name="PKIDColumnName">表的标识列</param>
        
/// <param name="ColumnHt">HashTable表</param>
        
/// <returns></returns>

        protected string SearchFiles(DataTable toFindDt,string PKIDColumnName, Hashtable ColumnHt)
        
{
            
//行数
            string[] ArraryPKID =  new string[toFindDt.Rows.Count];
            
for(int i=0; i<toFindDt.Rows.Count; i++)
            
{
                ArraryPKID[i] 
= "";
            }

            
string searchKey = "";
            
string cloumnName = "";
            
bool bfind;

            
for(int i=0; i<toFindDt.Rows.Count; i++)
            
{
                
//bfind = false;
                bfind = true;

                
#region 寻找各个字段ColumnHt里的是否有关键字
                
//寻找各个字段ColumnHt里的是否有关键字
                foreach (DictionaryEntry myDE in ColumnHt) 
                
{                    
                    cloumnName 
= myDE.Key.ToString();
                    searchKey 
= myDE.Value.ToString();

                    
#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<searchKey.Length;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<len;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<ArraryPKID.Length; 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);
                            
//取结果……
                        }

                    }

   呵呵,这个是或的关系,很容易改成与的关系.请高手赐教!
posted on 2006-12-18 09:20  冷火  阅读(601)  评论(0编辑  收藏  举报