比如我数据库中,有一个字段(Numbers),里面记录的是一串数字加空格,例如
(2 5 8 11 15 18 19 21 22 24)不包含括号现在我要从数据库中Numbers列的数据中,找出最少包含3个 (2 4 6 8 9)中的数的数据出来,该怎么做呀?例如数据库中某一行的Numbers里的字符串是:2 5 8 11 15 18 19 21 22 24我给出的5个数字是(2 5 7 9 22),那么这个就符合要求,取出来放入数组中!我用的是Access数据库,谢谢大家啦!
(2 5 8 11 15 18 19 21 22 24)不包含括号现在我要从数据库中Numbers列的数据中,找出最少包含3个 (2 4 6 8 9)中的数的数据出来,该怎么做呀?例如数据库中某一行的Numbers里的字符串是:2 5 8 11 15 18 19 21 22 24我给出的5个数字是(2 5 7 9 22),那么这个就符合要求,取出来放入数组中!我用的是Access数据库,谢谢大家啦!
如果只有这个方法的话,那请问,怎么把Numbers列中的数据都读到一个数组中?
程序不过的,自己调试一下,思路这样! System.Collections.ArrayList alGetDataRow = new System.Collections.ArrayList(s1.Length);
string sQuery = "2 5 7 9 22";
string[] sQueryArray = sQuery.Split(new char[] { ' ' }); //分割匹配条件的字串
DataTable dt = new DataTable();
foreach (DataRow drTemp in dt.Rows)
{
//分割字串处理
string[] s1 = drTemp["Numbers"].ToString().Split(new char[] { ' ' });
System.Collections.ArrayList al = new System.Collections.ArrayList(s1.Length);
s1.CopyTo(al,0);
int i = 0;
foreach (string sTemp in sQueryArray)
{
if (al.Contains(sTemp))
{
i++;
if (i == 3) //满足3个符合的条件
{
alGetDataRow.Add(drTemp); //放入数组
break;
}
}
}
} //alGetDataRow 存放了DATAROW的数组
这种在查找2这个数字时候,会把22找出来,
楼主不应该存放在一个字段中,这种存放查找会比较晕,用ACCESS这就功能更弱了,否则用2005的存放成XML,然后用XML查找方法查询,应该会更准确!
{
String[] wanted = {'2','5','7', '9', '22'};
OleDBDataReader reader = Conn.ExecuteReader("SELECT NumList FROM YourTable");
List<String> result = new List<String>();
while reader.read()
{
string [] numArr = reader[0].ToString().split(' ');
int i = 0; //numArr起始下标
int j = 0; //wanted起始下标
int fixed = 0; //找到符合的个数//(假定NumList字段中的数字是排好序的,才能高效率地比较)
while (i < numArr.Length)
{
if (numArr[i] == wanted[j])
{
j ++;
fixed ++;
}
i++;
}
if (fixed >=3)
{
result.Add(reader[0].ToString());
}
}//returns result Array
return result.ToArray();
}