Vb.Net : LogonMode and StartMode of SQLEXPRESS and SQLBROWSER

137 views Asked by At

Using VB.net we are suppose to change the LogonMode(Not LoginMode) and StartMode of Sql Express and Sql Browser.

What we mean is :
If(StartMode of Sql Express <> Automatic)Then
Make it Automatic
End If

If(StartMode of Sql Browser <> Automatic)Then
Make it Automatic
End If

If(LogonMode of Sql Express <> NetworkService)Then
Make it NetworkService
End If

If(LogonMode of Sql Browser <> NetworkService)Then
Make it NetworkService
End If

If this can be done by changing any key value in RegEdit, that would go easier.
Is there any way to do this using vb.net?

1

There are 1 answers

0
SHREE On BEST ANSWER

In RegEdit we could finally find Sql Express and Sql Browser Service values.
Link : HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\SQLBrowser\
Logon Mode is : ObjectName
Start Mode is : Start(1:ChangePending, 2:Automatic, 3: Manual, 4: Disabled)

Using the following Vb.net code we could settle the problem.

Imports Microsoft.Win32 

Private Sub ChangeSettings()
    Dim RegKey As RegistryKey

    'Changes for SQL Browser.'
    RegKey = Registry.LocalMachine.OpenSubKey("System\\CurrentControlSet\\Services\\SQLBrowser", True)
    If (RegKey.GetValue("ObjectName") <> "NT AUTHORITY\NetworkService") Then
        RegKey.SetValue("ObjectName", "NT AUTHORITY\NetworkService", RegistryValueKind.String)
    End If
    If (RegKey.GetValue("Start") <> 2) Then
        RegKey.SetValue("Start", 2, RegistryValueKind.DWord)
    End If

    'Changes for SQL Express.'
    RegKey = Registry.LocalMachine.OpenSubKey("System\\CurrentControlSet\\Services\\MSSQL$SQLEXPRESS", True)
    If (RegKey.GetValue("ObjectName") <> "NT AUTHORITY\NetworkService") Then
        RegKey.SetValue("ObjectName", "NT AUTHORITY\NetworkService", RegistryValueKind.String)
    End If
    If (RegKey.GetValue("Start") <> 2) Then
        RegKey.SetValue("Start", 2, RegistryValueKind.DWord)
    End If
End Sub