[英]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.