大家帮我看一下,为什么老提示 'Name' 附近有语法错误。
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' 附近有语法错误。
谢谢