public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)        {
            this.bind(); 
        }
    }    public SqlConnection GetConnection()
    {      
        string mystr = ConfigurationManager.ConnectionStrings["db_09ConnectionString"].ConnectionString;        
        SqlConnection myconn =new SqlConnection(mystr) ;        return myconn;
    }    public void bind()
    {
        SqlConnection myconn = GetConnection();
        myconn.Open();
        string strSql = "select * from tb_Student";
        SqlDataAdapter myDa = new SqlDataAdapter(strSql, myconn);
        DataSet myDs =new DataSet();
        myDa.Fill(myDs);
        GridView1.DataSource=myDs;
        GridView1 .DataBind ();
        myDa .Dispose();
        myDs .Dispose();
        myconn.Dispose();
      
    
    
    }   
    protected void btnSelect_Click(object sender, EventArgs e)
    {        if (this.txtName.Text !="")
        {
            SqlConnection myconn = GetConnection();            myconn.Open();
            string sqlStr = "select * from tb_Student wherer Name =@Name";
            SqlCommand myCmd = new SqlCommand(sqlStr,myconn);            myCmd.Parameters.Add("@Name", SqlDbType.VarChar, 20).Value = this.txtName.Text.Trim();
            //myCmd.Parameters.Add("@Name", SqlDbType.VarChar, 20).Value = this.txtName.Text.Trim();
            //myCmd.Parameters.Add("@Name", SqlDbType.VarChar, 20).Value = this.txtName.Text.Trim();            SqlDataAdapter myDa = new SqlDataAdapter(myCmd);
            DataSet myDs = new DataSet();
            myDa.Fill(myDs);
            if (myDs.Tables[0].Rows.Count > 0)
            {
                GridView1.DataSource = myDs;
                GridView1.DataBind();            }            else
            {
                Response.Write("<script>alert ('没有相关记录')</script>");
            }
            myDa.Dispose();
            myDs.Dispose();
            myconn.Dispose();        }        else            this.bind();    }
}//错误显示在 : myDa.Fill(myDs); 用户代码未处理sqlexception, 'Name' 附近有语法错误。谢谢

解决方案 »

  1.   

    字段 这么 写 select * from tb_Student wherer [Name] =@Name
      

  2.   

    name加上[]name会和Sql自带的冲突
      

  3.   

    sql 语句 
    where 写错了
      

  4.   

    这些敏感的关键字最好不要用!!!再加上几个字符StudentName多好呀!自己也看的明白!!
      

  5.   

    关键字 name 应该SQL的 标记上[]
      

  6.   

    string sqlStr = "select * from tb_Student wherer [Name] =@Name";这里还有一个问题!!
      

  7.   

    select * from tb_Student wherer Name =@Name
    where写错了
      

  8.   

    string sqlStr = "select * from tb_Student where [Name] =@Name";
      

  9.   

    name在sql是关键字,要加上[Name]
      

  10.   

    string sqlStr = "select * from tb_Student wherer [Name] =@Name";
    Name 关键字
      

  11.   

    where
      

  12.   

    Name是一个关建字 [Name] 这样写就行了
      

  13.   

    楼主,你的where关键字写错了,多了一个r
      

  14.   

    NAME可以不定义的 就直接这样上都可以用
      

  15.   

    "select * from tb_Student wherer Name =@Name"; 帅哥  是where
      

  16.   

    string sqlStr = "select * from tb_Student wherer Name =@Name";
    把wherer改成where就行了
      

  17.   

    string sqlStr = "select * from tb_Student wherer Name =@Name";where关键字用[]括起来。
      

  18.   

    select * from tb_Student wherer [Name] =@Namewhere 多加了一个 r
      

  19.   

    第一、将Name最好写成[Name];
    第二、将wherer写成where。
      

  20.   

    不要把关键字当做字段名啊,name是关键字,如果非得用的话,要加上[]
      

  21.   

    sql 语句  
    where 写错了
      

  22.   

    wherer  写错了,应该是 where 
      

  23.   


    select * from tb_Student where ......
    -_________-!!!
      

  24.   

    你用的事Access数据库吧需要注意关键字,name是关键字,需要[name]这样,才不会出错
      

  25.   

    select * from tb_Student where [Name] =@Name+2
      

  26.   

     string sqlStr = "select * from tb_Student wherer Name =@Name";where 关键字写错了。。
      

  27.   

    系统都提示sqlexception~~就从SQL代码里面找错误嘛……WHERE写错了……还有name之类的关键字最好加[] 以区分……
      

  28.   

    string sqlStr = "select * from tb_Student wherer Name =@Name";
      

  29.   

    select * from tb_Student wherer Name =@Name
    sql语句错了,你实在发现不了错误的话,就将sql语句在查询分析器里运行一下,运行无误了,再写到程序里
      

  30.   

    是where关键字写错了,改下就行了