/// <summary>
/// 获取省份
/// </summary>
/// <param name="strWhere"></param>
/// <returns></returns>
public DataSet GetWeiyiPid(string strWhere)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select * from (select ProId,count(ProId) as x from Collaborate ");
if (strWhere.Trim() != "")
{
strSql.Append(" where " + strWhere);
}
return DBHelper.Query(strSql.ToString());
} #endregion
private void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataBindProvince();
if (Request.QueryString["ProId"] == null || Request.QueryString["ProId"] == "")
{
string strSql = " group by ProId having count(ProId)>=1) c order by x desc ";
BindFridensLink(strSql); }
else
{
string ProId = Request.QueryString["ProId"].ToString();
string strSql = " 1 =1 and ProId ='" + ProId + "' group by ProId having count(ProId)>=1) c order by x desc ";
BindFridensLink(strSql);
}
} }
#endregion
请注意红色部分!错误就在那里! 空格我空了 那语句我在SQl2005用是可以的 你们看看是不是拼接错误啊!
'Collaborate' 附近有语法错误。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Exception: 'Collaborate' 附近有语法错误。源错误:
行 586: catch (System.Data.SqlClient.SqlException ex)
行 587: {
行 588: throw new Exception(ex.Message);
行 589: }
行 590: finally
/// 获取省份
/// </summary>
/// <param name="strWhere"></param>
/// <returns></returns>
public DataSet GetWeiyiPid(string strWhere)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select * from (select ProId,count(ProId) as x from Collaborate ");
if (strWhere.Trim() != "")
{
strSql.Append(" where " + strWhere);
}
return DBHelper.Query(strSql.ToString());
} #endregion
private void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataBindProvince();
if (Request.QueryString["ProId"] == null || Request.QueryString["ProId"] == "")
{
string strSql = " group by ProId having count(ProId)>=1) c order by x desc ";
BindFridensLink(strSql); }
else
{
string ProId = Request.QueryString["ProId"].ToString();
string strSql = " 1 =1 and ProId ='" + ProId + "' group by ProId having count(ProId)>=1) c order by x desc ";
BindFridensLink(strSql);
}
} }
#endregion
请注意红色部分!错误就在那里! 空格我空了 那语句我在SQl2005用是可以的 你们看看是不是拼接错误啊!
'Collaborate' 附近有语法错误。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Exception: 'Collaborate' 附近有语法错误。源错误:
行 586: catch (System.Data.SqlClient.SqlException ex)
行 587: {
行 588: throw new Exception(ex.Message);
行 589: }
行 590: finally
你的Collaborate后面是不是少了个括号
改成这样:select * from (select ProId,count(ProId) as x from Collaborate )as a where +"where;
strSql.Append("select * from (select ProId,count(ProId) as x from Collaborate ");
string strSql = " group by ProId having count(ProId)>=1) c order by x desc ";这两句连接起来就差个括号了
在最后一个strSql 这里打断点。取出sql语句到 数据库执行看看,是不是sql的问题。
public DataSet GetWeiyiPid(string strWhere)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select * from (select ProId,count(ProId) as x from Collaborate ");
if (strWhere.Trim() != "")
{
strSql.Append(" where " + strWhere);
}
return DBHelper.Query(strSql.ToString());
}
单看这一个方法strSql为"select * from (select ProId,count(ProId) as x from Collaborate where 条件"就不对了
group by ProId
having count(ProId)>=1) c order by x desc
这是我的Sql查询语句 这样少个括号是可以的 一加上就错!
这个方法没有通过/// 获取省份
/// </summary>
/// <param name="strWhere"></param>
/// <returns></returns>
public DataSet GetWeiyiPid(string strWhere)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select * from (select ProId,count(ProId) as x from Collaborate ");
if (strWhere.Trim() != "")
{
strSql.Append(" where " + strWhere);
}
return DBHelper.Query(strSql.ToString());
} #endregion
就报上面的那个错误了!加) 也错误!
group by ProId
having count(ProId)>=1 order by x desc 测试成功~~~~
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select * from (select ProId,count(ProId) as x from Collaborate ");
if (strWhere.Trim() != "")
{
strSql.Append(" where " + strWhere);
}
return DBHelper.Query(strSql.ToString());
}
这个方法本身就很多毛病
1)把strSql.Append(" where " + strWhere);改为
strSql.Append(" where " + strWhere+" )as c");
2)如果程序不走if (strWhere.Trim() != "")直接else掉,strsql则变为select * from (select ProId,count(ProId) as x from Collaborate 所以要加else{ strSql.Append(" )as c");}
后面private void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataBindProvince();
if (Request.QueryString["ProId"] == null || Request.QueryString["ProId"] == "")
{
string strSql = " group by ProId having count(ProId)>=1) c order by x desc ";
BindFridensLink(strSql); }
else
{
string ProId = Request.QueryString["ProId"].ToString();
string strSql = " 1 =1 and ProId ='" + ProId + "' group by ProId having count(ProId)>=1) c order by x desc ";
BindFridensLink(strSql);
}
} }
红色那个方法看里面的sql语句对不对
select * from (select ProId,count(ProId) as x
这里面的'(',和')'得转义,用\转义字符转义
怎么转啊!Append("select * from (select ProId\count(ProId) as x from 这样写上 直接就错!
group by ProId
having count(ProId)>=1) c order by x desc
你改一下你的Sql你看卡卡你行不! 加上) 不对了!
你换成这样看看 select * from where 1=1 and 拼接的条件 group by
/// 获取省份
/// </summary>
/// <param name="strWhere"></param>
/// <returns></returns>
public DataSet GetWeiyiPid(string strWhere)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select * from (select ProId,count(ProId) as x from Collaborate ");
if (strWhere.Trim() != "")
{
strSql.Append(strWhere);
}
return DBHelper.Query(strSql.ToString());
} #endregion
private void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataBindProvince();
if (Request.QueryString["ProId"] == null || Request.QueryString["ProId"] == "")
{
string strSql = " group by ProId having count(ProId)>=1) c order by x desc ";
BindFridensLink(strSql); }
else
{
string ProId = Request.QueryString["ProId"].ToString();
string strSql = " where 1 =1 and ProId ='" + ProId + "' group by ProId having count(ProId)>=1) c order by x desc ";
BindFridensLink(strSql);
}
} }
#endregion
你换成这样看看 select * from where 1=1 and 拼接的条件 group by因为你的条件写的是 != "" 不是 !equals("") ,为""的时候引用不一样也会拼接那个where
select * from (select ProId,count(ProId) as x from Collaborate
group by ProId
having count(ProId)>=1 )as c order by x desc
这是拼接的()!
你换成这样看看 select * from where 1=1 and 拼接的条件 group by因为你的条件写的是 != "" 不是 !equals("") ,为""的时候引用不一样也会拼接那个where
ps:为什么用美女头像提问的回答的人这么多
那个方法没过去!就说Collaborate 附近有语法错误!
呜呜呜!DBHeper没有过去啊!
strWhere的值,和strsql的值
把这2个值贴出来
select * from (select ProId,count(ProId) as x from Collaborate
group by ProId
having count(ProId)>=1 )as c order by x descsql语句没问题 测试通过!
问题应该出在那个 strSql.Append(" where " + strWhere);
里的“where”上,
建议楼主单步跟踪 看下return DBHelper.Query(strSql.ToString()); 这里的strSql的值 放在数据库里执行一下。
然后再短点调试,问题不大,一打断点就可以出来了
if (Request.QueryString["ProId"] == null || Request.QueryString["ProId"] == "")
{
string strSql = " group by ProId having count(ProId)>=1) c order by x desc ";
这句
if (Request.QueryString["ProId"] == null || Request.QueryString["ProId"] == "")
结果为false 结果不走里面的语句了。所以没有拼接上
然后你的SQL就不对了
Sql语句是对的 现在就是程序问题!拼接问题!以前没有排序我是对的!现在加上排序就不行了!
strSql.Append("select * from (select ProId,count(ProId) as x from Collaborate ");
string strSql = " group by ProId having count(ProId)>=1) c order by x desc ";
这两句,改为
strSql.Append("select ProId,count(ProId) as x from Collaborate ");
string strSql = " group by ProId having count(ProId)>=1 order by x desc ";
如果还不行,那你就中大奖了
换上你的!“/ArthomePro”应用程序中的服务器错误。
--------------------------------------------------------------------------------选择列表中的列 'Collaborate.ProId' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Exception: 选择列表中的列 'Collaborate.ProId' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中。源错误:
行 586: catch (System.Data.SqlClient.SqlException ex)
行 587: {
行 588: throw new Exception(ex.Message);
行 589: }
行 590: finally
提示一下LZ,在SQL语句时,一定要看清!