就是我用了一条语句 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{...
... (select * from score where studentnumber='" + comboBox1.Text + "'", myconn);
...
...
score 表里有studentnumber,name,caursename,classname,score,
问怎样调用执行了此句sql语句后得到的一条结果。
在combox2-4里面显示出name,caursename,classname,score的值?
{...
... (select * from score where studentnumber='" + comboBox1.Text + "'", myconn);
...
...
score 表里有studentnumber,name,caursename,classname,score,
问怎样调用执行了此句sql语句后得到的一条结果。
在combox2-4里面显示出name,caursename,classname,score的值?
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection myconn = null;
SqlDataReader = null;
try
{
SqlConnection myconn = new SqlConnection("server=....");
//确保studentnumber是varchar或char型的,否则去掉单引号
string number = comboBox1.Text;
string sql = "select name,caursename,classname,score from score where studentnumber='"+number+"'";
SqlCommand cmd = new SqlCommandmyconn(sql,myconn);
SqlDataReader reader = cmd.ExecuteReader();
if(reader.HasRows)
{
combox2.Items.Clear(); //这种情况,三个都用广本框即可
combox3.Items.Clear();
combox4.Items.Clear();
if(reader.Read())
{
combox2.Items.Add(reader.GetString(0));
combox3.Items.Add(reader.GetString(1));
combox4.Items.Add(reader.GetString(2));
}
combox2.SelectedIndex = 0;
combox2.SelectedIndex = 0;
combox2.SelectedIndex = 0;
}
}
finaly
{
if(reader != null) reader.Close();
if(myconn != null) myconn.Close();
}
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
SqlConnection myconn = new SqlConnection();
myconn.ConnectionString = "server=localhost;database=student2;uid=sa;pwd=123456;";
myconn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("select * from score where studentnumber='" + comboBox3.Text + "'", myconn);
da.Fill(ds, "student2");
DataRow myDR = ds.Tables["student2"].Rows[0];
string a = myDR["spename"].ToString();
string b = myDR["classname"].ToString();
this.comboBox1.Text = a;
this.comboBox2.Text = b;
this.comboBox4.Text = myDR["coursename"].ToString();
this.comboBox5.Text = myDR["name"].ToString();
this.comboBox6.Text = myDR["semester"].ToString();
this.textBox1.Text = myDR["score"].ToString();
myconn.Close();
}
catch (SqlException ex)
{ MessageBox.Show(ex.ToString()); }