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

标签 c# mysql winforms combobox

我正在开发一个应用程序,其中有四个组合框,即(用于类(class)的组合框1,用于组的组合框2,用于部分的组合框3,用于月份的组合框4)和一个列表框。我已从数据库中获取数据到“OnLoad Function”中的所有这些组合框。现在我想根据组合框在列表框中显示学生姓名。即当我从类(comboBox1)中选择“第 7 类”时。它应该显示所选类(class)(第 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()
        }


    }

最佳答案

看起来您正在使用 DataTable.Select方法(在本例中是dbOperation.select)以错误的方式。

Gets an array of all DataRow objects that match the filter criteria.

这就是为什么您的 studentid from Studentinclass where 部分位于第一个 .Select 中,而 * from Student where 部分位于第二个 .Select 是不必要的。它们不是过滤器。

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

关于c# - 根据组合框所选项目填充列表框时出现 SQL 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22088696/

相关文章:

php - SQL:选择多个表的计数

c# - 我怎样才能使窗体背景透明而不是其他控件

c# - 静态一次性元素

c# - 强签名程序集

c# - 在代码隐藏中创建一个 ajaxToolkit TabPanel

c# - 添加服务引用不会出现在 Visual Studio 2010 中

php - 使用 PHP mcrypt 插入时查询随机失败

c# - System.Argument 出现异常 :Specified argument was out of range of valid value

sql - MySQL 使用 REGEX 匹配逗号分隔值中是否存在术语

VB.NET 窗体高度问题