我可能知道下面的代码有什么问题..因为它没有显示结果。我想查看员工的姓名,他们每个月的活动数量,然后通过除以数字来查看他们的平均成绩。对他们的分数的活动。我希望你能帮助我做到这一点。我想在列表框中查看它
格式为:
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时,它不会出现。
答案 0 :(得分:0)
可能有几个问题。
您是否尝试在Sql Management Studio中运行sql语句?
他们有没有结果?
如果是这样,请尝试使用
listbox1.Items.Add
代替Insert
没有异常错误,rite?
如果我是你,我会使用sqlDataAdapter
并将所有结果放入datatable
然后我会检查datatable
是否有任何行。如果是这样,我将循环并将它们添加到列表框中。
答案 1 :(得分:0)
我以一种应该工作的方式更改了代码:
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);
}
答案 2 :(得分:0)
你最大的问题是你的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);
此外,您不需要在列表项上使用insert,您应该使用add:
listBox1.Items.Add(Convert.ToString(n));
etc...
最后,您应该从数据库中转换值,以防它们包含DBNulls:
Log_User = dr["Log_User"] as string;
etc...
答案 3 :(得分:0)
将插入替换为添加。
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));
}
此致
答案 4 :(得分:0)
您是否完全确定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
答案 5 :(得分:0)
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上帝保佑