嘿,我在这段代码中得到了这个错误,我不确定为什么。这将是一个很大的帮助,因为我正在尝试使我的代码更容易阅读
Public Function SaveProperty() As Boolean
'** Save Current Personal Data Record
' Error Checking
On Error GoTo Err_SaveProperty
' Dimension Local Variables
Dim uRecSnap As ADODB.Recordset
Dim uPar As ADODB.Parameter
' Check For Open Connection
If uDBase Is Nothing Then
OpenConnection()
bConnection = True
End If
' Run Stored Procedure - Save Property Record
uCommand = New ADODB.Command
With uCommand
.ActiveConnection = uDBase
.CommandType = ADODB.CommandTypeEnum.adCmdStoredProc
.CommandTimeout = 0
.Parameters.Append.CreateParameter("@PropertyID", ADODB.DataTypeEnum.adInteger, ADODB.ParameterDirectionEnum.adParamInput, 50, Val(lblPropertyIDValue.Text))
.Parameters.Append.CreateParameter("@PropertyManager", ADODB.DataTypeEnum.adLongVarChar, ADODB.ParameterDirectionEnum.adParamInput, 60, cmbPropertyManager.Text)
.Parameters.Append.CreateParameter("@AddressLine1", ADODB.DataTypeEnum.adLongVarChar, ADODB.ParameterDirectionEnum.adParamInput, 30, txtAddress1.Text)
.Parameters.Append.CreateParameter("@AddressLine2", ADODB.DataTypeEnum.adLongVarChar, ADODB.ParameterDirectionEnum.adParamInput, 30, txtAddress2.Text
'...ETC
.CommandText = "PropertyMaster_SaveRecord"
.Execute()
End With
' Close Connection
uRecSnap = Nothing
uCommand = Nothing
If bConnection Then CloseConnection()
SaveProperty = True
Err_SaveProperty:
If Err.Number <> 0 Then
sErrDescription = Err.Description
WriteAuditLogRecord("clsProperty", "SaveProperty", "Error", sErrDescription)
SaveProperty = False
End IfEnd函数
我已经将代码行数从如下所示减少了
uPar = .CreateParameter("@LandlordID", ADODB.DataTypeEnum.adInteger, ADODB.ParameterDirectionEnum.adParamInput)
.Parameters.Append(uPar)
.Parameters("@LandlordID").Value = Val(lblLandlordID.Text)发布于 2015-01-24 01:28:40
欢迎使用精彩的With语句的注意事项!
正如@RubberDuck所暗示的,你的问题在这里:
.Parameters.Append.CreateParameter(...)应该是:
.Parameters.Append .CreateParameter(...)如果没有With块,就会有这样的结果:
uCommand.Parameters.Append uCommand.CreateParameter(...)Append和CreateParameter都是uCommand块正在使用的With对象的成员。
发布于 2015-01-24 05:35:26
如果你真的想让你的代码更易读,只需这样做:
' Run Stored Procedure - Save Property Record
Set uCommand = New ADODB.Command 'Note use of Set keyword
With uCommand
.ActiveConnection = uDBase
.CommandType = adCmdStoredProc 'Don't need the entire object hierarchy here
.CommandTimeout = 0
.CommandText = "PropertyMaster_SaveRecord"
.Parameters.Refresh
.Parameters(1) = cmbPropertyManager.Text 'Collections are usually 1-based in VB6
.Parameters(2) = txtAddress1.Text
.Parameters(3) = txtAddress2.Text
'...ETC
.Execute()
End With这确实需要额外的往返行程来查找参数。但是,如果您能够负担得起性能开销,这是一种更容易的方法。此外,它还减少了潜在的维护开销,因为它与存储的proc的参数列表没有那么紧密地耦合;例如,您可以重命名存储的proc中的参数,而不必担心破坏代码。
还要注意,在使用枚举时,您不需要详细说明枚举的整个对象层次结构;事实上,我以前从未见过这样做。所以不这样做也会让你的代码更容易阅读。
接下来,在实例化新对象时使用Set关键字。
最后,替换为:
If uDBase Is Nothing Then
OpenConnection()
bConnection = True
End If有了这个:
If uDBase.State = adStateClosed Then
OpenConnection()
bConnection = True '?? Probably don't need this, see below
End If您不希望依赖于检查对象是否为nothing的原因是,您可以在代码中的其他位置关闭连接,而忘记将uDBase设置为nothing。这可能会产生一个很难找到的bug。此外,如果您拥有状态变量的唯一原因是因为您使用它来跟踪您的连接是否打开,那么您可以安全地删除它,只需使用bConnection属性。
进一步的改进是将您的OpenConnection例程更改为GetConnection例程,并将所有打开连接的检查等封装在其中。如下所示:
Public Function GetConnection() As ADODB.Connection
Static myConn As ADODB.Connection
If myConn.State = adStateClosed Then
Set myConn = New ADODB.Connection
myConn.Open "myConnectionString"
End If
GetConnection = myConn
End Function这假设您在整个应用程序中使用一个连接,并且它在应用程序运行时保持打开状态。这在uDBase变量的作用域中是隐含的。当然,还有更经济的方法来管理连接,您可能需要考虑这些方法,这取决于您正在处理的流量。
https://stackoverflow.com/questions/28114683
复制相似问题