我是C#的新手。我试图从名为'tblAddResult'的表中读取数据。在这个表中我有一些列,我想平均3列tblAddResult并将其保存到名为tblResult
的新表中。
其他一些计算也需要像tblAddResult
列那样采集50%的数据。
我这样做了(代码放在下面),但是我收到一个错误“读取器关闭时调用read的无效尝试”。我甚至不知道这是否是正确的方法,如果没有人可以帮助我写这种方式或任何有关这种方式的建议。我对这个问题真的很无奈。
private void button1_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("SELECT tblAddResult.* FROM tblAddResult", con);
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
String subject = sdr.GetString(2);
int january = sdr.GetInt32(3);
int february = sdr.GetInt32(4);
int march = sdr.GetInt32(5);
int average = (january + february + march) / 3;
int average40= average*40/(100);
int marks = sdr.GetInt32(6);
int marks50 = marks * 50 / 100;
int WorkingDay = sdr.GetInt32(7);
int Attandence = sdr.GetInt32(8);
int Attendence10 = Attandence * 10 / 100;
int totalMarks = average40 + marks50 + Attendence10;
string grade = "" ;
if (totalMarks < 51) { grade = "C"; }
else if (totalMarks < 61) { grade = "B"; }
else if (totalMarks < 71) { grade = "A-"; }
else if (totalMarks < 81) { grade = "A"; }
else if (totalMarks < 91) { grade = "A+"; }
else if (totalMarks <= 100) { grade = "A++"; }
con.Close();
con.Open();
SqlCommand comnd = new SqlCommand("INSERT INTO tblResult (Subject, [Full Marks], January, February, March, [Average Class Perfomance], [Earned Marks], [Working Day], Attendence, [Cls Attendence(40%)], [Exam Perfomance(50%)], [Attendence(10%)], [Marks(%)], Grade)VALUES ('"+subject+"','"+"100"+"','"+january+"','"+february+"','"+march+"','"+average+"','"+marks+"','"+WorkingDay+"','"+Attandence+"','"+average40+"','"+marks50+"','"+Attendence10+"','"+totalMarks+"','"+grade+"')",con);
comnd.ExecuteNonQuery();
MessageBox.Show("Successfull!");
}
答案 0 :(得分:0)
您收到此错误是因为在返回/完成阅读器之前关闭了连接。删除两行con.Close();
和con.Open();
并在阅读器完成后(即,在while循环之外)连接连接。
答案 1 :(得分:0)
编辑如下代码,以提高安全性。发生错误是因为DataReader操作仅在处于Opened Connection状态时才起作用。
con.Open();
cmd = new SqlCommand("SELECT tblAddResult.* FROM tblAddResult", con);
SqlDataReader sdr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);//this will close the DataReader along with Connection close.
while (sdr.Read())
{
String subject = sdr.GetString(2);
int january = sdr.GetInt32(3);
int february = sdr.GetInt32(4);
int march = sdr.GetInt32(5);
int average = (january + february + march) / 3;
int average40= average*40/(100);
int marks = sdr.GetInt32(6);
int marks50 = marks * 50 / 100;
int WorkingDay = sdr.GetInt32(7);
int Attandence = sdr.GetInt32(8);
int Attendence10 = Attandence * 10 / 100;
int totalMarks = average40 + marks50 + Attendence10;
string grade = "" ;
if (totalMarks < 51) { grade = "C"; }
else if (totalMarks < 61) { grade = "B"; }
else if (totalMarks < 71) { grade = "A-"; }
else if (totalMarks < 81) { grade = "A"; }
else if (totalMarks < 91) { grade = "A+"; }
else if (totalMarks <= 100) { grade = "A++"; }
cmd = new SqlCommand("INSERT INTO tblResult (Subject, [Full Marks], January, February, March, [Average Class Perfomance], [Earned Marks], [Working Day], Attendence, [Cls Attendence(40%)], [Exam Perfomance(50%)], [Attendence(10%)], [Marks(%)], Grade)VALUES ('"+subject+"','"+"100"+"','"+january+"','"+february+"','"+march+"','"+average+"','"+marks+"','"+WorkingDay+"','"+Attandence+"','"+average40+"','"+marks50+"','"+Attendence10+"','"+totalMarks+"','"+grade+"')",con);
cmd.ExecuteNonQuery();
}
con.Close();
MessageBox.Show("Successfull!");
编辑:
使用DataTable代替使用DataReader方法,下面给出了一个示例。希望它会有所帮助。
cmd = new SqlCommand("SELECT * FROM Test_Table1", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
foreach (DataRow sdr in dt.Rows)
{
String subject = sdr[0].ToString();
int january = Convert.ToInt32(sdr[1]);
int february = Convert.ToInt32(sdr[2]);
int march = 0;
int average = (january + february + march) / 3;
int average40 = average * 40 / (100);
cmd = new SqlCommand("INSERT INTO Test_Table2 (Test_Col1, Test_Col2) VALUES ('" + subject + "'," + average40 + ")", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}