结果不会显示在列表框中

本文关键字:列表 显示 结果 | 更新日期: 2023-09-27 18:17:53

我可以知道下面的代码出了什么问题。因为它没有显示结果。我想查看员工的姓名,他们每月的活动数量,然后通过将活动数除以他们的分数来查看他们的平均成绩。我希望你能帮助我做到这一点。我想在列表框中查看它

格式是这样的:

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时,它没有出现。

结果不会显示在列表框中

可能是几个问题。

您是否尝试过在 Sql Management Studio 中运行 SQL 语句?

他们有什么结果吗?

如果是这样,请尝试使用

listbox1.Items.Add而不是Insert

没有异常错误,仪式?

如果我是你,我会使用 sqlDataAdapter 并将所有结果放入datatable然后我会检查datatable是否有任何行。如果是这样,我将循环并将它们添加到列表框中。

我以一种

应该工作的方式更改了您的代码:

econ = new SqlConnection(); 
econ.ConnectionString = emp_con; 
econ.Open(); 
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.Items.Add(txtYear.Text); 
listBox1.Items.Add(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); 
 listBox1.Items.Add(string.Format("{0}", n)); 
 listBox1.Items.Add(string.Format("Grade: {0}", iGrade))); 
 listBox1.Items.Add(string.Format("Score: {0}",  Score))); 
 listBox1.Items.Add(string.Format("Count: {0}", Count)); 
 listBox1.Items.Add(Log_User); 
 listBox1.Items.Add(n.ToString()); 
} 
econ.Close(); 
} 
catch (Exception x) 
{ 
  MessageBox.Show(x.GetBaseException().ToString(), "Connection Status", MessageBoxButtons.OK,     MessageBoxIcon.Error); 
} 

你最大的问题是你的SQL语句,特别是喜欢的。您不添加 %,因此除非用户键入该 %,否则不会得到任何结果。

另外,我认为在这种情况下,喜欢是不合适的,因为您正在寻找月份数字。

最后,您应该使用参数来防止 SQL 注入,但我将其作为另一个练习。

以下是您需要的最小更改:

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) = " + comMonth.Text + " AND Year(Sched_Start) = " + txtYear.Text + " GROUP BY Log_User", econ);

此外,您不需要在列表项上使用插入,您应该改用添加:

listBox1.Items.Add(Convert.ToString(n));
etc...

最后,您应该从数据库中强制转换值,以防它们包含 DBNulls:

Log_User = dr["Log_User"] as string;
etc...

将插入替换为添加。

 if (dr == null || !dr.HasRows) {
                                 //No records found
                                }
     else
       {
     listBox1.Items.Add(found, Convert.ToString(n));
     listBox1.Items.Add(found, Convert.ToString(strGrade) + Convert.ToString(iGrade));
     listBox1.Items.Add(found, Convert.ToString(strScore) + Convert.ToString(Score));
     listBox1.Items.Add(found, Convert.ToString(strCount) + Convert.ToString(Count));
     listBox1.Items.Add(found, Convert.ToString(Log_User));
     listBox1.Items.Add(found, Convert.ToString(n));
      }

问候

您是否绝对确定 SQL 查询在 SQL 管理工作室中正确运行?您没有对"计数"字段进行分组(至少(,因此您应该会收到聚合错误。

此外,您可能希望正确加入,而不是通过 WHERE 子句(个人偏好(加入。

实际上,您的SQL语句似乎已损坏。试试这个:

SELECT Log_User, COUNT(Det_Score) as count, SUM(Det_Score) as Score
FROM MEMBER M
    JOIN DETAILS D on M.Emp_Id = D.Emp_Id
WHERE MONTH(Sched_Start) like + "' + comMonth.Text + '" AND YEAR(Sched_Start) LIKE "' + txtYear.Text '"
GROUP BY Log_User, COUNT(Det_Score) as count, SUM(Det_Score) as Score
listBox1.Items.Clear();
listBox1.Text = "";
econ = new SqlConnection();
econ.ConnectionString = emp_con;
econ.Open();
float iGrade = 0;
float Grade = 0.00F;
string Log_User;
float Count, Score;
string n = "";
string strScore = "Score: ";
string strGrade = "Grade: ";
string strCount = "Count: ";
string date = DateTime.Now.ToShortTimeString();
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 Log_User like" + "'" + Convert.ToString(comEmployee.Text) + "'AND Month(Sched_Start) =" + "'" + Convert.ToInt32(iMonth) + "'AND Year(Sched_Start) =" + "'" + Convert.ToInt32(txtYear.Text) + "'GROUP BY Log_User", econ);
ecmd.CommandType = CommandType.Text;
ecmd.Connection = econ;
dr = ecmd.ExecuteReader();
listBox1.Items.Add("As of " + date + ": "+ comMonth.Text + ", "+ txtYear.Text);
while (dr.Read())
{
   if (dr == null || !dr.HasRows)
   {
      MessageBox.Show("No record found.", "Error");
   }
   else
   {
      Log_User = (string)dr["Log_User"];
      Count = (dr["Count"] as int?) ?? 0;
      Score = (dr["Score"] as int?) ?? 0;
      try
      {
         iGrade = Score / Count;
         Grade = iGrade * 100;
      }
      catch (DivideByZeroException)
      {
          Console.WriteLine("Exception occured");
      }
      listBox1.Items.Add(Convert.ToString(n));
      listBox1.Items.Add(Convert.ToString(Log_User));
      listBox1.Items.Add(Convert.ToString(strCount + Count));
      listBox1.Items.Add(Convert.ToString(strScore + Score));
      listBox1.Items.Add(Convert.ToString(strGrade + Grade));
      }
   }               
   econ.Close();

上面的代码是我对我的问题的回答。我希望它可以帮助其他人。感谢那些帮助我弄清楚的人。:D上帝保佑