首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

附近有异常

2011-12-30 
附近有错误stringstrconnserverlocalhostuidsapwdsadatabase购物 SqlConnectioncnnewSqlConne

附近有错误
string   strconn   =   "server=localhost;uid=sa;pwd=sa;database=购物 ";
                SqlConnection   cn   =   new   SqlConnection(strconn);
                string   mysql   =   "select   *   from   Product,Price   where   Product.PID=Price.PPID ";
                if   (ddl_pgroup.SelectedIndex.ToString()   !=   "0 ")
                {
                        mysql   =   mysql   +   "and   pgroup= "   +   Convert.ToInt16(ddl_pgroup.SelectedIndex.ToString());
                }
                if   (tbx_pid.Text.ToString()   !=   " ")
                {
                        mysql   =   mysql   +   "and   PID= "   +   Convert.ToInt16(tbx_pid.Text.ToString());
                }
               
                if   (tbx_pname.Text.ToString()!= " ")  
                {
                        mysql   =   mysql   +   "and   pname   like '% "   +   tbx_pname.Text.ToString()   +   "% ' ";
                }
                if   (tbx_pvender.Text.ToString()   !=   " ")
                {
                        mysql   =   mysql   +   "and   pvender   like   '% "   +   tbx_pvender.Text.ToString()   +   "% ' ";
                }
               
                SqlDataAdapter   da   =   new   SqlDataAdapter(mysql,   cn);
                DataSet   ds   =   new   DataSet();
                da.Fill(ds);
                dgd_plist.DataSource   =   ds;
                dgd_plist.DataBind();
                cn.Close();
这是一段搜索的代码,但是每次运行的时候,只要用任何的内容来搜索总是出现错误。比如用pname来搜索,就出现“ 'pname '   附近有语法错误”的问题,哪位高手能指点一下啊,谢谢

[解决办法]
string mysql = "select * from Product,Price where Product.PID=Price.PPID ";
==========================================================================
string mysql = "select * from Product,Price where Product.PID=Price.PPID ";

后面先加一个空格,否则拼接肯定错误

[解决办法]


mysql = mysql + "and pgroup= " + Convert.ToInt16(ddl_pgroup.SelectedIndex.ToString());
mysql = mysql + "and PID= " + Convert.ToInt16(tbx_pid.Text.ToString());
mysql = mysql + "and pname like '% " + tbx_pname.Text.ToString() + "% ' ";
mysql = mysql + "and pvender like '% " + tbx_pvender.Text.ToString() + "% ' ";

这四条语句在and前面都加个空格

[解决办法]
mysql = mysql + "and pname like '% " + tbx_pname.Text.ToString() + "% ' ";
======================================================================

like和 '之间加个空格

错误估计就这么多了吧,不过还有地方写的不是很好...
[解决办法]
顶LS上的,拼接SQL语句时,空格一定要注意!
[解决办法]
string strconn = "server=localhost;uid=sa;pwd=sa;database=购物 ";
SqlConnection cn = new SqlConnection(strconn);
string mysql = "select * from Product,Price where Product.PID=Price.PPID ";
if (ddl_pgroup.SelectedIndex.ToString() != "0 ")
{
mysql += " and pgroup= " + Convert.ToInt16(ddl_pgroup.SelectedIndex.ToString());
}
if (tbx_pid.Text.ToString() != " ")
{
mysql += " and PID= " + Convert.ToInt16(tbx_pid.Text.ToString());
}

if (tbx_pname.Text.ToString()!= " ")
{
mysql += " and pname like '% " + tbx_pname.Text.ToString() + "% ' ";
}
if (tbx_pvender.Text.ToString() != " ")
{
mysql += " and pvender like '% " + tbx_pvender.Text.ToString() + "% ' ";
}

SqlDataAdapter da = new SqlDataAdapter(mysql, cn);
DataSet ds = new DataSet();
da.Fill(ds);
dgd_plist.DataSource = ds;
dgd_plist.DataBind();
cn.Close();

[解决办法]
把 SQL 语句输出看一下,一切就都明白了呀!

热点排行