从一个 table 获取值以从另一个 table 提取值
Getting a value from one table to pull up values from another table
我正在尝试根据 windows 登录名从一个 table 中查找一个 employeeid,然后使用此 employeeid 从另一个 table 中获取值以将它们相加。因此,对于 Bill,在 tblEmployees 中的 employeeid 是 1。我在 employeeid 等于 1 的 tblTimeAvailable 中对所有 noofhours 求和,并将其显示在我的网页上。它不工作。我不知道如何通过在第一个 table 中找到的 employeeid 来搜索第二个 table。 (由于 sql 注入,我正在重写代码。)
Dim windowsLoginName As System.String = HttpContext.Current.User.Identity.Name 'System.Security.Principal.WindowsIdentity.GetCurrent().Name
Dim split As String() = Nothing
Dim vname As String
'Get network login name (name only)
split = windowsLoginName.Split("\".ToCharArray)
vname = split(1)
Dim Connection As String = "Data Source=WillSQL\ict2;Initial Catalog=TimeSQL;Integrated Security=SSPI"
Using con As New SqlConnection(Connection)
Dim sqlemp As String = "SELECT EmployeeID FROM tblEmployees where login = @loginname"
Dim command As New SqlCommand(sqlemp, con)
con.Open()
rve = cmde.Parameters.Add(New SqlParameter With {.ParameterName = "@loginname", .SqlDbType = SqlDbType.NVarChar, .Value = vname})
End Using
当我查看 rve 的值时,它给我的是@loginname 而不是 employeeid。仅供参考 - tblEmployees 中始终只有一行,因为每个 Windows 登录名都是唯一的。
'Get Sick Time
Using con As New SqlConnection(Connection)
Dim sqls1 As String = "Select SUM(NoofHours) as Total from tblTimeAvailable where workcode = 1 and EmployeeID = @employeeid"
Dim command As New SqlCommand(sqls1, con)
con.Open()
rvsa = cmde.Parameters.Add(New SqlParameter With {.ParameterName = "@employeeid", .SqlDbType = SqlDbType.NVarChar, .Value = rve})
End Using
' If the sum equals 0, show 0 on webpage. If another value, show value.
If IsDBNull(rvsa) Then
rvsa = 0
TextBoxsa.Text = 0
Else
TextBoxsa.Text = rvsa.ToString
End If
感谢您能给我的任何帮助!
您永远不会执行命令。设置各种参数的值后,您需要调用 SqlCommand
对象上的执行方法之一。在这种情况下,由于您只是从一行中读取一个值,您可以简单地使用 ExecuteScalar
方法:
rve = command.ExecuteScalar()
我正在尝试根据 windows 登录名从一个 table 中查找一个 employeeid,然后使用此 employeeid 从另一个 table 中获取值以将它们相加。因此,对于 Bill,在 tblEmployees 中的 employeeid 是 1。我在 employeeid 等于 1 的 tblTimeAvailable 中对所有 noofhours 求和,并将其显示在我的网页上。它不工作。我不知道如何通过在第一个 table 中找到的 employeeid 来搜索第二个 table。 (由于 sql 注入,我正在重写代码。)
Dim windowsLoginName As System.String = HttpContext.Current.User.Identity.Name 'System.Security.Principal.WindowsIdentity.GetCurrent().Name
Dim split As String() = Nothing
Dim vname As String
'Get network login name (name only)
split = windowsLoginName.Split("\".ToCharArray)
vname = split(1)
Dim Connection As String = "Data Source=WillSQL\ict2;Initial Catalog=TimeSQL;Integrated Security=SSPI"
Using con As New SqlConnection(Connection)
Dim sqlemp As String = "SELECT EmployeeID FROM tblEmployees where login = @loginname"
Dim command As New SqlCommand(sqlemp, con)
con.Open()
rve = cmde.Parameters.Add(New SqlParameter With {.ParameterName = "@loginname", .SqlDbType = SqlDbType.NVarChar, .Value = vname})
End Using
当我查看 rve 的值时,它给我的是@loginname 而不是 employeeid。仅供参考 - tblEmployees 中始终只有一行,因为每个 Windows 登录名都是唯一的。
'Get Sick Time
Using con As New SqlConnection(Connection)
Dim sqls1 As String = "Select SUM(NoofHours) as Total from tblTimeAvailable where workcode = 1 and EmployeeID = @employeeid"
Dim command As New SqlCommand(sqls1, con)
con.Open()
rvsa = cmde.Parameters.Add(New SqlParameter With {.ParameterName = "@employeeid", .SqlDbType = SqlDbType.NVarChar, .Value = rve})
End Using
' If the sum equals 0, show 0 on webpage. If another value, show value.
If IsDBNull(rvsa) Then
rvsa = 0
TextBoxsa.Text = 0
Else
TextBoxsa.Text = rvsa.ToString
End If
感谢您能给我的任何帮助!
您永远不会执行命令。设置各种参数的值后,您需要调用 SqlCommand
对象上的执行方法之一。在这种情况下,由于您只是从一行中读取一个值,您可以简单地使用 ExecuteScalar
方法:
rve = command.ExecuteScalar()