String MySQLConnectionString = global::MyPersonnel.Properties.Settings.Default.MyPersonnelConnectionString;
            SqlConnection MyConnection = new SqlConnection(MySQLConnectionString);
            MyConnection.Open();
            My员工编号 = this.基本档案DataGridView.CurrentRow.Cells[1].Value.ToString();
            string MySql = "select count(*) from @表名 where 员工编号= '" + My员工编号 + "'";
            SqlCommand MyCommand = MyConnection.CreateCommand();
            MyCommand.CommandText = MySql;
            MyCommand.Parameters.Add(new SqlParameter("@表名", SqlDbType.VarChar));            //运行到下面这行提示错误,没有给 @表名 赋值
            MyCommand.Parameters["@表名"].Value = "工资发放表";         //赋值为一个表的名字
            工资发放表Result=Convert.ToInt32(MyCommand.ExecuteScalar());//计算能否查到结果             MyCommand.Parameters["@表名"].Value = "工资核算表";           //赋值为另一个表的名字
            工资核算表Result = Convert.ToInt32(MyCommand.ExecuteScalar());//再次计算能否查到结果
以上错误怎么改正呢?高人指点,这个写法应该没错误的吧

解决方案 »

  1.   

    没有给 @表名 赋值
    MyCommand.Parameters.Add(new SqlParameter("@表名", SqlDbType.VarChar));
    你现在只是给它添加了这个参数,但你没有复制value。
    new SqlParameter("@表名", SqlDbType.VarChar)
      

  2.   

    “@表名”应该是个具体的table名,怎么还用@符号标示呢 "select count(*) from @表名 where 员工编号= '" + My员工编号 + "'";=〉"select count(*) from "+tablename+" where 员工编号= '" + My员工编号 + "'";下面的MyCommand.Parameters.Add(new SqlParameter("@表名", SqlDbType.VarChar));
    旧不用了,到时候直接给tablename 复制即可。
    @符号这样用"select count(*) from "+tablename+" where 员工编号= @employeename";
     MyCommand.Parameters.Add(new SqlParameter("@employeename", SqlDbType.VarChar));
      

  3.   

    MyCommand.Parameters["@表名"].Value = "工资发放表"; 
    这样就是赋值啊,不对吗?
      

  4.   

    SqlParameter parm3 = new SqlParameter("@表名", SqlDbType.VarChar);
    parm3.Value = argFBCID;
      

  5.   

    哦,你的意思是只能给where后的条件值使用@标志参数,而其它表名或字段名都不可以使用这个方法吧?
    我现在只是要不断变化表名执行这个查询,直接用一般的变量就应该就可以了吧,马上试下....
      

  6.   

    SqlParameter 类 表示 SqlCommand 的参数,一个SqlCommand的参数指的是where子句的查询条件名称,不能将表明作为参数的。