C# 根据所选项目组合框填充列表框时出现SQL语法错误

C# 根据所选项目组合框填充列表框时出现SQL语法错误,c#,mysql,winforms,combobox,C#,Mysql,Winforms,Combobox,我正在开发一个应用程序,其中有四个组合框I-e(comboBox1用于类,Combox2用于组,Combox3用于节,Combox4用于月)和一个列表框。我已经在“OnLoad函数”中将数据从数据库提取到所有这些组合框中。现在我想根据组合框在列表框中显示学生姓名。当我从类(组合框1)中选择“第7类”时,即为e。它应该显示在所选班级(第7班)、所选小组(在comboBox2中选择)和所选部分(在Combox3中选择)中的学生。这是我的代码,但它给了我SQL语法异常和IndexOutfrange异常

我正在开发一个应用程序,其中有四个组合框I-e(comboBox1用于类,Combox2用于组,Combox3用于节,Combox4用于月)和一个列表框。我已经在“OnLoad函数”中将数据从数据库提取到所有这些组合框中。现在我想根据组合框在列表框中显示学生姓名。当我从类(组合框1)中选择“第7类”时,即为e。它应该显示在所选班级(第7班)、所选小组(在comboBox2中选择)和所选部分(在Combox3中选择)中的学生。这是我的代码,但它给了我SQL语法异常和IndexOutfrange异常。连接的数据库是mysql。“dbOperation”是类“MyDatabase”的对象,我在其中连接了数据库并实现了所有查询

   public partial class FeeVoucherPrint : Form
    {

        myDatabase dbOperation = new myDatabase();
        DataTable table = new DataTable();
        public FeeVoucherPrint()
        {
            InitializeComponent();
            this.MaximizeBox = false;
            this.MinimizeBox = false;

        }

        private void FeeVoucherPrint_Load(object sender, EventArgs e)
        {
            table = dbOperation.select("* from class");
            comboBox1.DataSource = table;
            comboBox1.DisplayMember = "name";
            comboBox1.ValueMember = "id";

            table = dbOperation.select("* from `group`");
            comboBox2.DataSource = table;
            comboBox2.DisplayMember = "name";
            comboBox2.ValueMember = "id";

            table = dbOperation.select("* from section");
            comboBox3.DataSource = table;
            comboBox3.DisplayMember = "name";
            comboBox3.ValueMember = "id";

            table = dbOperation.select("* from months");
            comboBox4.DataSource = table;
            comboBox4.DisplayMember = "month";
            comboBox4.ValueMember = "id";
        }

        private void fill()
        {
            try
            {
                table = dbOperation.select("studentid from studentinclass where classid = " + comboBox1.SelectedValue + " and groupid = " + comboBox2.SelectedValue);
                table = dbOperation.select("* from student where studid = " + table.Rows[0]["studentid"]);
                listBox1.DataSource = table;
                listBox1.DisplayMember = "name";
                listBox1.ValueMember = "studid";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            fill();
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            fill();
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            fill()
        }


    }
看起来您正在以错误的方式使用方法(
dbOperation。在本例中选择

获取与筛选条件匹配的所有DataRow对象的数组。

这就是为什么您的
studentid from studentinclass where
部分在第一个
中。选择
*from student where
部分在第二个
中。选择
是不必要的。它们不是过滤器


阅读文档的备注部分。

假设dbOperation返回一个datatable或ienumerable,则应将其直接分配给数据源。