目的:查询DATAGRID当前表中的序号为1所有记录;_________________________________________
private void button1_Click(object sender, System.EventArgs e)
{
string tablestr;
SqlConnection thisconnection=new SqlConnection("Server=(local);Integrated Security=SSPI;"+ "Initial Catalog=aohome");
  tablestr=this.dataGrid1.DataMember.ToString();
         this.sqlSelectCommand1.CommandText="select * from" +tablestr+"where 序号='1'";
this.dataSet11.Clear();
this.sqlDataAdapter1.Fill(this.dataSet11,tablestr);
                  dataGrid1.SetDataBinding(this.dataSet11,tablestr);

}_____________________________________
出错信息:未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。其他信息: 系统错误。错误定位语句:this.sqlDataAdapter1.Fill(this.dataSet11,tablestr);
________________
调试1:this.sqlSelectCommand1.CommandText="select * from "+tablestr+" order by 序号 "+sortstr;可以对数据库排序,没报错.
调试2:this.sqlSelectCommand1.CommandText="select 序号 from" +tablestr;
仍然报错.

解决方案 »

  1.   

    检查tablestr的值,是不是和表名一样
      

  2.   

    " +tablestr+"SQL语句里面少空格
      

  3.   

    修改:
    this.sqlSelectCommand1.CommandText="select 序号 from" +tablestr;为:
    this.sqlSelectCommand1.CommandText="select 序号 from " +tablestr;少了一个空格
      

  4.   

    tablestr可以获取当前的表名.
    调试1:this.sqlSelectCommand1.CommandText="select * from "+tablestr+" order by 序号 "+sortstr;可以对数据库排序,没报错.
      

  5.   

    但是调试2:this.sqlSelectCommand1.CommandText="select 序号 from" +tablestr;
    仍然报错.