none
install and create MS SQL instance and DB with my application RRS feed

  • Question

  • Hello,

    I have an application that work with SQL server 2008 Database and my problem is that i'm so limited with the market here as i'm from a small town and obviously i need to go in person to install the SQL server and DB for each customer.

    I want to go online and offer to the customer a free trail of my app, I manged to make my ConectionString Dynamical as it contain the machine name but, still must have the same instance name, User and Password.

    So is there any way to install and create MS SQL instance,DB,Users and Password with my application or my App Installer as ".msi"?

    really appreciate the reply as it will help me a lot.
    • Edited by AbanoubZak Thursday, February 13, 2020 11:16 AM
    Thursday, February 13, 2020 11:14 AM

Answers

  • Hello,

    I don't have a fully working code sample but can give you a direction. Using a connection and command objects and following documentation you can create a database, user and login (along with if needed to create tables and populate them).

    So for instance to create a user and login with on the fly variables

    Dim databaseName As String = "MyDatabase"
    Dim login As String = "Payne"
    Dim password As String = "MyPassword"
    Dim script =
            <SQL>
                CREATE DATABASE [<%= databaseName %>]
                CREATE LOGIN [<%= login %>] WITH PASSWORD=N'<%= password %>', DEFAULT_DATABASE=[<%= databaseName %>]
                ALTER SERVER ROLE [sysadmin] ADD MEMBER [<%= login %>]
                USE [<%= databaseName %>]
                CREATE USER [<%= login %>] FOR LOGIN [<%= login %>]
                ALTER USER [<%= login %>] WITH DEFAULT_SCHEMA=[dbo]
                ALTER ROLE [db_owner] ADD MEMBER [<%= login %>]
            </SQL>.Value
    

    We get

                    

    CREATE DATABASE [MyDatabase]
    CREATE LOGIN [Payne] WITH PASSWORD=N'MyPassword', DEFAULT_DATABASE=[MyDatabase]
    ALTER SERVER ROLE [sysadmin] ADD MEMBER [Payne]
    USE [MyDatabase]
    CREATE USER [Payne] FOR LOGIN [Payne]
    ALTER USER [Payne] WITH DEFAULT_SCHEMA=[dbo]
    ALTER ROLE [db_owner] ADD MEMBER [Payne]

    If the database already exists then the first line would not be needed.

    I would script out the data and create the database rather than present it as part of the install where the script can be generated in SSMS and altered e.g. remove all GO statements.

    Bottom line is there is a bit of work involved but by taking your time it can be done.

    In regards to the installer look at using this one which can take time to learn, we use it for large installs.

    https://marketplace.visualstudio.com/items?itemName=VisualStudioClient.MicrosoftVisualStudio2017InstallerProjects


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by AbanoubZak Thursday, February 13, 2020 4:30 PM
    • Unmarked as answer by AbanoubZak Thursday, February 13, 2020 6:42 PM
    • Marked as answer by AbanoubZak Friday, February 14, 2020 8:50 AM
    Thursday, February 13, 2020 11:37 AM
    Moderator
  • There are several ways to do this, here I'm using My.Settings to store the server name. So the first time through it will take a while but the app will remain responsive.

    You may need to modify the part checking for SQLEXPRESS needing .\

    Also You will need to get the device name which I'm not sure what that is too you.

    Utility/helper class

    Imports System.Data.Sql
    
    Public Class SqlServerHelpers
        Public Async Function SqlServerIsAvailable() As Task(Of Boolean)
    
            Dim success As Boolean = False
    
            Try
                Await Task.Run(
                    Sub()
                        Dim sqlDataSourceEnumeratorInstance As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance
                        Dim dt As DataTable = sqlDataSourceEnumeratorInstance.GetDataSources()
                        If dt IsNot Nothing Then
                            success = True
                        End If
                    End Sub)
            Catch ex As Exception
    
            End Try
    
            Return success
    
        End Function
        Public Async Function DefaultServerName() As Task(Of String)
    
            Dim success As String = ""
    
            Await Task.Run(
                Sub()
                    Dim sqlDataSourceEnumeratorInstance As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance
                    Dim dt As DataTable = sqlDataSourceEnumeratorInstance.GetDataSources()
                    If dt IsNot Nothing Then
                        Dim dataRowTest = dt.AsEnumerable().FirstOrDefault(
                            Function(row)
                                Return Not String.IsNullOrWhiteSpace(row.Field(Of String)("InstanceName"))
                            End Function)
                        If dataRowTest IsNot Nothing Then
                            success = dataRowTest.Field(Of String)("InstanceName")
                        End If
                    End If
                End Sub)
    
            Return success
    
        End Function
    End Class

    Sample code to try this out

    Public Class Form1
        ''' <summary>
        ''' Requires My.Setting.ServerName setting
        ''' This code is shown in a button click event but could be
        ''' where ever you want this code to run
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Button1.Enabled = False
            Dim ConnectionString As String
    
            If String.IsNullOrWhiteSpace(My.Settings.ServerName) Then
                Dim helper = New SqlServerHelpers
                Dim result = Await helper.SqlServerIsAvailable()
                If result Then
    
                    Dim serverName = Await helper.DefaultServerName()
    
                    If Not String.IsNullOrWhiteSpace(serverName) Then
    
                        If serverName = "SQLEXPRESS" Then
                            serverName = $".\{serverName}"
                        End If
    
                        My.Settings.ServerName = serverName
                        My.Settings.Save()
    
                        ConnectionString = $"Data Source={My.Settings.ServerName};Initial Catalog={My.Settings.Catalog};Integrated Security=True"
                    End If
                Else
                    '
                    ' SQL Server is not running - need to figure out why,
                    ' is it installed and set to fire up on windows startup?
                    '
                End If
            End If
    
            If String.IsNullOrWhiteSpace(ConnectionString) AndAlso Not String.IsNullOrWhiteSpace(My.Settings.ServerName) Then
                ConnectionString = $"Data Source={My.Settings.ServerName};Initial Catalog={My.Settings.Catalog};Integrated Security=True"
            Else
                '
                ' Something went wrong, you need to figure out the issue
                '
            End If
    
            Console.WriteLine(ConnectionString)
            Button1.Enabled = True
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            My.Settings.ServerName = ""
            My.Settings.Save()
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Thursday, February 13, 2020 3:50 PM
    Moderator

All replies

  • Hello,

    I don't have a fully working code sample but can give you a direction. Using a connection and command objects and following documentation you can create a database, user and login (along with if needed to create tables and populate them).

    So for instance to create a user and login with on the fly variables

    Dim databaseName As String = "MyDatabase"
    Dim login As String = "Payne"
    Dim password As String = "MyPassword"
    Dim script =
            <SQL>
                CREATE DATABASE [<%= databaseName %>]
                CREATE LOGIN [<%= login %>] WITH PASSWORD=N'<%= password %>', DEFAULT_DATABASE=[<%= databaseName %>]
                ALTER SERVER ROLE [sysadmin] ADD MEMBER [<%= login %>]
                USE [<%= databaseName %>]
                CREATE USER [<%= login %>] FOR LOGIN [<%= login %>]
                ALTER USER [<%= login %>] WITH DEFAULT_SCHEMA=[dbo]
                ALTER ROLE [db_owner] ADD MEMBER [<%= login %>]
            </SQL>.Value
    

    We get

                    

    CREATE DATABASE [MyDatabase]
    CREATE LOGIN [Payne] WITH PASSWORD=N'MyPassword', DEFAULT_DATABASE=[MyDatabase]
    ALTER SERVER ROLE [sysadmin] ADD MEMBER [Payne]
    USE [MyDatabase]
    CREATE USER [Payne] FOR LOGIN [Payne]
    ALTER USER [Payne] WITH DEFAULT_SCHEMA=[dbo]
    ALTER ROLE [db_owner] ADD MEMBER [Payne]

    If the database already exists then the first line would not be needed.

    I would script out the data and create the database rather than present it as part of the install where the script can be generated in SSMS and altered e.g. remove all GO statements.

    Bottom line is there is a bit of work involved but by taking your time it can be done.

    In regards to the installer look at using this one which can take time to learn, we use it for large installs.

    https://marketplace.visualstudio.com/items?itemName=VisualStudioClient.MicrosoftVisualStudio2017InstallerProjects


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by AbanoubZak Thursday, February 13, 2020 4:30 PM
    • Unmarked as answer by AbanoubZak Thursday, February 13, 2020 6:42 PM
    • Marked as answer by AbanoubZak Friday, February 14, 2020 8:50 AM
    Thursday, February 13, 2020 11:37 AM
    Moderator
  • https://www.entityframeworktutorial.net/code-first/migration-in-code-first.aspx

    You could use Entity Framework migrations.

    What is EF?

    https://www.entityframeworktutorial.net/what-is-entityframework.aspx

    You could install MS SQL Server Localdb MSI through another MSI.

    On the other hand, you could use SQLite and not use MS SQL Server, which would be more appropriate. EF works with SQLite too.

    Of course there are VB examples of using EF by using Bing or Google.

    Thursday, February 13, 2020 2:19 PM
  • unfortunately using something instead of MS SQL Server is not an option as it will make me Redo a 6 month Work.
    Thursday, February 13, 2020 3:02 PM
  • Hello,

    I don't have a fully working code sample but can give you a direction. Using a connection and command objects and following documentation you can create a database, user and login (along with if needed to create tables and populate them).

    So for instance to create a user and login with on the fly variables

    Dim databaseName As String = "MyDatabase"
    Dim login As String = "Payne"
    Dim password As String = "MyPassword"
    Dim script =
            <SQL>
                CREATE DATABASE [<%= databaseName %>]
                CREATE LOGIN [<%= login %>] WITH PASSWORD=N'<%= password %>', DEFAULT_DATABASE=[<%= databaseName %>]
                ALTER SERVER ROLE [sysadmin] ADD MEMBER [<%= login %>]
                USE [<%= databaseName %>]
                CREATE USER [<%= login %>] FOR LOGIN [<%= login %>]
                ALTER USER [<%= login %>] WITH DEFAULT_SCHEMA=[dbo]
                ALTER ROLE [db_owner] ADD MEMBER [<%= login %>]
            </SQL>.Value

    We get

                    

    CREATE DATABASE [MyDatabase]
    CREATE LOGIN [Payne] WITH PASSWORD=N'MyPassword', DEFAULT_DATABASE=[MyDatabase]
    ALTER SERVER ROLE [sysadmin] ADD MEMBER [Payne]
    USE [MyDatabase]
    CREATE USER [Payne] FOR LOGIN [Payne]
    ALTER USER [Payne] WITH DEFAULT_SCHEMA=[dbo]
    ALTER ROLE [db_owner] ADD MEMBER [Payne]

    If the database already exists then the first line would not be needed.

    I would script out the data and create the database rather than present it as part of the install where the script can be generated in SSMS and altered e.g. remove all GO statements.

    Bottom line is there is a bit of work involved but by taking your time it can be done.

    In regards to the installer look at using this one which can take time to learn, we use it for large installs.

    https://marketplace.visualstudio.com/items?itemName=VisualStudioClient.MicrosoftVisualStudio2017InstallerProjects


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    my exmple for connetionstring is 

    Server=DeviceName\ServerName;Database=DataBaseName;UserId=User;Password=1234;MultipleActiveResultSets=true

    your code will help me to create DB for sure but i will still be stuck with the creation of SQL instance,in my case "ServerName".

    i search many time and found something about using clickonce and publish i believe this will help to download and install the program my app needs like net framework and so one but how i create a custom SQL instance.

    thank you.


    Thursday, February 13, 2020 3:05 PM
  • There are several ways to do this, here I'm using My.Settings to store the server name. So the first time through it will take a while but the app will remain responsive.

    You may need to modify the part checking for SQLEXPRESS needing .\

    Also You will need to get the device name which I'm not sure what that is too you.

    Utility/helper class

    Imports System.Data.Sql
    
    Public Class SqlServerHelpers
        Public Async Function SqlServerIsAvailable() As Task(Of Boolean)
    
            Dim success As Boolean = False
    
            Try
                Await Task.Run(
                    Sub()
                        Dim sqlDataSourceEnumeratorInstance As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance
                        Dim dt As DataTable = sqlDataSourceEnumeratorInstance.GetDataSources()
                        If dt IsNot Nothing Then
                            success = True
                        End If
                    End Sub)
            Catch ex As Exception
    
            End Try
    
            Return success
    
        End Function
        Public Async Function DefaultServerName() As Task(Of String)
    
            Dim success As String = ""
    
            Await Task.Run(
                Sub()
                    Dim sqlDataSourceEnumeratorInstance As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance
                    Dim dt As DataTable = sqlDataSourceEnumeratorInstance.GetDataSources()
                    If dt IsNot Nothing Then
                        Dim dataRowTest = dt.AsEnumerable().FirstOrDefault(
                            Function(row)
                                Return Not String.IsNullOrWhiteSpace(row.Field(Of String)("InstanceName"))
                            End Function)
                        If dataRowTest IsNot Nothing Then
                            success = dataRowTest.Field(Of String)("InstanceName")
                        End If
                    End If
                End Sub)
    
            Return success
    
        End Function
    End Class

    Sample code to try this out

    Public Class Form1
        ''' <summary>
        ''' Requires My.Setting.ServerName setting
        ''' This code is shown in a button click event but could be
        ''' where ever you want this code to run
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Button1.Enabled = False
            Dim ConnectionString As String
    
            If String.IsNullOrWhiteSpace(My.Settings.ServerName) Then
                Dim helper = New SqlServerHelpers
                Dim result = Await helper.SqlServerIsAvailable()
                If result Then
    
                    Dim serverName = Await helper.DefaultServerName()
    
                    If Not String.IsNullOrWhiteSpace(serverName) Then
    
                        If serverName = "SQLEXPRESS" Then
                            serverName = $".\{serverName}"
                        End If
    
                        My.Settings.ServerName = serverName
                        My.Settings.Save()
    
                        ConnectionString = $"Data Source={My.Settings.ServerName};Initial Catalog={My.Settings.Catalog};Integrated Security=True"
                    End If
                Else
                    '
                    ' SQL Server is not running - need to figure out why,
                    ' is it installed and set to fire up on windows startup?
                    '
                End If
            End If
    
            If String.IsNullOrWhiteSpace(ConnectionString) AndAlso Not String.IsNullOrWhiteSpace(My.Settings.ServerName) Then
                ConnectionString = $"Data Source={My.Settings.ServerName};Initial Catalog={My.Settings.Catalog};Integrated Security=True"
            Else
                '
                ' Something went wrong, you need to figure out the issue
                '
            End If
    
            Console.WriteLine(ConnectionString)
            Button1.Enabled = True
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            My.Settings.ServerName = ""
            My.Settings.Save()
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Thursday, February 13, 2020 3:50 PM
    Moderator
  • There are several ways to do this, here I'm using My.Settings to store the server name. So the first time through it will take a while but the app will remain responsive.

    You may need to modify the part checking for SQLEXPRESS needing .\

    Also You will need to get the device name which I'm not sure what that is too you.

    Utility/helper class

    Imports System.Data.Sql
    
    Public Class SqlServerHelpers
        Public Async Function SqlServerIsAvailable() As Task(Of Boolean)
    
            Dim success As Boolean = False
    
            Try
                Await Task.Run(
                    Sub()
                        Dim sqlDataSourceEnumeratorInstance As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance
                        Dim dt As DataTable = sqlDataSourceEnumeratorInstance.GetDataSources()
                        If dt IsNot Nothing Then
                            success = True
                        End If
                    End Sub)
            Catch ex As Exception
    
            End Try
    
            Return success
    
        End Function
        Public Async Function DefaultServerName() As Task(Of String)
    
            Dim success As String = ""
    
            Await Task.Run(
                Sub()
                    Dim sqlDataSourceEnumeratorInstance As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance
                    Dim dt As DataTable = sqlDataSourceEnumeratorInstance.GetDataSources()
                    If dt IsNot Nothing Then
                        Dim dataRowTest = dt.AsEnumerable().FirstOrDefault(
                            Function(row)
                                Return Not String.IsNullOrWhiteSpace(row.Field(Of String)("InstanceName"))
                            End Function)
                        If dataRowTest IsNot Nothing Then
                            success = dataRowTest.Field(Of String)("InstanceName")
                        End If
                    End If
                End Sub)
    
            Return success
    
        End Function
    End Class

    Sample code to try this out

    Public Class Form1
        ''' <summary>
        ''' Requires My.Setting.ServerName setting
        ''' This code is shown in a button click event but could be
        ''' where ever you want this code to run
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Button1.Enabled = False
            Dim ConnectionString As String
    
            If String.IsNullOrWhiteSpace(My.Settings.ServerName) Then
                Dim helper = New SqlServerHelpers
                Dim result = Await helper.SqlServerIsAvailable()
                If result Then
    
                    Dim serverName = Await helper.DefaultServerName()
    
                    If Not String.IsNullOrWhiteSpace(serverName) Then
    
                        If serverName = "SQLEXPRESS" Then
                            serverName = $".\{serverName}"
                        End If
    
                        My.Settings.ServerName = serverName
                        My.Settings.Save()
    
                        ConnectionString = $"Data Source={My.Settings.ServerName};Initial Catalog={My.Settings.Catalog};Integrated Security=True"
                    End If
                Else
                    '
                    ' SQL Server is not running - need to figure out why,
                    ' is it installed and set to fire up on windows startup?
                    '
                End If
            End If
    
            If String.IsNullOrWhiteSpace(ConnectionString) AndAlso Not String.IsNullOrWhiteSpace(My.Settings.ServerName) Then
                ConnectionString = $"Data Source={My.Settings.ServerName};Initial Catalog={My.Settings.Catalog};Integrated Security=True"
            Else
                '
                ' Something went wrong, you need to figure out the issue
                '
            End If
    
            Console.WriteLine(ConnectionString)
            Button1.Enabled = True
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            My.Settings.ServerName = ""
            My.Settings.Save()
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    i can handle the devicename so well but for the Clarification to help any one in need it`s the Machine Name aka "Computer Name"

    Thanks Again

    Thursday, February 13, 2020 4:26 PM
  • unfortunately using something instead of MS SQL Server is not an option as it will make me Redo a 6 month Work.

    No not really, and you see,  if you were implementing SoC, then it shouldn't matter that you swapped out the backend to another database, becuase the other layers such as the presentation and business logic layers wouldn't have direct contact with the database. It wouldn't matter that you stopped using ADO.NET with datasets, datatables and table adapters and switched to an ORM like EF.

    If you are concerned about switching databases or using other .NET technology, then you should be looking into architecting .NET solutions more  effectively and not do tight coupling as opposed to loose coupling. 

    https://en.wikipedia.org/wiki/Separation_of_concerns

    https://visualstudiomagazine.com/articles/2016/10/01/loosely-coupled-classes.aspx



    • Edited by DA924x Thursday, February 13, 2020 4:34 PM
    Thursday, February 13, 2020 4:27 PM
  • There are several ways to do this, here I'm using My.Settings to store the server name. So the first time through it will take a while but the app will remain responsive.

    You may need to modify the part checking for SQLEXPRESS needing .\

    Also You will need to get the device name which I'm not sure what that is too you.

    Utility/helper class

    Imports System.Data.Sql
    
    Public Class SqlServerHelpers
        Public Async Function SqlServerIsAvailable() As Task(Of Boolean)
    
            Dim success As Boolean = False
    
            Try
                Await Task.Run(
                    Sub()
                        Dim sqlDataSourceEnumeratorInstance As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance
                        Dim dt As DataTable = sqlDataSourceEnumeratorInstance.GetDataSources()
                        If dt IsNot Nothing Then
                            success = True
                        End If
                    End Sub)
            Catch ex As Exception
    
            End Try
    
            Return success
    
        End Function
        Public Async Function DefaultServerName() As Task(Of String)
    
            Dim success As String = ""
    
            Await Task.Run(
                Sub()
                    Dim sqlDataSourceEnumeratorInstance As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance
                    Dim dt As DataTable = sqlDataSourceEnumeratorInstance.GetDataSources()
                    If dt IsNot Nothing Then
                        Dim dataRowTest = dt.AsEnumerable().FirstOrDefault(
                            Function(row)
                                Return Not String.IsNullOrWhiteSpace(row.Field(Of String)("InstanceName"))
                            End Function)
                        If dataRowTest IsNot Nothing Then
                            success = dataRowTest.Field(Of String)("InstanceName")
                        End If
                    End If
                End Sub)
    
            Return success
    
        End Function
    End Class

    Sample code to try this out

    Public Class Form1
        ''' <summary>
        ''' Requires My.Setting.ServerName setting
        ''' This code is shown in a button click event but could be
        ''' where ever you want this code to run
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Button1.Enabled = False
            Dim ConnectionString As String
    
            If String.IsNullOrWhiteSpace(My.Settings.ServerName) Then
                Dim helper = New SqlServerHelpers
                Dim result = Await helper.SqlServerIsAvailable()
                If result Then
    
                    Dim serverName = Await helper.DefaultServerName()
    
                    If Not String.IsNullOrWhiteSpace(serverName) Then
    
                        If serverName = "SQLEXPRESS" Then
                            serverName = $".\{serverName}"
                        End If
    
                        My.Settings.ServerName = serverName
                        My.Settings.Save()
    
                        ConnectionString = $"Data Source={My.Settings.ServerName};Initial Catalog={My.Settings.Catalog};Integrated Security=True"
                    End If
                Else
                    '
                    ' SQL Server is not running - need to figure out why,
                    ' is it installed and set to fire up on windows startup?
                    '
                End If
            End If
    
            If String.IsNullOrWhiteSpace(ConnectionString) AndAlso Not String.IsNullOrWhiteSpace(My.Settings.ServerName) Then
                ConnectionString = $"Data Source={My.Settings.ServerName};Initial Catalog={My.Settings.Catalog};Integrated Security=True"
            Else
                '
                ' Something went wrong, you need to figure out the issue
                '
            End If
    
            Console.WriteLine(ConnectionString)
            Button1.Enabled = True
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            My.Settings.ServerName = ""
            My.Settings.Save()
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    i get error on Task.run

    i tried to use (Imports System.Threading.Tasks) but it gives me now error on Run as it's not member of Task

    Thursday, February 13, 2020 6:29 PM
  • Code was done in VS2017, Framework 4.7.2

    What are your specs?


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, February 13, 2020 6:57 PM
    Moderator
  • Code was done in VS2017, Framework 4.7.2

    What are your specs?


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    VS2019 framwrok 4,, needed to upgrade to 4.7.2 also 4.5 will do the trick

    • Edited by AbanoubZak Thursday, February 13, 2020 9:21 PM
    Thursday, February 13, 2020 9:15 PM
  • 4.5 should be fine, I've not gone down from 4.7.2 in over a year or so.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, February 13, 2020 9:25 PM
    Moderator


  • i can handle the devicename so well but for the Clarification to help any one in need it`s the Machine Name aka "Computer Name"

    Thanks Again

    Use

    Environment.MachineName


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, February 14, 2020 1:32 AM
    Moderator