gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:36:59 25 4
gpt4 key购买 nike

我正在开发一个应用程序,其中有四个组合框,即(用于类(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com