我希望将所有员工的出勤日期显示为月度报告。 所以我的数据库将是这样的。
如何以这样的方式显示它们,将计算所有月份。感谢
我尝试过使用此代码,只返回1个结果,谢谢
SELECT EMPLOYEEID ,COUNT( DISTINCT checkinDate) As April FROM ATTENDANCE WHERE checkinDate BETWEEN '4/1/2018' AND '4/30/2018' GROUP BY EMPLOYEEID
答案 0 :(得分:2)
安静简单。首先将员工ID存储在列表中。
Dim employeeList as New List(of String)
Dim con as New SqlConnection("connection string her")
Dim cmd as New SqlCommand("Select employeeID from tableName",con)
Dim dr as new SqlDataReader = cmd.ExecuteReader
While dr.read
employeeList.Add(dr(0).ToString)
End while
'Make sure to remove duplicates from the list'
employeeList=employeeList.Distinct.ToList()
创建一个处理employeeId,Attendence计数和月份名称的类
Public class employees
Public id as String
Public month as String
Public atCount as Integer
End class
现在您可以使用列表中的ID并获取每个employeeId的关注度,同时确保创建我们刚刚创建的类的列表。
Dim resultList as New List(of employees) 'We are creating the list to hold the records of each employee'
Private sub addData()
Dim month as Integer = 1
Dim year as Integer = 2014 'I created two variables which would later be used in the sql statements and furthermore.'
Dim cmd as New SqlCommand("Select * from tablename Where employeeId=@empid,MONTH(checkinDate) = " + month + " AND YEAR(checkinDate) =" + year + "" , con) 'Here we use the MONTH(date) and YEAR function of SQL'
cmd.Parameters.Add("@empid",sqlDbType.VarChar)
Dim dr as SqlDataReader = cmd.ExecuteReader
For Each id in employeeList 'Here we iterate through the list'
cmd.Parameters(0).Value = id
While dr.read
Dim emp as New employees
emp.id = id
emp.month = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(month)
emp.atCount = emp.atCount + 1 'Here we increase the count of attendence for each record that exisits'
resultList.Add(emp) 'Finally we add the data to the list'
End While
dr.Close()
If not month = 12 Then
month = month + 1 'Firstly we check if our "month" value exceeded 12,if it did, we count one more year , if not , we count one more month'
Else
year = year + 1
month = 1
End if
Next
End sub
现在您有一个列表..您可以将它的数据保存到数据库中:
Dim cmd as new SqlCommand("Insert Into tableName(empId,MonthName,Count)VALUES(@eid,@mname,@count)")
cmd.Parameters.Add("@eid",sqlDbType.Varchar)
cmd.Parameters.Add("@mname",sqlDbType.Varchar)
cmd.Parameters.Add("@count",sqlDbType.Int)
For Each employee in resultList 'We iterate through our final list now'
cmd.Parameters(0).Value = employee.id
cmd.Parameters(1).Value = employee.month
cmd.Parameters(2).Value = employee.atCount
cmd.ExecuteNonQuery
Next
'Make sure you close the connection when all is done :)'
con.Close
或者,如果您只是想在datagridview中显示最终列表,请考虑以下代码段:
For Each employee in resultList
Dim row As DataGridViewRow = CType(dgvw1.Rows(0).Clone(), DataGridViewRow)
row.Cells(0).Value = employee.id
row.Cells(1).Value = employee.month
row.Cells(2).Value = employee.atCount
dgvw1.Rows.Add(row)
Next
希望这有助于并丰富您的知识:)