生成sql查询以将其传递给查询字符串时出现问题... [英] problem in generating the sql query to pass it to query string...

查看:85
本文介绍了生成sql查询以将其传递给查询字符串时出现问题...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在主页上进行登录,并将其传递给另一个页面的查询字符串..

我在生成查询时出错

我正在使用的逻辑如下..

i am trying to make a login in main page and pass it to a querystring to another page..

i am wrong in generating a query

the logic i am using is as below..

while (dr.Read())
{
   if(proid == String.Empty)
   {
      proid =  dr["ProductID"].ToString(); 
   }
   else
   {
      proid += "," + dr["ProductID"].ToString();
   }
}



当我在另一页上使用request.querystring校准查询时,即Productcatalog.aspx



and when i cal the query with request.querystring on another page i.e Productcatalog.aspx

string query = "Select * from Products where ProductID in ('" + Request.QueryString["ID"].ToString() +"') AND UnitCost < 5000";



但生成的查询如下所示



but the query generated is as below

query	"Select * from Products where ProductID= ',63,64,65' AND UnitCost < 5000"	string



我想将查询生成为



i want to generate the query as

Select * from Products where ProductID in (63,64,65) AND UnitCost < 5000



我必须在while循环中进行哪些更改.生成类似的查询.



what changes i have to make in above while loop. to generate the similar query.

推荐答案

循环的一种方法是
One way to do the loop is
while (dr.Read())
{
   proid = dr["ProductID"].ToString() + ", ";
}
proid.TrimEnd(' ', ',');


在更优雅(更快)的版本中,您可以使用StringBuilder.


In more elegant (and faster) version you could use StringBuilder.


尝试:

Try:

string query = "Select * from Products where ProductID in (''" + Request.QueryString["ID"].ToString().Substring(1) +"'') AND UnitCost < 5000";



问题出在以下语句中:



The problem lies in this statement:

proid += "," + dr["ProductID"].ToString();



您在返回的每个值之前都添加了逗号.子字符串将在第一个逗号之后包含所有内容.



You are putting a comma before every value that is returned. The substring will take everything after the first comma.


这篇关于生成sql查询以将其传递给查询字符串时出现问题...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆