日期:2014-05-19  浏览次数:20909 次

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

------解决方案--------------------
如果字段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,用一个,使用完关闭就可以
------解决方案--------------------
接上:
4.SqlDataAdapter 可以自动打开关闭数据库,所以cnn.Close();这些语句没有必要