列前缀 'System.Data' 与查询中所用的表名或别名不匹配。

解决方案 »

  1.   

    我这里有个combobox的下拉表,想实现的功能是:选择其中一项,然后在datagridview中显示内容,我双击combobox进行编码:        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
            {
                SqlConnection cnn = new SqlConnection("server=127.0.0.1;database=chat1;uid=sa;pwd=;");
                SqlDataAdapter sdad = new SqlDataAdapter();
                sdad.SelectCommand = new SqlCommand("select * from MassageTable where ReceiverID=" + comboBox2.SelectedValue, cnn);
                DataSet dse = new DataSet();
                sdad.Fill(dse, "nnn");
                this.dataGridView3.DataSource = dse.Tables["nnn"];
                cnn.Close();
            }
    显示的错误是:列前缀 'System.Data' 与查询中所用的表名或别名不匹配。
    希望高手指教
      

  2.   

    MassageTable 这个表名可能错了,你仔细检查一下,看chat1数据库中是否有这表
      

  3.   

    sdad.SelectCommand = new SqlCommand("select * from MassageTable where ReceiverID=" + comboBox2.SelectedValue, cnn);
    --------------
     sdad.SelectCommand = new SqlCommand("select * from MassageTable where ReceiverID=\"" + comboBox2.SelectedValue + "\"", cnn);
      

  4.   

    trysdad.SelectCommand = new SqlCommand("select * from MassageTable where ReceiverID='" + comboBox2.SelectedItem.ToString() + "'", cnn);
      

  5.   

    sdad.SelectCommand = new SqlCommand("select * from MassageTable where ReceiverID= " + comboBox1.SelectedValue + "", cnn);
      

  6.   

    应该是你的comboBox2.SelectedValue取值有问题,取到的可能是
    System.Data.DataRowView
    这样的值,这样就会把'System.Data' 认为是一个表的别名了,所以才会报这样的错断点跟踪下看是否取到正确的值了也可以先用一个变量来接收这个值,然后做下判断再去数据库里查询
      

  7.   

    comboBox2绑定的时候没有指定Value字段的名字
      

  8.   

    应该是comboBox2绑定的时候出的问题,另外
    打印comboBox2.SelectedValue看是什么
      

  9.   

    构造SQL查询的那句话之前
    把comboBox2.SelectedValue.ToString()打印出来看看
    八成是它的问题
      

  10.   

    绑定的时候指定 ValueMember
      

  11.   

    大致是这样样子
    comboBox2.ValueMember = "你的ReceiverID获得数据的字段名字";
      

  12.   

    sdad.SelectCommand = new SqlCommand("select * from MassageTable where ReceiverID=" + comboBox2.SelectedValue, cnn);
    这上面的取值有问题,既comboBox2.SelectedValue取不到相对应的值,comboBox2.SelectedValue取不到相对应的值是因为绑定comboBox2的时候设置不全,确保绑定的时候comboBox2.DisplayMember以及comboBox2.ValueMember都绑定了值,且保证绑定的数据源里有相对应的字段
      

  13.   

    各位大哥,下拉表的数据绑定是这样的:
    SqlConnection cnn = new SqlConnection("server=127.0.0.1;database=chat1;uid=sa;pwd=;");
                SqlDataAdapter sdad = new SqlDataAdapter();
                sdad.SelectCommand = new SqlCommand("select * from userTable1", cnn);
                DataSet sdr = new DataSet();
                sdad.Fill(sdr, "userID");
                this.comboBox2.DataSource = sdr.Tables["userID"];
                comboBox2.DisplayMember = "userName";
                comboBox2.ValueMember = "userID";
                cnn.Close();
    其中userID和ReceiverID是不同表中想关联的项
      

  14.   

    lxcnn(过客) ,你提到的System.Data.DataRowView是正确的,可是还没有找到解决的办法
      

  15.   

    你可以先断点看一下sdr.Tables["userID"];的内容是否正确,如果你确定你的绑定数据源没问题,那这样得到选中行的数据DataRowView rowView = (DataRowView)comboBox2.SelectedItem;
    string id = rowView.Row["userID"].ToString();
    string name = rowView.Row["userName"].ToString();
      

  16.   

    从comboBox2.DisplayMember = "userName";开始就有问题了
      

  17.   

    sdad.Fill(sdr, "userID");
    this.comboBox2.DataSource = sdr.Tables["userID"];
    comboBox2.DisplayMember = "userName";
    comboBox2.ValueMember = "userID";
    cnn.Close();
    -------------->
    sdad.Fill(sdr, "userInfo");
    this.comboBox2.DataSource = sdr.Tables["userInfo"];
    comboBox2.DisplayMember = "userName";
    comboBox2.ValueMember = "userID";
    cnn.Close();
    或改成其它名字
      

  18.   

    参照这个
    myCommand = new SqlCommand("select * from userTable1", myConnection);
                    sdad = new SqlDataAdapter();
                    sdad .SelectCommand = myCommand;
                    DataSet sdr = new DataSet();                sdad .Fill(sdr );
                    comboBox2.DataSource = sdr .Tables[0];
                    comboBox2.DisplayMember = "userName";
                    comboBox2.ValueMember = "userID";================================================
    这里不要加表名
    sdad .Fill(m_dsInput);
    这里同样不要表名
    comboBox2.DataSource = sdad .Tables[0];
      

  19.   

    还是同样的问题哦 hyde100(愛抓兔子的貓!)
      

  20.   

    我改了,可是问题还是存在,
    comboBox2.DisplayMember = "userName";
    comboBox2.ValueMember = "userID";执行不到
      

  21.   

    数据库userTable1表中是否有“userName”和“userID”字段select * from userTable1 在查询分析器中执行结果如何断点跟踪查看一下到
    this.comboBox2.DataSource = sdr.Tables["userInfo"];
    这一步时,sdr.Tables["userInfo"]的内容
      

  22.   

    语句出错没有,“userName”和“userID”字段也存在,跟踪到comboBox2.DataSource = sdr .Tables[0];后面就不执行了,马上弹出错误了
      

  23.   

    列前缀 'System.Data' 与查询中所用的表名或别名不匹配。
      

  24.   

    对了,你们说我combobox和datagridview我刚才给的代码分别放在哪里,我有没有放错哦?
      

  25.   

    //lxcnn(过客) ( ) 
    数据库userTable1表中是否有“userName”和“userID”字段select userID,userName from userTable1 在查询分析器中执行结果如何
      

  26.   

    public FormMain()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();
    this.LV_OnlineUser.AfterLabelEdit +=new LabelEditEventHandler(LV_OnlineUser_AfterLabelEdit);
    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
                SqlConnection con = new SqlConnection("server=127.0.0.1;database=chat1;uid=sa;pwd=;");
                SqlDataAdapter sda = new SqlDataAdapter();
                sda.SelectCommand = new SqlCommand("select userID as 'ID',companyName as '公司名称',userAddress as '公司地址',userName as  '代理人' ,userSex as '性别 ',userIdentity as '职称' from userTable1", con);
                DataSet ds = new DataSet();
                sda.Fill(ds, "emp");
                this.dataGridView1.DataSource = ds.Tables["emp"];
                con.Close();
                //绑定选择的用户
                SqlConnection cnn = new SqlConnection("server=127.0.0.1;database=chat1;uid=sa;pwd=;");
                SqlDataAdapter sdad = new SqlDataAdapter();
                //sdad.SelectCommand = new SqlCommand("select * from userTable1", cnn);
                SqlCommand  myConmmand = new SqlCommand("select * from userTable1", cnn);
                //DataSet sdr = new DataSet();
                sdad = new SqlDataAdapter();
                sdad.SelectCommand = myConmmand;
                DataSet sdr = new DataSet();
                //sdad.Fill(sdr, "userInfo");
                sdad.Fill(sdr);
                this.comboBox2.DataSource = sdr.Tables[0];
                comboBox2.DisplayMember = "userName";
                comboBox2.ValueMember = "userID";
                cnn.Close(); }        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
            {
                SqlConnection cnn = new SqlConnection("server=127.0.0.1;database=chat1;uid=sa;pwd=;");
                SqlDataAdapter sdad = new SqlDataAdapter();
                //Value =comboBox2.SelectedValue.ToString();
                sdad.SelectCommand = new SqlCommand("select * from MassageTable where ReceiverID=" + comboBox2.SelectedValue, cnn);
                DataSet dse = new DataSet();
                sdad.Fill(dse, "aaa");
                this.dataGridView3.DataSource = dse.Tables["aaa"];
                cnn.Close();
            }
      

  27.   

    如果字段userID是int型的,这样改一下结果应该就对了private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
       DataRowView rowView = (DataRowView)comboBox2.SelectedItem;
       int id = Convert.ToInt32(rowView.Row["userID"]);            SqlConnection cnn = new SqlConnection("server=127.0.0.1;database=chat1;uid=sa;pwd=;");
                SqlDataAdapter sdad = new SqlDataAdapter();
                //Value =comboBox2.SelectedValue.ToString();
                sdad.SelectCommand = new SqlCommand("select * from MassageTable where ReceiverID=" + id, cnn);
                DataSet dse = new DataSet();
                sdad.Fill(dse, "aaa");
                this.dataGridView3.DataSource = dse.Tables["aaa"];
                cnn.Close();
    }如果是varchar类型的,改下相应位置代码
    string id = rowView.Row["userID"].ToString();sdad.SelectCommand = new SqlCommand("select * from MassageTable where ReceiverID='" + id +"'", cnn);
    还有几点是需要楼主注意的
    1、上面那些从SqlConnection con = new SqlConnection("server=127.0.0.1;database=chat1;uid=sa;pwd=;");
    开始的代码不要放在构造函数里,放在Form1_Load里
    2、因为窗体加载时就触发了comboBox2_SelectedIndexChanged事件,所以会报上面的错误
    3、同一事件里不用写两个SqlConnection,用一个,使用完关闭就可以
      

  28.   

    接上:
    4.SqlDataAdapter 可以自动打开关闭数据库,所以cnn.Close();这些语句没有必要
      

  29.   

    哈哈,谢谢大家了,问题解决了,lxcnn(过客)的方法,还要谢谢各位大哥,谢谢!!!!!!!!!!!!!!11