为什么 "using" 在一个函数中看似被拒绝,但在另一个函数中却没问题?

Why is "using" seemingly rejected in one function, but okay in another?

使用此代码:

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 

因此,"using con" 行与问题有关。我将大部分代码注释掉,只留下 return 一些随机值,效果很好。

但是,还有另一个 "Using" 正在使用:Button1_Click 事件处理程序使用 using。它不只是因为按钮没有被点击而抱怨吗?

实际上,当我点击按钮时,我确实得到了一个错误,但这与使用无关;这是因为我显然在按钮处理的表单上有太多控件(实际上是数千或 label/checkbox 对):

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

那么 "using" 真的是第一段代码中的问题吗?有没有办法继续使用它,或者我是否需要恢复到旧的 [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)