一段简单的代码[求助]
public static DataTable SearchFor(string strKey)
{
objConn.Open();
string[] keyStrings;
string tempString = " ";
char[] temp = tempString.ToCharArray();
string enterTemp = strKey;
keyStrings = enterTemp.Split(temp);
//循环读取关键字并输出
DataTable objDataTable = null;
foreach (string keyString in keyStrings)
{
string strComm = "SELECT ... ";
SqlDataAdapter objComm = new SqlDataAdapter(strComm, objConn);
DataSet objDS = new DataSet();
objComm.Fill(objDS, "article ");
if (objDS.Tables[ "article "].Rows.Count > 0)
{
objDataTable = objDS.Tables[ "article "];
}.
}
return objDataTable;
objConn.Close();
}
----------------------------------------------------
现在搜到的结果只能是包含最后一个关键字的数据,
怎么才能对返回的数据做个累加,好能把包含全部关键字的数据都列出来呢?
------解决方案--------------------把关键字弄成如下形式
'test1 ', 'test2 ', 'test3 ', 'test4 '
然后在SQL语句中IN一下就好了
------解决方案--------------------把关键字循环输出到数组,再将数组转成SQL识别的字符串
string[] ID = new string[a.Length];
if (a.Length > 0)
{
for (int i = 0; i < a.Length; i++)
{
ID[i] = a[i].Substring(0, 4);
}
string id= " ";
for (int i = 0; i < a.Length; i++)
{
id += ID[i] + ', ';
}
在写SQL的时候 ---- WHERE id IN ( "+id+ ")
------解决方案--------------------同一楼,拼接字符串
------解决方案--------------------