当前位置: 代码迷 >> ASP.NET >> 给数据库操作传参数失误
  详细解决方案

给数据库操作传参数失误

热度:2348   发布时间:2013-02-25 00:00:00.0
给数据库操作传参数出错
C# code
  protected void btn_seek_Click(object sender, EventArgs e)        {             using (SqlConnection conn1 = new SqlConnection(sqlstr))                {                   conn1.Open();  //等会做实验看看这个open是不是必须的                   string comtxt = "select * from t_student @select_str";                   using (SqlCommand comm1 = new SqlCommand(comtxt, conn1))                   {                            comm1.Parameters.AddWithValue("@select_str", this.TextBox_select_str.Text);                         SqlDataAdapter myadapter = new SqlDataAdapter(comm1);                         DataTable ds2 = new DataTable();                         myadapter.Fill(ds2);                         GridView1.DataSource = ds2;                         GridView1.DataBind();                     }                }                      } 


前台代码:
 
HTML code
 <asp:TextBox ID="TextBox_select_str" runat="server"></asp:TextBox>&nbsp;&nbsp;        <asp:Button ID="btn_seek" runat="server" Text="查询" onclick="btn_seek_Click" />...



  总是提示错误: [size=24px][/size] 
  '@select_str' 附近有语法错误。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.Data.SqlClient.SqlException: '@select_str' 附近有语法错误。

源错误: 


行 63: SqlDataAdapter myadapter = new SqlDataAdapter(comm1);
行 64: DataTable ds2 = new DataTable();
行 65: myadapter.Fill(ds2);[size=18px][/size]行 66: GridView1.DataSource = ds2;
行 67: GridView1.DataBind();
 


------解决方案--------------------------------------------------------
检查一下这里:
comm1.Parameters.AddWithValue("@selectstr", this.TextBox_select_str.Text.Trim() );


看看this.TextBox_select_str.Text.Trim() 这个取到值了没有


------解决方案--------------------------------------------------------
SqlDataAdapter myadapter = new SqlDataAdapter(comm1);
在这行代码处设断点,检查一下comm1这里的值,看看对不对。

方便的话请楼主把代码贴完整
------解决方案--------------------------------------------------------
参数不可以这么传吧?

要不:
select * from t_student where id=@selectstr
comm1.Parameters.AddWithValue("@selectstr", this.TextBox_select_str.Text);

要不:(写个方法传进去)
 public DataTable GetList(string strWhere)
{
......
..........
StringBuilder strSql = new StringBuilder();
strSql.append("SELECT * FROM dbo.Student");
if (strWhere.Trim() != "")
{
strSql.Append(" " + strWhere);
}

.......
}
------解决方案--------------------------------------------------------
参数不是那样传的,楼上的建议正解 要用到Parameters集合,在写一个方法去处理
------解决方案--------------------------------------------------------
探讨
我稍微修改了一下代码,调试一下,是这样的情况:

C# code
protected void btn_seek_Click(object sender, EventArgs e)
{ Label7.Text = this.TextBox_select_str.Text;
string commstr2 = this.TextBox_……

------解决方案--------------------------------------------------------
大哥首先你这逻辑就有问题, comm1.CommandText = "select * from t_student @selectstr"; 中的@selectstr最终的值会是一个常量字符串了,导致你这句话语法错误,你要构造动态查询就你直接 comm1.CommandText = "select * from t_student" + commstr2 就行了。。。comm1.Parameters.AddWithValue("@selectstr", this.TextBox_select_str.Text )这个是将传人的值转换为常量字符串了不会作为查询语法的一部分了
  相关解决方案