问题描述
我正在支持工作中的申请。下面是一些代码段
,我无法理解它们是如何工作的。首先让我说,我永远不会以b $ b代码这个标准。我真的好不知所措。
这就是设置。我有一个名为GetEmployeeCertifications的函数,
将调用SQL DB并返回结果集。这个
模块调用另一个函数,称为FillParameters,以确定是否需要将
SQL参数附加到命令对象。
FillParameters函数......
没有返回值
命令对象未全局定义
并且命令对象按值传递而不是引用
但是GetEmployeeCertifications函数中的命令对象
已附加到它来自FillParameters
函数的参数对象。怎么会这样?
公共共享功能GetEmployeeCertifications(ByVal条件为
SearchCriteria)As EmployeeCertificationCollection
Dim tblEmployees As New DataTable
Dim connection As New SqlConnection(connectionString)
Dim cmdGetEmployees As New SqlCommand
Dim myDataAdapter As New SqlDataAdapter
昏昏欲睡的员工作为新员工认证收集
尝试
connection.Open()
cmdGetEmployees.Connection = connection
cmdGetEmployees.CommandText =" GetEmployeeCertifications"
cmdGetEmployees.CommandType = CommandType.StoredProcedure
FillParameters(criteria,cmdGetEmployees)''< ---这是没有作业的
电话
myDataAdapter.SelectCommand = cmdGetEmployees
myDataAdapter.Fill(tblEmployees)
Catch ex As Exception
最后
''转换匹配的数据行为
EmployeeCertificationCollection
如果不是tblEmployees什么都没有那么
Dim row As DataRow
For每一行在tblEmployees.Rows
employees.Add(GetEmployeeCertification(row))
下一页
结束如果
结束尝试
''如果他们按成本中心搜索,则返回按成本中心排序的结果
。
''结果从按姓氏排序的数据库返回。
如果criteria.CostCenterFrom.Length 0或
criteria.CostCenterTo.Length 0那么
employees.ApplySort(" CostCenter",
ComponentModel.ListSortDirection.Ascending)
结束如果
返回员工
结束函数
私有共享函数FillParameters(ByVal条件为
SearchCriteria,ByVal cmd As SqlCommand)
如果criteria.LastName.Length 0那么
cmd.Parameters.Add(New SqlParamete r(" @ LastName",
SqlEscape(criteria.LastName)))
结束如果
如果不是(条件.SiteCode =" ALL"或者criteria.SiteCode.Length =
0)然后
cmd.Parameters.Add(新的SqlParameter(" @ SiteCode",
条件) .SiteCode))
结束如果
结束功能
I''m supporting an application at work. Below are some code segments
that I can''t understand how they work. First let me say, I would never
code this standard. I''m just really creeped out that it works.
Here''s the setup. I have a function, called GetEmployeeCertifications,
that is going to make a call to a SQL DB and return a result set. This
module calls another function, called FillParameters, to determine if
SQL parameters need to be appended to the command object. The
FillParameters function...
does not have a return value
the command object is not globally defined
and the command object is passed by value not reference
But yet the command object, in the GetEmployeeCertifications function,
has appended to it the parameter objects from the FillParameters
function. How can that be?
Public Shared Function GetEmployeeCertifications(ByVal criteria As
SearchCriteria) As EmployeeCertificationCollection
Dim tblEmployees As New DataTable
Dim connection As New SqlConnection(connectionString)
Dim cmdGetEmployees As New SqlCommand
Dim myDataAdapter As New SqlDataAdapter
Dim employees As New EmployeeCertificationCollection
Try
connection.Open()
cmdGetEmployees.Connection = connection
cmdGetEmployees.CommandText = "GetEmployeeCertifications"
cmdGetEmployees.CommandType = CommandType.StoredProcedure
FillParameters(criteria, cmdGetEmployees) ''<---Here is the
call with no assignment
myDataAdapter.SelectCommand = cmdGetEmployees
myDataAdapter.Fill(tblEmployees)
Catch ex As Exception
Finally
''convert the matching data rows to an
EmployeeCertificationCollection
If Not tblEmployees Is Nothing Then
Dim row As DataRow
For Each row In tblEmployees.Rows
employees.Add(GetEmployeeCertification(row))
Next
End If
End Try
''If they searched by cost center, then return the results
sorted by cost center.
''The results come back from the database ordered by last name.
If criteria.CostCenterFrom.Length 0 Or
criteria.CostCenterTo.Length 0 Then
employees.ApplySort("CostCenter",
ComponentModel.ListSortDirection.Ascending)
End If
Return employees
End Function
Private Shared Function FillParameters(ByVal criteria As
SearchCriteria, ByVal cmd As SqlCommand)
If criteria.LastName.Length 0 Then
cmd.Parameters.Add(New SqlParameter("@LastName",
SqlEscape(criteria.LastName)))
End If
If Not (criteria.SiteCode = "ALL" Or criteria.SiteCode.Length =
0) Then
cmd.Parameters.Add(New SqlParameter("@SiteCode",
criteria.SiteCode))
End If
End Function
推荐答案
基本上你对参数传递感到困惑。请参阅
这是用C#编写的,但原理是一样的 - 即使是
a参数按值传递,如果它是引用类型(例如
SqlCommand),则它是传递的* reference *。在
方法终止后,调用者仍然可以看到通过该引用对对象进行的更改
。
-
Jon Skeet - < sk *** @ pobox.com>
博客:
如果回复小组,请不要给我发邮件
You''re confused about parameter passing, basically. See
http://www.pobox.com/~skeet/csharp/parameters.html
It''s written in terms of C#, but the principle is the same - even when
a parameter is passed by value, if it''s a reference type (such as
SqlCommand) it''s the *reference* which is passed. Changes to the object
made via that reference are still visible to the caller after the
method has terminated.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
这是一个参考电话。一个Command对象总是通过引用。
所以使用引用填充参数。
Jesse
This is a call by reference. A Command object is by reference always.
So the parameters are filled using the reference.
Jesse
这是通过引用打电话。 Command对象始终是引用。
This is a call by reference. A Command object is by reference always.
不,对象根本没有通过。通过引用传递
和按值传递引用之间存在差异。请参阅
了解更多信息。
-
Jon Skeet - < sk *** @ pobox .com>
博客: http://www.msmvps.com/jon.skeet
如果回复小组,请不要给我发邮件
No, the object isn''t passed at all. There''s a different between passing
an object by reference and passing a reference by value. See
http://www.pobox.com/~skeet/csharp/parameters.html for more on this.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
这篇关于为什么这段代码有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!