简体   繁体   中英

SQL Server 2012 Express connection successful but error occured during login

I get the following errors accessing my local SQL Server 2012 Express data using VB.Net 2012.

A connection was successfully established with the server, but then an error occurred during the login process. (provider: TCP Provider, error: 0 - The specified network name is no longer available.

A connection was successfully established with the server, but then an error occurred during the login process. (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.

It seems like the connection is not being released because I have a function with the exact same code (except parameters) and it works 3 times, but on the 4th I get an error like above.

Code:

Public Function LookupSQRIDX(Lotnumber As String, TableToUse As String, IDXFieldToUse As String, LotField As String, LotMatch As String, SearchLine As String) As String
    Dim sConnection As String = "Server=MARIO\VSQL;Database=LEASE;User Id=cookieuser;Password=oreo;"
    Dim objCommand As New SqlCommand


    objCommand.CommandText = "Select top 1 " & IDXFieldToUse & " From [dbo].[" & TableToUse & "] where " & LotField & " like '%" & LotMatch & "' and line = '" & SearchLine & "' order by " & IDXFieldToUse
    objCommand.Connection = New SqlConnection(sConnection)
    objCommand.Connection.Open()
    Dim objDataReader As SqlDataReader = objCommand.ExecuteReader()
    Try
        Dim NumCols, NumRows As Integer
        Dim RowNum As Integer
        Dim MyArray = ""
        Dim Map1LastLot = "", Map2LastLot = ""

        If objDataReader.HasRows Then
            NumCols = objDataReader.FieldCount - 1
            Do While objDataReader.Read()
                RowNum = RowNum + 1
                MyArray = objDataReader(0).ToString
            Loop
            NumRows = RowNum
            objDataReader.Close()
            objCommand.Dispose()
            Return MyArray
        Else
            objDataReader.Close()
            objCommand.Dispose()
            Return ""
        End If

    Catch ex As Exception
        MessageBox.Show("Error: " & ex.Message)
        objDataReader.Close()
        objCommand.Dispose()
        Return ""
    End Try

    objDataReader.Close()

    objCommand.Dispose()

End Function

Added the following (using the tip provided by hvd) and the problem went away. Evidently you need to close and dispose of the connection even though it is already contained in the objCommand being disposed.

       objCommand.Connection.Close()
       objCommand.Connection.Dispose()
       objDataReader.Close()
       objCommand.Dispose()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM