gpt4 book ai didi

vb.net - 为什么 "using"在一个函数中似乎被拒绝,但在另一个函数中可以?

转载 作者:行者123 更新时间:2023-12-05 00:52:23 27 4
gpt4 key购买 nike

使用此代码:

Protected Function GetArgValsForCompanyName(coName As String) As String()
Dim args(2) As String

Using con As New SqlConnection("SERVER=PLATYPUS42;DATABASE=duckbilldata;UID=durante;PWD=pondscum"),
cmd As New SqlCommand("select Unit, MemberNo, CustNo from Customers WHERE CompanyName = @CoName", con)

con.Open()

cmd.CommandType = CommandType.Text
cmd.Parameters.Add("@CoName", SqlDbType.VarChar, 50).Value = coName

Using reader As SqlDataReader = cmd.ExecuteReader

While reader.Read
args(0) = reader.Item(0).ToString()
args(1) = reader.Item(1).ToString()
args(2) = reader.Item(2).ToString()
End While

End Using

End Using

Return args
End Function

...我得到:
Server Error in '/EMS/customerreportingnet' Application.
--------------------------------------------------------------------------------
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30203: Identifier expected.

Source Error:

Line 94: Dim args(2) As String
Line 95:
Line 96: Using con As New SqlConnection("SERVER=PLATYPUS42;DATABASE=duckbilldata;UID=durante;PWD=pondscum"),
Line 97: cmd As New SqlCommand("select Unit, MemberNo, CustNo from Customers WHERE CompanyName = @CoName", con)
Line 98:

Source File: C:\EnhancedMonthlySalesReporting\customerreportingnet\customerreportingnet\pages\custmaint_categoryadmin.aspx.vb Line: 96

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.5485; ASP.NET Version:2.0.50727.5491

因此,“使用 con”行被牵连为问题。我注释掉了大部分代码,只返回一些随机值,它工作正常。

但是,使用了另一个“Using”:Button1_Click 事件处理程序使用了 using。它不会仅仅因为按钮没有被点击而提示吗?

实际上,当我单击按钮时,确实会出现错误,但这与使用无关;这是因为我显然在按钮处理的表单上有太多控件(实际上是数千个或标签/复选框对):
Server Error in '/EMS/customerreportingnet' Application.
--------------------------------------------------------------------------------

Operation is not valid due to the current state of the object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Operation is not valid due to the current state of the object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[InvalidOperationException: Operation is not valid due to the current state of the object.]
System.Web.HttpValueCollection.ThrowIfMaxHttpCollectionKeysExceeded() +4198079
System.Web.HttpValueCollection.FillFromEncodedBytes(Byte[] bytes, Encoding encoding) +60
System.Web.HttpRequest.FillInFormCollection() +189

[HttpException (0x80004005): The URL-encoded form data is not valid.]
System.Web.HttpRequest.FillInFormCollection() +11196408
System.Web.HttpRequest.get_Form() +119
System.Web.TraceContext.InitRequest() +1188
System.Web.TraceContext.VerifyStart() +133
System.Web.HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +11307449
System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr) +452



--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.5485; ASP.NET Version:2.0.50727.5491

那么第一 block 代码中的问题真的是“使用”吗?有没有办法仍然使用它,还是我需要恢复到旧的 [fangl,fashion] ed 创建对象的方式?

最佳答案

在 VB.NET 中,从编译器的角度来看,行继续符允许将两行视为一行。该字符是下划线,应该是该行的最后一个字符。

最新版本的 VB.NET 编译器已在很大程度上消除了这一要求,该编译器具有足够的智能来理解语句的结束位置。
例如,在 LinqPAD 5.10 中,不需要在两个 using 语句之间使用此字符。

相反,您当前的编译器似乎无法理解这一点,添加延续字符应该可以解决问题

Using con As New SqlConnection("SERVER=PLATYPUS42;DATABASE=duckbilldata;UID=durante;PWD=pondscum"), _
cmd As New SqlCommand("select Unit, MemberNo, CustNo from Customers WHERE CompanyName = @CoName", con)

关于vb.net - 为什么 "using"在一个函数中似乎被拒绝,但在另一个函数中可以?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42938121/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com