martincostello/sqllocaldb

Return values

regattamaster opened this issue · 6 comments

How does one get the return values from a call to the API? For instance, sqllocaldbapi.ShareInstance(dbInstance, dbSharedInstance) fails but does not return an error.

Could you give a bit more detail about how it is failing? Any non-zero return code from the SQL Instance API method for sharing should cause an exception to be thrown.

You can turn on logging to see what's happening under the hood like this:

<system.diagnostics>
  <sharedListeners>
    <add name="Console" type="System.Diagnostics.ConsoleTraceListener" />
  </sharedListeners>
  <sources>
    <source name="System.Data.SqlLocalDb">
      <listeners>
        <add name="Console" />
      </listeners>
    </source>
  </sources>
  <switches>
    <add name="System.Data.SqlLocalDb" value="Verbose" />
  </switches>
  <trace autoflush="true" />
</system.diagnostics>

You should get output similar to this:

2016-12-07 10:15:49,743 DEBUG System.Data.SqlLocalDb - Sharing SQL LocalDB instance '{Name}' for owner SID '{SID}'. Shared instance name: '{SharedName}'.
2016-12-07 10:15:49,745 DEBUG System.Data.SqlLocalDb - Shared SQL LocalDB instance '{Name}' for owner SID '{SID}' as '{SharedName}'.
2016-12-07 10:15:50,042 DEBUG System.Data.SqlLocalDb - Stopping sharing SQL LocalDB instance '{Name}'.
2016-12-07 10:15:50,043 DEBUG System.Data.SqlLocalDb - Stopped sharing SQL LocalDB instance '{Name}'.

@regattamaster I'd suggest an approach similar to the snippet below by catching SqlLocalDbException which is thrown if the operation is not successful, and then inspecting its ErrorCode property. You can use the constants in the SqlLocalDbErrors class to compare against specific errors that you want to ignore/handle.

Imports System.Data.SqlLocalDb

Friend Module Program

    Friend Sub Main()

        Dim dbInstance As String = "MyName"
        Dim dbSharedInstance As String = "MySharedName"

        Try
            ' Try to share the instance
            SqlLocalDbApi.ShareInstance(dbInstance, dbSharedInstance)

        Catch ex As SqlLocalDbException

            ' Inspect the ErrorCode associated with the exception against the SQL LocalDB error codes
            If (ex.ErrorCode <> SqlLocalDbErrors.AdminRightsRequired) Then

                ' Do something with the errors you are or are not interested in
                Throw

            End If

        End Try

    End Sub

End Module

@regattamaster No problem - are you happy with me closing this issue?