gpt4 book ai didi

c# - 需要写一个总和(成绩)/总和(学分)代码

转载 作者:行者123 更新时间:2023-11-30 00:08:51 26 4
gpt4 key购买 nike

我正在制作一个学生数据库,其中包含类(class) ID、类(class)名称、每门类(class)的学分、通过/失败声明和成绩。我的目标是让按钮显示总和(成绩)/总和(学分)

这是我编写的代码,它给我一个错误,说“已经有一个与此连接关联的打开的数据读取器,必须首先关闭”。这是我的代码:

private void button1_Click(object sender, EventArgs e)
{
string ConString = " datasource = localhost; port = 3306; username = root; password = 3306";
string Query = " Select sum(grade) from studentdata.semestre1";
string Query1 = " Select sum(grade) from studentdata.semestre1";
MySqlConnection ConDatabase = new MySqlConnection(ConString);
MySqlCommand cmdDataBase = new MySqlCommand(Query, ConDatabase);
MySqlCommand cmdDataBase1 = new MySqlCommand(Query1, ConDatabase);

MySqlDataReader myReader;
MySqlDataReader myReader1;

ConDatabase.Open();

myReader = cmdDataBase.ExecuteReader() ;
myReader1 = cmdDataBase1.ExecuteReader();

while ((myReader.Read()) && (myReader1.Read()))
{
textBox2.Text =Convert.ToString(double.Parse( myReader.GetString(0))/ double.Parse(myReader1.GetString(0))) ;
}
myReader.Close();
myReader1.Close();

有人可以帮忙吗?

最佳答案

该错误表明数据读取器在其他位置处于打开状态。它类似于连接对象。您不必创建两次,您可以在单个查询中执行相同的操作

  string ConString = " datasource = localhost; port = 3306; username = root; password = 3306";
string Query = "Select SUM(grade)/SUM(credits) FROM studentdata.semestre1";
MySqlConnection ConDatabase = new MySqlConnection(ConString);
MySqlCommand cmdDataBase = new MySqlCommand(Query, ConDatabase);
MySqlDataReader myReader;
ConDatabase.Open();

while ((myReader.Read()) && (myReader1.Read()))
{
textBox2.Text =Convert.ToString(double.Parse( myReader.GetString(0))) ;
}
myReader.Close();

关于c# - 需要写一个总和(成绩)/总和(学分)代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24264083/

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