SQL String Connection

Jorge Enciso 116 Reputation points
2021-08-25T14:47:15.523+00:00

My Connection String:
Dim conString1 As String = "Data Source=igfnav03; Initial Catalog=[Test Navision]; integrated Security=true"

Error in Program:
System.Data.SqlClient.SqlException: 'Cannot open database "[Test Navision]" requested by the login. The login failed.
Login failed for user 'GOURMETFOODS\jenciso'.'

Error in SQL:

Error: 18456, Severity: 14, State: 38.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,899 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,606 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yitzhak Khabinsky 25,206 Reputation points
    2021-08-30T18:37:33.627+00:00

    Hi JorgeEnciso-6991,

    Please try the following.

    VB.NET

    Sub Main
        Const FILENAME As String = "e:\temp\file_ADO.NET.VB.xml"
    
        Dim builder As SqlConnectionStringBuilder = New SqlConnectionStringBuilder()
        builder.DataSource = "SPACESHIP,1433"
        builder.InitialCatalog = "AdventureWorks2012"
        builder.IntegratedSecurity = True
        builder.ApplicationName = "Wonderful application"
        builder.NetworkLibrary = "dbmssocn"
        builder.PacketSize = 32768
    
        Using con As SqlConnection = New SqlConnection(builder.ConnectionString)
            Using cmd As SqlCommand = New SqlCommand()
                cmd.Connection = con
                cmd.CommandType = CommandType.Text
                cmd.CommandText = "SELECT TOP(2) * FROM Person.Person FOR XML PATH('r'), TYPE, ROOT('root')"
                con.Open()
    
                Using reader As XmlReader = cmd.ExecuteXmlReader()
                    Dim xdoc As XDocument = XDocument.Load(reader)
                    Dim settings = New XmlWriterSettings()
                    settings.Indent = True
                    settings.OmitXmlDeclaration = False
                    settings.IndentChars = Microsoft.VisualBasic.Constants.vbTab
                    settings.Encoding = New UTF8Encoding(False)
    
                    Using writer = XmlWriter.Create(FILENAME, settings)
                        xdoc.Save(writer)
                    End Using
                End Using
    
                Console.WriteLine("File '{0}' has been created.", FILENAME)
            End Using
        End Using
    End Sub
    
    0 comments No comments

18 additional answers

Sort by: Most helpful
  1. Dan Guzman 9,211 Reputation points
    2021-08-25T14:56:46.12+00:00

    Connection string values may optionally be enclosed in single or double quotes but not square brackets. There is no need to enclose the database name value in this case:

    Dim conString1 As String = "Data Source=igfnav03; Initial Catalog=Test Navision; integrated Security=true"
    
    0 comments No comments

  2. Michael Taylor 49,251 Reputation points
    2021-08-25T14:57:35.067+00:00

    Is igfnav03 a SQL Server instance on your local machine or the network?

    Is Windows auth enabled on the SQL Server?

    In SSMS does it show that your user account has permissions to access the Test Navision database?

    Your tag includes Azure SQL. To connect to SQL Azure using Win auth you have to properly enable AD integration (which requires infrastructure support) and you must use AAD integration.

    0 comments No comments

  3. Jorge Enciso 116 Reputation points
    2021-08-25T16:34:10.337+00:00

    DanGuzman:
    Without brakets have this error:
    System.Data.SqlClient.SqlException: 'Incorrect syntax near '`'.
    Unclosed quotation mark after the character string ' '.'

    cooldadtx:
    Network server
    Windows Auth. enables in SQL Server
    Have Permissions to access Test Navision

    Now here is the thing:
    If I made a new dbconnection in VS2019 and use Windows Ath it works no problem but if i do SQL Auth have same error.

    Thank you for the responds........
    ps
    TAG was a mistake no AZURE at all.


  4. Seeya Xi-MSFT 16,451 Reputation points
    2021-08-26T03:18:47.397+00:00

    Hi @Jorge Enciso ,

    Did you set this:
    126547-1.png

    Best regards,
    Seeya


    If the response is helpful, please click "Accept Answer" and upvote it, as this could help other community members looking for similar queries.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.