本文介绍了如果SQL Server数据库中存在所选项,请从组合框中选择下一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Windows窗体上,有两个组合框,分别显示特定课程的名称和部分的数量。但是,我想为我的组合框(部分数量)编写一些代码,使其能够自动选择正确的部分编号。例如,对于课程C#,如果第一部分插入到SQL Server中,则部分编号的组合框应选择该课程的第二部分。

 SqlConnection objConnection =  new  SqlConnection( @ 数据源=(LocalDB)\ MSSQLLocalDB ; AttachDbFilename = C:\UniversityDataBase.mdf; Integrated Security = True); 

private void CourseName_Click(对象 sender,EventArgs e)
{
string query = SELECT * FROM CourseTable;

SqlDataAdapter SDA = new SqlDataAdapter(query,objConnection);
DataTable dt = new DataTable();
SDA.Fill(dt);

comboBox1.DataSource = dt;
comboBox1.DisplayMember = Coursename;
objConnection.Close();
}

private void SectionNo_Click( object sender,EventArgs e)
{
string query = SELECT * FROM SecNo;

SqlDataAdapter SDA = new SqlDataAdapter(query,objConnection);
DataTable dt = new DataTable();
SDA.Fill(dt);

comboBox2.DataSource = dt;
comboBox2.DisplayMember = SectoinNumber;

objConnection.Close();
}





我的尝试:



[ ]

解决方案

On my Windows form, there are two comboboxes showing name of specific courses and number of sections respectively. But, I want to write some codes for my combobox (number of sections) that makes it enable to choose the correct section number automatically. For example, for course C#, if section one is inserted into SQL Server, the combobox for section number should select section two for that course.

SqlConnection objConnection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\UniversityDataBase.mdf;Integrated Security=True");

private void CourseName_Click(object sender, EventArgs e)
{
    string query = "SELECT *FROM CourseTable";

    SqlDataAdapter SDA = new SqlDataAdapter(query, objConnection);
    DataTable dt = new DataTable();
    SDA.Fill(dt);

    comboBox1.DataSource = dt;
    comboBox1.DisplayMember = "Coursename";
    objConnection.Close();
}

private void SectionNo_Click(object sender, EventArgs e)
{
    string query = "SELECT * FROM SecNo";

    SqlDataAdapter SDA = new SqlDataAdapter(query, objConnection);
    DataTable dt = new DataTable();
    SDA.Fill(dt);

    comboBox2.DataSource = dt;
    comboBox2.DisplayMember = "SectoinNumber";

    objConnection.Close();
}



What I have tried:

c# - Select Next Item from Combo Box if the item exists in SQL Server database - Stack Overflow[^]

解决方案


这篇关于如果SQL Server数据库中存在所选项,请从组合框中选择下一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 07:28