【问题标题】:SQL syntax error in populating ListBox according to comboBoxes selected item根据组合框选定项填充列表框时的 SQL 语法错误
【发布时间】:2014-04-01 01:45:57
【问题描述】:

我正在开发一个应用程序,其中我有四个组合框(即组合框 1 用于类,组合框 2 用于组,组合框 3 用于部分,组合框 4 用于月份)和一个列表框。我已经从数据库中获取数据到“OnLoad 函数”中的所有这些组合框。现在我想根据组合框在列表框中显示学生姓名。即当我从类(comboBox1)中选择“第 7 类”时。它应该显示那些在选定班级(第 7 班)、选定组(在组合框 2 中选定)和选定部分(在组合框 3 中选定)的学生。这是我的代码,但它给了我 SQL 语法异常和 indexOutOfRange 异常。附加的数据库在mysql中。 “dbOperation”是“myDatabse”类的对象,我在其中连接了数据库并实现了所有查询。

   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 返回一个数据表或一个 ienumerable ,你应该将它直接分配给数据源。

标签: c# mysql winforms combobox


【解决方案1】:

您似乎以错误的方式使用了DataTable.Select 方法(在本例中为dbOperation.select)。

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

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

阅读DataColumn.Expression property 文档的备注部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多