加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

vb.net – 为什么“使用”似乎在一个函数中被拒绝,但在另一个函

发布时间:2020-12-17 07:17:25 所属栏目:百科 来源:网络整理
导读:使用此代码: 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 U
使用此代码:

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,con)
Line 98: 

Source File: C:EnhancedMonthlySalesReportingcustomerreportingnetcustomerreportingnetpagescustmaint_categoryadmin.aspx.vb    Line: 96     

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

因此,“使用con”行被认为是问题所在.我注释掉了大部分代码只返回一些随机值,它运行正常.

但是,还有另一个“使用”: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

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

解决方法

在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,con)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读