我这里有个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' 与查询中所用的表名或别名不匹配。 希望高手指教
MassageTable 这个表名可能错了,你仔细检查一下,看chat1数据库中是否有这表
sdad.SelectCommand = new SqlCommand("select * from MassageTable where ReceiverID=" + comboBox2.SelectedValue, cnn); -------------- sdad.SelectCommand = new SqlCommand("select * from MassageTable where ReceiverID=\"" + comboBox2.SelectedValue + "\"", cnn);
trysdad.SelectCommand = new SqlCommand("select * from MassageTable where ReceiverID='" + comboBox2.SelectedItem.ToString() + "'", cnn);
sdad.SelectCommand = new SqlCommand("select * from MassageTable where ReceiverID= " + comboBox1.SelectedValue + "", cnn);
sdad.SelectCommand = new SqlCommand("select * from MassageTable where ReceiverID=" + comboBox2.SelectedValue, cnn); 这上面的取值有问题,既comboBox2.SelectedValue取不到相对应的值,comboBox2.SelectedValue取不到相对应的值是因为绑定comboBox2的时候设置不全,确保绑定的时候comboBox2.DisplayMember以及comboBox2.ValueMember都绑定了值,且保证绑定的数据源里有相对应的字段
各位大哥,下拉表的数据绑定是这样的: 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是不同表中想关联的项
{
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' 与查询中所用的表名或别名不匹配。
希望高手指教
--------------
sdad.SelectCommand = new SqlCommand("select * from MassageTable where ReceiverID=\"" + comboBox2.SelectedValue + "\"", cnn);
System.Data.DataRowView
这样的值,这样就会把'System.Data' 认为是一个表的别名了,所以才会报这样的错断点跟踪下看是否取到正确的值了也可以先用一个变量来接收这个值,然后做下判断再去数据库里查询
打印comboBox2.SelectedValue看是什么
把comboBox2.SelectedValue.ToString()打印出来看看
八成是它的问题
comboBox2.ValueMember = "你的ReceiverID获得数据的字段名字";
这上面的取值有问题,既comboBox2.SelectedValue取不到相对应的值,comboBox2.SelectedValue取不到相对应的值是因为绑定comboBox2的时候设置不全,确保绑定的时候comboBox2.DisplayMember以及comboBox2.ValueMember都绑定了值,且保证绑定的数据源里有相对应的字段
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是不同表中想关联的项
string id = rowView.Row["userID"].ToString();
string name = rowView.Row["userName"].ToString();
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();
或改成其它名字
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];
comboBox2.DisplayMember = "userName";
comboBox2.ValueMember = "userID";执行不到
this.comboBox2.DataSource = sdr.Tables["userInfo"];
这一步时,sdr.Tables["userInfo"]的内容
数据库userTable1表中是否有“userName”和“userID”字段select userID,userName from userTable1 在查询分析器中执行结果如何
{
//
// 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();
}
{
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();这些语句没有必要