【发布时间】:2011-12-13 06:08:49
【问题描述】:
请问我下面的代码有什么问题吗?因为它没有显示结果。我想查看员工的姓名,他们每月的活动数量,然后除以数量来查看他们的平均成绩。活动到他们的分数。我希望你能帮助我做到这一点。我想在列表框中查看它
格式是这样的:
Juan Dela Cruz
Count: 10
Score: 5
Grade: 2
econ = new SqlConnection();
econ.ConnectionString = emp_con;
econ.Open();
int found = -1;
string Log_User, Count, Score;
int iGrade = 0;
string n = "";
string strScore = "Score: ";
string strGrade = "Grade: ";
string strCount = "Count: ";
ecmd = new SqlCommand("SELECT Log_User, Count = COUNT(Det_Score), Score = SUM(Det_Score) FROM MEMBER M,DETAILS D WHERE D.Emp_Id = M.Emp_Id AND Month(Sched_Start) like" + "'" + comMonth.Text + "'AND Year(Sched_Start) like" + "'" + txtYear.Text + "'GROUP BY Log_User", econ);
ecmd.CommandType = CommandType.Text;
ecmd.Connection = econ;
dr = ecmd.ExecuteReader();
listBox1.Text = txtYear.Text;
listBox1.Text = comMonth.Text;
while (dr.Read())
{
Log_User = (string)dr["Log_User"];
Count = (string)dr["Count"];
Score = (string)dr["Score"];
iGrade = Convert.ToInt32(Count) / Convert.ToInt32(Score);
found += 1;
listBox1.Items.Insert(found, Convert.ToString(n));
listBox1.Items.Insert(found, Convert.ToString(strGrade) + Convert.ToString(iGrade));
listBox1.Items.Insert(found, Convert.ToString(strScore) + Convert.ToString(Score));
listBox1.Items.Insert(found, Convert.ToString(strCount) + Convert.ToString(Count));
listBox1.Items.Insert(found, Convert.ToString(Log_User));
listBox1.Items.Insert(found, Convert.ToString(n));
}
econ.Close();
}
catch (Exception x)
{
MessageBox.Show(x.GetBaseException().ToString(), "Connection Status", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
我想在这里查看结果,但看不到输出。我的sql语句有问题吗??在 MS SQL 2005 中,当我运行 sql 时,它运行顺利,但是当我将它运行到 VS 2010 时,它没有出现。
【问题讨论】:
-
请不要在标题末尾添加“C#”。这就是 for 的标签。
-
对不起.. 你能告诉我我的代码有什么问题吗?谢谢..
-
没有。阅读的代码太多了。创建一个重现问题的小示例,然后发布示例。
-
解决此问题后,您可能希望将代码扔到 StackExchange 代码审查中……您需要处理大量未正确处理的资源。 econ.Close() 不足以让您远离麻烦。
标签: c# sql-server