大家帮我看一下,为什么老提示 'Name' 附近有语法错误。

tianya_best 2010-08-15 02:27:38
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' 附近有语法错误。

谢谢
...全文
878 41 打赏 收藏 转发到动态 举报
写回复
用AI写文章
41 条回复
切换为时间正序
请发表友善的回复…
发表回复
脾气不坏 2010-08-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 huwei12345 的回复:]

字段 这么 写 select * from tb_Student wherer [Name] =@Name
[/Quote]
+1
周煜皓 2010-08-16
  • 打赏
  • 举报
回复
string sqlStr = "select * from tb_Student wherer Name =@Name";

where 关键字写错了。。
elf512 2010-08-16
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 awayy1432 的回复:]
引用 1 楼 huwei12345 的回复:

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

+1
[/Quote]
select * from tb_Student where [Name] =@Name

+2
hu_86727515 2010-08-16
  • 打赏
  • 举报
回复
[Name]
亚丁号 2010-08-16
  • 打赏
  • 举报
回复
看来这次大家意见比较统一
loveheronly 2010-08-16
  • 打赏
  • 举报
回复
你用的事Access数据库吧。。。需要注意关键字,name是关键字,需要[name]这样,才不会出错
伴老思源 2010-08-16
  • 打赏
  • 举报
回复
[Quote=引用楼主 tianya_best 的回复:]
string sqlStr = "select * from tb_Student wherer Name =@Name";
[/Quote]

select * from tb_Student where ......


-_________-!!!
chinawes 2010-08-16
  • 打赏
  • 举报
回复
wherer 写错了,应该是 where
dengxiao1981 2010-08-16
  • 打赏
  • 举报
回复
sql 语句
where 写错了
generhappy 2010-08-16
  • 打赏
  • 举报
回复
不要把关键字当做字段名啊,name是关键字,如果非得用的话,要加上[]
kingcsx666 2010-08-16
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 aganar 的回复:]
第一、将Name最好写成[Name];
第二、将wherer写成where。
[/Quote].
stepday 2010-08-16
  • 打赏
  • 举报
回复
第一、将Name最好写成[Name];
第二、将wherer写成where。
xz5211314_0 2010-08-16
  • 打赏
  • 举报
回复
wherer改成where就行了
qiandongwu 2010-08-16
  • 打赏
  • 举报
回复
是where关键字写错了,改下就行了
zhangyumei 2010-08-16
  • 打赏
  • 举报
回复
select * from tb_Student wherer Name =@Name
sql语句错了,你实在发现不了错误的话,就将sql语句在查询分析器里运行一下,运行无误了,再写到程序里
booth.chen 2010-08-16
  • 打赏
  • 举报
回复
string sqlStr = "select * from tb_Student wherer Name =@Name";
publicxiao 2010-08-16
  • 打赏
  • 举报
回复
[Quote=引用 36 楼 baysos 的回复:]
系统都提示sqlexception~~就从SQL代码里面找错误嘛……WHERE写错了……还有name之类的关键字最好加[] 以区分……
[/Quote]+1
baysos 2010-08-16
  • 打赏
  • 举报
回复
系统都提示sqlexception~~就从SQL代码里面找错误嘛……WHERE写错了……还有name之类的关键字最好加[] 以区分……
honglangzhu 2010-08-16
  • 打赏
  • 举报
回复
[Quote=引用 32 楼 hu_86727515 的回复:]
[Name]
[/Quote]
+3
Dearxun 2010-08-15
  • 打赏
  • 举报
回复
路过,学习
加载更多回复(21)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧