C#传参查询用like查询不出来
两种写法第一种除了like 查询不出来,其他都OK 另一种都 可以
底下是like不行的
public DataTable GetTable(string conditionClause, string fields,string tables, params string[] parameters)
{
int paramCount = parameters.Length;
string[] paramNames = new string[paramCount];
string clause = " ";
if(conditionClause!=null||conditionClause!=string.Empty)
clause= conditionClause.ToLower().Trim();
if (clause != string.Empty)
{
if (paramCount > 0)
{
for (int i = 0; i < paramCount; i++)
{
paramNames[i] = "@param " + i.ToString();
}
clause = string.Format(conditionClause, paramNames);
}
if (!clause.Trim().StartsWith( "and ")) clause = " and " + clause;
}
SqlCommand cmd = new SqlCommand();
string sql = "select "+fields+ " from "+tables+ " where 1=1 " + clause;
cmd.CommandText = sql;
SqlParameterCollection paramCollection = cmd.Parameters;
if (clause != string.Empty)
{
for (int j = 0; j < paramCount; j++)
{
paramCollection.Add(new SqlParameter(paramNames[j], SqlDbType.NChar));
if (parameters[j] != null)
{
paramCollection[paramNames[j]].Value = parameters[j];
}
else
{
paramCollection[paramNames[j]].Value = string.Empty;
}
}
}
return new DataHelper().GetTable(cmd);
}
按下面一种就可以了,很怪的问题哦
public DataTable GetLikeList(string tables,string fields,string text,string status)
{
SqlCommand cmd = new SqlCommand();
string sql= "select "+fields+ " from "+tables+ " where ([title] like @param0 or [content] like @param0) and status=@param1 ";
cmd.CommandText=sql;
SqlParameterCollection paramCollection = cmd.Parameters;
paramCollection.Add(new SqlParameter( "@param0 ",SqlDbType.NVarChar));
paramCollection.Add(new SqlParameter( "@param1 ",SqlDbType.NVarChar));
paramCollection[ "@param0 "].Value=text;
paramCollection[ "@param1 "].Value=status;
return new DataHelper().GetTable(cmd);
}
在线等,急用
------解决方案--------------------加个断点看看 你的 第一种方法 到底把 sql 拼成什么样了。
------解决方案--------------------like 应该是 like '%xxx% '
------解决方案--------------------楼上正解
------解决方案--------------------like查询时conditionClause传了个什么值?
------解决方案--------------------string sql = "select "+fields+ " from "+tables+ " where 1=1 " + clause;