条件查询

       public DataTable GetTable(DataRow[] drows)
        {

            DataTable tempT = new DataTable();
            if (drows.Length == 0) return tempT;
            tempT = drows[0].Table.Clone();//克隆表结构
            DataSet tempDs = new DataSet();
            tempDs.Tables.Add(tempT);//添加到辅助数据集中
            tempDs.Merge(drows);//利用数据集合并行集到表
            return tempDs.Tables[0];//返回此表

        }

       

        public DataTable pkzeng(DataTable dt,string[] str)
        {
            /*DataTable MyTable = new DataTable();
            DataSet tempDs = new DataSet();
            DataRow[] rowst = dt.Select("");
            DataTable Table = rowst[0].Table.Clone();
            string name = dt.TableName;
            //参数循环
            for (int i = 0; i < str.Length; i++)
            {
                //列循环
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    //取得匹配行
                    DataRow[] rows = dt.Select(dt.Columns[j].ColumnName + " like '" + str[i] + "'");
                    if (rows.Length != 0)
                    {
                        DataTable temp = rowst[0].Table.Clone();
                        temp.TableName = i + "-" + j;
                        tempDs.Tables.Add(temp);
                        tempDs.Merge(rows);
                    }
                }
                dt = tempDs.Tables[name].Copy() ;
                tempDs.Tables[name].Clear();
            }
            MyTable = dt;
            return MyTable;*/
            dt.Columns.Add("pkzeng", Type.GetType("System.String"));
            int number = 0;
            string Sql = "";
            DataTable Table = new DataTable();
            //参数循环
            for (int i = 0; i < str.Length; i++)
            {
                //列循环
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    //取得匹配行
                    Sql = dt.Columns[j].ColumnName + " like '" + str[i] + "'";
                    if (dt.Columns[j].DataType == Type.GetType("System.String"))
                    {
                        DataRow[] rows = dt.Select(Sql);
                        if (rows.Length != 0)
                        {
                            for (int k = 0; k < rows.Length; k++)
                            {

                                DataRow temp = rows[k];
                                temp["pkzeng"] = "pkzeng" + i;
                                number = i;
                                rows[k] = temp;
                            }
                            Sql = dt.Columns[j].ColumnName + " like '" + str[i] + "' and " + dt.Columns["pkzeng"].ColumnName + " = 'pkzeng" + i + "'";
                        }
                    }
                   
                }
                dt = GetTable(dt.Select(dt.Columns["pkzeng"].ColumnName + " = 'pkzeng" + number + "'"));
            }
            if (dt.Rows.Count != 0)
            {
                DataRow[] MyRows = dt.Select(dt.Columns["pkzeng"].ColumnName + " = 'pkzeng" + (str.Length - 1) + "'");
                Table = GetTable(MyRows);
                Table.Columns.Remove("pkzeng");
            }
            return Table;
        }

原文地址:https://www.cnblogs.com/zlf344242525/p/2271726.html