"select * from dbo.Student where Student_id in(select Student_id from dbo.Student_Course where Course_id=(select Course_id from dbo.Course where Course_name ='"+ this.comboBox2.Text +"')) "; 这句话有问题吗?private void Form2_Load(object sender, EventArgs e)
{
string constr = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=学生系统;Data Source=(local)";
SqlConnection conn = new SqlConnection(constr);
DataSet ds = new DataSet();
try
{
string sqlstr = "select * from dbo.Users "
+ "select * FROM dbo.Student "
+ "select * from dbo.Course "
+ "select * from dbo.Student where Student_id in(select Student_id from dbo.Student_Course where Course_id=(select Course_id from dbo.Course where Course_name ='"+ this.comboBox2.Text +"')) ";
SqlDataAdapter da = new SqlDataAdapter(sqlstr, conn);
da.Fill(ds);
ds.Tables[0].TableName = "Users";
ds.Tables[1].TableName = "Student";
ds.Tables[2].TableName = "Course";
ds.Tables[3].TableName = "S_Course"; }
catch (SqlException oe)
{
MessageBox.Show(oe.Message, "连接数据库错误信息");
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
this.listBox1.DataSource = ds;//用户管理里面的控件的代码绑定
this.listBox1.DisplayMember = "Users.User_id";
//this.listBox1.DataBindings.Add("Text", ds, "Users.User_id");
//this.listBox1.ValueMember = "Users.User_id";
this.textBox1.DataBindings.Add("Text", ds, "Users.User_id");
this.textBox2.DataBindings.Add("Text", ds, "Users.User_password");
this.comboBox1.DataBindings.Add("Text", ds, "Users.User_power");
this.listBox2.DataSource = ds;//学生管理里面的控件的代码绑定
//this.listBox2.DataBindings.Add("Text", ds, "Student.Student_id");
this.listBox2.DisplayMember = "Student.Student_id";
this.textBox3.DataBindings.Add("Text", ds, "Student.Student_id");
this.textBox4.DataBindings.Add("Text", ds, "Student.Student_name");
this.textBox5.DataBindings.Add("Text", ds, "Student.Student_sex");
this.textBox6.DataBindings.Add("Text", ds, "Student.Student_nation");
this.textBox7.DataBindings.Add("Text", ds, "Student.Student_birthday");
this.textBox8.DataBindings.Add("Text", ds, "Student.Student_time");
this.textBox9.DataBindings.Add("Text", ds, "Student.Student_class");
this.textBox10.DataBindings.Add("Text", ds, "Student.Student_home");
this.richTextBox1.DataBindings.Add("Text", ds, "Student.Student_else");
this.listBox3.DataSource = ds;//课程管理里面的控件的代码绑定
this.listBox3.DisplayMember = "Course.Course_id";
this.textBox11.DataBindings.Add("Text", ds, "Course.Course_id");
this.textBox12.DataBindings.Add("Text", ds, "Course.Course_name");
this.textBox13.DataBindings.Add("Text", ds, "Course.Course_period");
this.textBox14.DataBindings.Add("Text", ds, "Course.Course_credit");
this.richTextBox2.DataBindings.Add("Text", ds, "Course.Course_describe");
this.comboBox2.DataSource = ds;
this.comboBox2.DisplayMember = "Course.Course_name";
this.comboBox2.ValueMember = "Course.Course_name";
this.comboBox2.Text=valu
this.listBox4.DataSource = ds;
this.listBox4.DisplayMember = "S_Course.Student_id"; //这里也无法绑定上去 label23.Text = "所有" + this.comboBox2.Text + "的学生ID";//为何这个没能显示comboBox2.Text 的内容呢? }
{
string constr = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=学生系统;Data Source=(local)";
SqlConnection conn = new SqlConnection(constr);
DataSet ds = new DataSet();
try
{
string sqlstr = "select * from dbo.Users "
+ "select * FROM dbo.Student "
+ "select * from dbo.Course "
+ "select * from dbo.Student where Student_id in(select Student_id from dbo.Student_Course where Course_id=(select Course_id from dbo.Course where Course_name ='"+ this.comboBox2.Text +"')) ";
SqlDataAdapter da = new SqlDataAdapter(sqlstr, conn);
da.Fill(ds);
ds.Tables[0].TableName = "Users";
ds.Tables[1].TableName = "Student";
ds.Tables[2].TableName = "Course";
ds.Tables[3].TableName = "S_Course"; }
catch (SqlException oe)
{
MessageBox.Show(oe.Message, "连接数据库错误信息");
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
this.listBox1.DataSource = ds;//用户管理里面的控件的代码绑定
this.listBox1.DisplayMember = "Users.User_id";
//this.listBox1.DataBindings.Add("Text", ds, "Users.User_id");
//this.listBox1.ValueMember = "Users.User_id";
this.textBox1.DataBindings.Add("Text", ds, "Users.User_id");
this.textBox2.DataBindings.Add("Text", ds, "Users.User_password");
this.comboBox1.DataBindings.Add("Text", ds, "Users.User_power");
this.listBox2.DataSource = ds;//学生管理里面的控件的代码绑定
//this.listBox2.DataBindings.Add("Text", ds, "Student.Student_id");
this.listBox2.DisplayMember = "Student.Student_id";
this.textBox3.DataBindings.Add("Text", ds, "Student.Student_id");
this.textBox4.DataBindings.Add("Text", ds, "Student.Student_name");
this.textBox5.DataBindings.Add("Text", ds, "Student.Student_sex");
this.textBox6.DataBindings.Add("Text", ds, "Student.Student_nation");
this.textBox7.DataBindings.Add("Text", ds, "Student.Student_birthday");
this.textBox8.DataBindings.Add("Text", ds, "Student.Student_time");
this.textBox9.DataBindings.Add("Text", ds, "Student.Student_class");
this.textBox10.DataBindings.Add("Text", ds, "Student.Student_home");
this.richTextBox1.DataBindings.Add("Text", ds, "Student.Student_else");
this.listBox3.DataSource = ds;//课程管理里面的控件的代码绑定
this.listBox3.DisplayMember = "Course.Course_id";
this.textBox11.DataBindings.Add("Text", ds, "Course.Course_id");
this.textBox12.DataBindings.Add("Text", ds, "Course.Course_name");
this.textBox13.DataBindings.Add("Text", ds, "Course.Course_period");
this.textBox14.DataBindings.Add("Text", ds, "Course.Course_credit");
this.richTextBox2.DataBindings.Add("Text", ds, "Course.Course_describe");
this.comboBox2.DataSource = ds;
this.comboBox2.DisplayMember = "Course.Course_name";
this.comboBox2.ValueMember = "Course.Course_name";
this.comboBox2.Text=valu
this.listBox4.DataSource = ds;
this.listBox4.DisplayMember = "S_Course.Student_id"; //这里也无法绑定上去 label23.Text = "所有" + this.comboBox2.Text + "的学生ID";//为何这个没能显示comboBox2.Text 的内容呢? }
如果 select Course_id from dbo.Course where Course_name ='"+ this.comboBox2.Text +"')) 卜唯一时就出错~
+ "select * FROM dbo.Student "
+ "select * from dbo.Course "
+ "select * from dbo.Student where Student_id in(select Student_id from dbo.Student_Course where Course_id=(select Course_id from dbo.Course where Course_name ='"+ this.comboBox2.Text +"')) "; 这个有问题。你似乎想一下返回你需要的几个集合。一般一个SQL语句只返回一个结果集合。你可以分开写成几个SQL语句,然后分别执行。string sqlstr1 = "select * from dbo.Users " ;
string sqlstr2= "select * FROM dbo.Student " ;
string sqlstr3= "select * from dbo.Course " ;
string sqlstr4="select * from dbo.Student where Student_id in(select Student_id from dbo.Student_Course where Course_id=(select Course_id from dbo.Course where Course_name ='"+ this.comboBox2.Text +"')) "; 分别使用SqlCommand对象执行上面的各个SQL语句,再返回结果。返回的结果可以放到一个DataSet中。比如:
da1.Fill(ds.Table[0]);
da2.Fill(ds.Table[1]);
.....
这句话,其实你可以放在sql2000 或sql2005 的查询分析器中检测一下,看看是不是你想要的结果,然后再做定夺。
SQL中就像以上这样是可以的呀,而且还有运行结果呢!!
为何这句也不能显示内容呢?这个应该不用什么连接吧!!
label23.Text = "所有" + this.comboBox2.Text + "的学生ID";//为何这个没能显示comboBox2.Text 的内容呢?
{
public partial class Form2 : Form
{
public DataSet ds;
public Form2()
{
InitializeComponent(); } private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
} private void button1_Click(object sender, EventArgs e)
{
Form1 F1 = new Form1();
F1.Show();
textBox1.Text = "";
textBox2.Text = "";
} private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
} private void Form2_Load(object sender, EventArgs e)
{
string constr = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=学生系统;Data Source=(local)";
SqlConnection conn = new SqlConnection(constr);
DataSet ds = new DataSet();
try
{
string sqlstr = "select * from dbo.Users "
+ "select * FROM dbo.Student "
+ "select * from dbo.Course ";
//+ "select * from dbo.Student where Student_id in(select Student_id from dbo.Student_Course where Course_id=(select Course_id from dbo.Course where Course_name ='"+ "数据库"+"')) ";
SqlDataAdapter da = new SqlDataAdapter(sqlstr, conn);
da.Fill(ds);
ds.Tables[0].TableName = "Users";
ds.Tables[1].TableName = "Student";
ds.Tables[2].TableName = "Course";
//ds.Tables[3].TableName = "S_Course";
}
catch (SqlException oe)
{
MessageBox.Show(oe.Message, "连接数据库错误信息");
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
this.listBox1.DataSource = ds;//用户管理里面的控件的代码绑定
this.listBox1.DisplayMember = "Users.User_id";
//this.listBox1.DataBindings.Add("Text", ds, "Users.User_id");
//this.listBox1.ValueMember = "Users.User_id";
this.textBox1.DataBindings.Add("Text", ds, "Users.User_id");
this.textBox2.DataBindings.Add("Text", ds, "Users.User_password");
this.comboBox1.DataBindings.Add("Text", ds, "Users.User_power");
this.listBox2.DataSource = ds;//学生管理里面的控件的代码绑定
//this.listBox2.DataBindings.Add("Text", ds, "Student.Student_id");
this.listBox2.DisplayMember = "Student.Student_id";
this.textBox3.DataBindings.Add("Text", ds, "Student.Student_id");
this.textBox4.DataBindings.Add("Text", ds, "Student.Student_name");
this.textBox5.DataBindings.Add("Text", ds, "Student.Student_sex");
this.textBox6.DataBindings.Add("Text", ds, "Student.Student_nation");
this.textBox7.DataBindings.Add("Text", ds, "Student.Student_birthday");
this.textBox8.DataBindings.Add("Text", ds, "Student.Student_time");
this.textBox9.DataBindings.Add("Text", ds, "Student.Student_class");
this.textBox10.DataBindings.Add("Text", ds, "Student.Student_home");
this.richTextBox1.DataBindings.Add("Text", ds, "Student.Student_else");
this.listBox3.DataSource = ds;//课程管理里面的控件的代码绑定
this.listBox3.DisplayMember = "Course.Course_id";
this.textBox11.DataBindings.Add("Text", ds, "Course.Course_id");
this.textBox12.DataBindings.Add("Text", ds, "Course.Course_name");
this.textBox13.DataBindings.Add("Text", ds, "Course.Course_period");
this.textBox14.DataBindings.Add("Text", ds, "Course.Course_credit");
this.richTextBox2.DataBindings.Add("Text", ds, "Course.Course_describe");
this.comboBox2.Text = "数据库";
//this.comboBox2.DataSource = ds;
//this.comboBox2.DisplayMember = "Course.Course_name";
//this.comboBox2.ValueMember = "Course.Course_name";
//this.listBox4.DataSource = ds;
//this.listBox4.DisplayMember = "S_Course.Student_id";
//this.listBox4.ValueMember = "S_Course.Student_id";
//label23.Text = "所有" + this.comboBox2.Text.ToString() + "的学生ID";
DataSet ds1 = new DataSet();
try
{
string sqlstr1 = "select * from dbo.Student where Student_id in(select Student_id from dbo.Student_Course where Course_id=(select Course_id from dbo.Course where Course_name ='" + comboBox2.Text.ToString() + "')) ";
SqlDataAdapter da1 = new SqlDataAdapter(sqlstr1,conn);
da1.Fill(ds1, "S_Course");
}
catch (SqlException oe)
{
MessageBox.Show(oe.Message, "连接数据库错误信息");
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
this.listBox4.DataSource = ds1;
this.listBox4.DisplayMember = "S_Course.Student_id";
this.listBox4.ValueMember = "S_Course.Student_id";
label23.Text = "所有" + this.comboBox2.Text.ToString() + "的学生ID";
}
}
}这样就得!!!有什么comboBox绑定了数据之后.它的text属性就空了!!不是绑定了数据之后它的text属性就等于里面的值了吗?
SELECT Student.你要的列1 ,Student.你要的列2 ,Student.你要的列3 ,Student.你要的列4
FROM Student INNER JOIN Student_Course
ON Student.Student_id = Student_Course.Student_id
INNER JOIN Course
ON Student_Course.Course_id = Course.Course_id
WHERE Course.Course_name = '"+ this.comboBox2.Text +"'
{
public partial class Form2 : Form
{
public DataSet ds;
public Form2()
{
InitializeComponent(); } private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
} private void button1_Click(object sender, EventArgs e)
{
Form1 F1 = new Form1();
F1.Show();
textBox1.Text = "";
textBox2.Text = "";
} private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
} private void Form2_Load(object sender, EventArgs e)
{
string constr = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=学生系统;Data Source=(local)";
SqlConnection conn = new SqlConnection(constr);
DataSet ds = new DataSet();
try
{
string sqlstr = "select * from dbo.Users "
+ "select * FROM dbo.Student "
+ "select * from dbo.Course ";
//+ "select * from dbo.Student where Student_id in(select Student_id from dbo.Student_Course where Course_id=(select Course_id from dbo.Course where Course_name ='"+ "数据库"+"')) ";
SqlDataAdapter da = new SqlDataAdapter(sqlstr, conn);
da.Fill(ds);
ds.Tables[0].TableName = "Users";
ds.Tables[1].TableName = "Student";
ds.Tables[2].TableName = "Course";
//ds.Tables[3].TableName = "S_Course";
}
catch (SqlException oe)
{
MessageBox.Show(oe.Message, "连接数据库错误信息");
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
this.listBox1.DataSource = ds;//用户管理里面的控件的代码绑定
this.listBox1.DisplayMember = "Users.User_id";
//this.listBox1.DataBindings.Add("Text", ds, "Users.User_id");
//this.listBox1.ValueMember = "Users.User_id";
this.textBox1.DataBindings.Add("Text", ds, "Users.User_id");
this.textBox2.DataBindings.Add("Text", ds, "Users.User_password");
this.comboBox1.DataBindings.Add("Text", ds, "Users.User_power");
this.listBox2.DataSource = ds;//学生管理里面的控件的代码绑定
//this.listBox2.DataBindings.Add("Text", ds, "Student.Student_id");
this.listBox2.DisplayMember = "Student.Student_id";
this.textBox3.DataBindings.Add("Text", ds, "Student.Student_id");
this.textBox4.DataBindings.Add("Text", ds, "Student.Student_name");
this.textBox5.DataBindings.Add("Text", ds, "Student.Student_sex");
this.textBox6.DataBindings.Add("Text", ds, "Student.Student_nation");
this.textBox7.DataBindings.Add("Text", ds, "Student.Student_birthday");
this.textBox8.DataBindings.Add("Text", ds, "Student.Student_time");
this.textBox9.DataBindings.Add("Text", ds, "Student.Student_class");
this.textBox10.DataBindings.Add("Text", ds, "Student.Student_home");
this.richTextBox1.DataBindings.Add("Text", ds, "Student.Student_else");
this.listBox3.DataSource = ds;//课程管理里面的控件的代码绑定
this.listBox3.DisplayMember = "Course.Course_id";
this.textBox11.DataBindings.Add("Text", ds, "Course.Course_id");
this.textBox12.DataBindings.Add("Text", ds, "Course.Course_name");
this.textBox13.DataBindings.Add("Text", ds, "Course.Course_period");
this.textBox14.DataBindings.Add("Text", ds, "Course.Course_credit");
this.richTextBox2.DataBindings.Add("Text", ds, "Course.Course_describe");
this.comboBox2.Text = "数据库"; //直接写上就得!!这样证明不是SQL语句的问题!!
//this.comboBox2.DataSource = ds;
//this.comboBox2.DisplayMember = "Course.Course_name";
//this.comboBox2.ValueMember = "Course.Course_name";
//this.listBox4.DataSource = ds;
//this.listBox4.DisplayMember = "S_Course.Student_id";
//this.listBox4.ValueMember = "S_Course.Student_id";
//label23.Text = "所有" + this.comboBox2.Text.ToString() + "的学生ID"; 这样不得
DataSet ds1 = new DataSet();
try
{
string sqlstr1 = "select * from dbo.Student where Student_id in(select Student_id from dbo.Student_Course where Course_id=(select Course_id from dbo.Course where Course_name ='" + comboBox2.Text.ToString() + "')) ";
SqlDataAdapter da1 = new SqlDataAdapter(sqlstr1,conn);
da1.Fill(ds1, "S_Course");
}
catch (SqlException oe)
{
MessageBox.Show(oe.Message, "连接数据库错误信息");
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
this.listBox4.DataSource = ds1;
this.listBox4.DisplayMember = "S_Course.Student_id";
this.listBox4.ValueMember = "S_Course.Student_id";
label23.Text = "所有" + this.comboBox2.Text.ToString() + "的学生ID"; //直接写上就得!!这样证明不是SQL语句的问题!!
}
}
} 这样就得!!!有什么comboBox绑定了数据之后.它的text属性就空了!!不是绑定了数据之后它的text属性就等于里面的值了吗?
这个地方
改为
where Course_id in (select Course_id from dbo.Course where Course_name ='数据库')) 如果不唯一,就会报错的
可是一下这个还是不行呢!!
label23.Text = "所有选择" + this.comboBox2.Text.ToString() + "的学生ID";
{
public partial class Form2 : Form
{
public DataSet ds;
public Form2()
{
InitializeComponent(); } private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
} private void button1_Click(object sender, EventArgs e)
{
Form1 F1 = new Form1();
F1.Show();
textBox1.Text = "";
textBox2.Text = "";
} private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
} private void Form2_Load(object sender, EventArgs e)
{
string constr = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=学生系统;Data Source=(local)";
SqlConnection conn = new SqlConnection(constr);
DataSet ds = new DataSet();
try
{
string sqlstr = "select * from dbo.Users "
+ "select * FROM dbo.Student "
+ "select * from dbo.Course "
+ "select Student_id from dbo.Student where Student_id in(select Student_id from dbo.Student_Course where Course_id=(select Course_id from dbo.Course where Course_name ='" + comboBox2.Text.ToString() + "')) ";
SqlDataAdapter da = new SqlDataAdapter(sqlstr, conn);
da.Fill(ds);
ds.Tables[0].TableName = "Users";
ds.Tables[1].TableName = "Student";
ds.Tables[2].TableName = "Course";
ds.Tables[3].TableName = "S_Course";
}
catch (SqlException oe)
{
MessageBox.Show(oe.Message, "连接数据库错误信息");
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
this.listBox1.DataSource = ds;//用户管理里面的控件的代码绑定
this.listBox1.DisplayMember = "Users.User_id";
//this.listBox1.DataBindings.Add("Text", ds, "Users.User_id");
//this.listBox1.ValueMember = "Users.User_id";
this.textBox1.DataBindings.Add("Text", ds, "Users.User_id");
this.textBox2.DataBindings.Add("Text", ds, "Users.User_password");
this.comboBox1.DataBindings.Add("Text", ds, "Users.User_power");
this.listBox2.DataSource = ds;//学生管理里面的控件的代码绑定
//this.listBox2.DataBindings.Add("Text", ds, "Student.Student_id");
this.listBox2.DisplayMember = "Student.Student_id";
this.textBox3.DataBindings.Add("Text", ds, "Student.Student_id");
this.textBox4.DataBindings.Add("Text", ds, "Student.Student_name");
this.textBox5.DataBindings.Add("Text", ds, "Student.Student_sex");
this.textBox6.DataBindings.Add("Text", ds, "Student.Student_nation");
this.textBox7.DataBindings.Add("Text", ds, "Student.Student_birthday");
this.textBox8.DataBindings.Add("Text", ds, "Student.Student_time");
this.textBox9.DataBindings.Add("Text", ds, "Student.Student_class");
this.textBox10.DataBindings.Add("Text", ds, "Student.Student_home");
this.richTextBox1.DataBindings.Add("Text", ds, "Student.Student_else");
this.listBox3.DataSource = ds;//课程管理里面的控件的代码绑定
this.listBox3.DisplayMember = "Course.Course_id";
this.textBox11.DataBindings.Add("Text", ds, "Course.Course_id");
this.textBox12.DataBindings.Add("Text", ds, "Course.Course_name");
this.textBox13.DataBindings.Add("Text", ds, "Course.Course_period");
this.textBox14.DataBindings.Add("Text", ds, "Course.Course_credit");
this.richTextBox2.DataBindings.Add("Text", ds, "Course.Course_describe");
this.comboBox2.Text = "数据库";
this.comboBox2.DataSource = ds;
this.comboBox2.DisplayMember = "Course.Course_name";
this.comboBox2.ValueMember = "Course.Course_name";
this.listBox4.DataSource = ds;
this.listBox4.DisplayMember = "S_Course.Student_id";
this.listBox4.ValueMember = "S_Course.Student_id";
label23.Text = "所有选择" + comboBox2.Text + "的学生ID";
}
}
}
改了还是不行!!!
label23.Text = "所有选择" + comboBox2.Text + "的学生ID";
这句都不行
如果这句行!!就得了!!
关于4楼的看法是正确的!弹在这里并没有影响到我的数据绑定!!
一位我
this.listBox4.DataSource = ds;
this.listBox4.DisplayMember = "S_Course.Student_id";
this.listBox4.ValueMember = "S_Course.Student_id";
这里用了S_Course.Student_id已经指明是 那一列了因此不是这里的原因!!
经我研究得知引起这个问题的原因是我的窗口里用了tabContorl控件!!
并且tabContorl控件里有多个Tabpage!!当有多个tabContorl控件里有多个Tabpage时就会出现这种情况,如果是tabContorl控件里有多个Tabpage时就得把这些数据填充的时放到第一个Tabpage里才能正常绑定数据。或者只有一Tabpage时也能正常绑定!!我知道引起的原因但是目前还没有找到解决的方法!!希望大家能给个意见!!