Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 结果不会显示在列表框中_C#_Sql Server - Fatal编程技术网

C# 结果不会显示在列表框中

C# 结果不会显示在列表框中,c#,sql-server,C#,Sql Server,我可以知道我下面的代码有什么问题吗因为它没有显示结果。我想查看员工的姓名,他们每月的活动数量,然后通过将活动数量除以他们的分数来查看他们的平均分数。我希望你能帮我做这件事。我想在列表框中查看它 格式如下: Juan Dela Cruz Count: 10 Score: 5 Grade: 2 econ = new SqlConnection(); econ.ConnectionString = emp_con; econ.Open(); int found = -1; string Lo

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

格式如下:

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

没有异常错误,rite


如果我是您,我会使用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语句,特别是类似的语句。您不添加%,因此,除非用户键入%,否则不会得到任何结果

另外,我认为like在这种情况下是不合适的,因为你正在寻找一个月数

最后,您应该使用参数来防止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);
此外,您不需要在列表项上使用insert,而应该使用add:

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 Mgmt studio中正确运行?您至少没有在Count字段上分组,因此应该会出现聚合错误

此外,您可能希望正确加入,而不是通过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

上面的代码是我对我的问题的回答。我希望它能帮助其他人。感谢那些帮助我解决问题的人D上帝保佑

请不要在标题末尾加C。这就是标签的作用。对不起。。你能告诉我我的代码有什么问题吗??谢谢……不用了。这是太多太多的代码阅读。创建一个复制问题的小示例,然后发布该示例。解决此问题后,您可能希望在StackExchange code review上放弃该代码。。。您需要处理大量未正确处理的资源。econ.Close还不足以让您免于麻烦。至少,在第一条Insert语句处设置一个断点,然后在“局部变量”窗口中查看listbox对象,或者将鼠标悬停在该对象上,然后查看该值是否已设置。我的猜测是,它根本没有被设定。正如William所说,使用Add,而不是Insert。此外,将数据放入datatable可以更容易地查看和调试。我在我的SQL Management Studio中试用了它,它运行得很顺利。但在VS2010中,它不会运行。我尝试了你的listbox1.Items.Add,它显示红线。我将尝试SQLDataAdapter,并在此处发布结果。使用Fischermaen对listbox1.Items.Add的方法以正确显示。。如果这仍然有问题的话。首先创建一个临时列表项。然后在listbox1.Items之后填充listItem.Value和listItem.Text。AddlistItem@Kevzz:然后调试代码,while dr.Read中一定有错误。谢谢您的代码。。我正在调试我的代码。。到目前为止,结果显示,但他们仍然是我的一些代码,我需要改变。。顺便说一句,谢谢。只要删除括号中的,它就会运行。。谢谢你帮助我D
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();