簡體   English   中英

結果沒有出現在列表框中

[英]the result doesn't appear in listbox

請問下面的代碼有什么問題。因為它不顯示結果。 我想查看員工的姓名,每月的活動數量,然后通過除以數字來查看其平均等級。 活動的得分。 我希望你能幫助我做到這一點。 我想在列表框中查看

格式是這樣的:

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 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
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上帝保佑

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM