我们的内部网站上有一个表格,员工必须每年完成一次。
要填写此表单,员工必须先输入他/她的员工ID。
检查此ID以查看此员工之前是否已完成此表单。
处理顺序:
我们在数据库上有几个表,但最相关的三个表是Employees表,dateDetails表和IncomeSource表。
员工输入他的个人信息,然后继续填写表格的其余部分。
处理顺序是员工信息保存到employees表中,检索最后插入的ID并将其与日期详细信息一起存储在dateDetails表中。
同样,当其他表格完成时,同样最后插入的ID也会插入其他表格以及这些表格的详细信息。
这对我们来说是完美无缺的。
历史:
当员工将他/她的员工ID输入文本框并且他/她的鼠标离开文本框时,我们需要向员工提供三种不同的消息之一。
我的问题是,下面的当前代码只检查员工的信息是否在数据库中。
Protected Sub txtEmpID_TextChanged(sender As Object, e As EventArgs) Handles txtEmpID.TextChanged
If Not String.IsNullOrEmpty(txtEmpID.Text) Then
Dim Conn As SqlConnection
'Read in connection String'
Conn = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Conn.Open()
Dim cmd As New SqlCommand("select *
from Employees e
inner join dateDetails d on e.employeeID = d.EmployeeID
where EmpID=@empID
and Year(d.enterDate) >= 2015
and Year(d.enterDate)<= 2017", Conn)
cmd.Parameters.AddWithValue("@empID", txtEmpID.Text)
Dim dr As SqlDataReader = cmd.ExecuteReader()
If dr.HasRows Then
dr.Read()
checkusername.Visible = True
'imgstatus.ImageUrl = "images/NotAvailable.jpg"'
lblStatus.Text = "Please verify your information for accuracy. Then complete rest of the form."
lblStatus.ForeColor = System.Drawing.Color.Red
System.Threading.Thread.Sleep(100)
txteName.Text = dr("employeeName").ToString()
txttitle.Text = dr("empTitle").ToString()
txtemail.Text = dr("email").ToString()
txtEmpID.Text = dr("empID").ToString()
Else
dr.Read()
checkusername.Visible = True
'imgstatus.ImageUrl = "images/Icon_Available.gif"'
lblStatus.Text = "Proceed"
lblStatus.ForeColor = System.Drawing.Color.Red
System.Threading.Thread.Sleep(100)
txteName.Text = dr("employeeName").ToString()
txttitle.Text = dr("empTitle").ToString()
txtemail.Text = dr("email").ToString()
txtEmpID.Text = dr("empID").ToString()
End If
Else
checkusername.Visible = False
End If
End Sub
如果是,员工会收到一条已存在的消息。
如果不是,员工会收到要继续的消息。
这还不够。
如何修改以满足上面列举的三个要求?
答案 0 :(得分:0)
您可以使用MAX
这样简单地向您的数据库询问姓名,电子邮件,标题,ID和最后输入的日期(考虑到datedetails可以有多个具有相同employeeID的行,如果不是只是去除MAX和GROUP BY):
SELECT name, email, title, EmpID, d.enterDate as enterYear
FROM Employees e
INNER JOIN dateDetails d on e.employeeID = d.EmployeeID
INNER JOIN IncomeSource i on e.employeeID = i.EmployeeID
WHERE EmpID=@empID
然后验证使用Vb.Net输入的最后日期。如果没有返回任何内容,那么您的员工并不存在于所有表中。
如果你真的想检查查询中的所有案例,那就可以了。你需要看看布尔值
SELECT name, email, title, EmpID,
CASE WHEN YEAR(d.enterDate) = YEAR(getdate()) -1 THEN 1 ELSE 0 END as previousYear,
CASE WHEN YEAR(d.enterDate) = YEAR(getdate()) THEN 1 ELSE 0 END as thisYear
FROM Employees e
INNER JOIN dateDetails d on e.employeeID = d.EmployeeID
INNER JOIN IncomeSource i on e.employeeID = i.EmployeeID
WHERE EmpID=@empID
对于VB部分:
If dr.HasRows Then
'Employee exists'
If dr("previousYear").ToString() = "1"
'employee entered his information last year'
elseif dr("thisYear").ToString() = "1"
'employee entered his information this year'
else
'not his year, nor the previous year'
Else
'Employee doesn t exists'
End If