Share via

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.

Developer technologies | VB
SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

0 comments No comments

Answer accepted by question author

  1. Yitzhak Khabinsky 27,116 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
    

    Was this answer helpful?

    0 comments No comments

18 additional answers

Sort by: Most helpful
  1. Seeya Xi-MSFT 16,676 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.

    Was this answer helpful?


  2. 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.

    Was this answer helpful?


  3. Michael Taylor 61,221 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.

    Was this answer helpful?

    0 comments No comments

  4. Dan Guzman 9,516 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"
    

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.