none
SQL Server Not Found Error in VB.net code RRS feed

  • Question

  • I have code to parse emails and now I want to place certain data into a SQL database.  I can't connect to the database.  I keep getting Error 26 which seems to say it can't find my SQLExpress 2017 server which is on my local machine. So I wrote a simpler test code to test everything I could think of. 

    Imports System.Data
    Imports System.Data.SqlClient
    
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim MyConnection As SqlConnection
            'Tryed the ./SQLEXPESS default instead of the Server's Name (DESKTOP-11TM25R); and move the actual database file to the root - neither made any differance. "Data Source=./SQLEXPRESS; AttachDbFilename=D:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\eCom_Email.mdf; Integrated Security=True; Connect Timeout=30; User Instance=True"
            './SQLEXPRESS
            MyConnection = New SqlConnection("Data Source = DESKTOP-11TM25R; AttachDbFilename=D:\eCom_Email.mdf; Integrated Security=True; Connect Timeout=30; User Instance=True")
            'open the connection
            MyConnection.Open()
            lblConState.Text = MyConnection.State.ToString
            'close connection
            MyConnection.Close()
            lblConState.Text += MyConnection.State.ToString
        End Sub
    End Class
    The actual error message is:

    "Message=A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
      Source=.Net SqlClient Data Provider   StackTrace: <Cannot evaluate the exception stack trace>"

    Anyone have an idea of what I'm doing wrong?  Thanks

    Carl

    Sunday, July 14, 2019 12:39 AM

Answers

  • Thanks for all the help.  I tried it all.  What finally worked was changing the "Owner" of the database file.  Once I did this everything worked.  Thanks again.

    Carl

    Wednesday, July 17, 2019 7:06 PM

All replies

  • Either 'Attach' the detached MDF file to the DB engine and change the connectionstring to the database server, or use Localdb for the deteached MDF file pointing to the location of the detached MDF file.

    If  'Attaching' the MDF with its LDF file to the DB engine, the one should move the files to the Data directory where all the other attached MDF files are kept.

    One connectionstring where the machine had Localdb installed during the SQL Server Express instalation  And it is pointing to the location where all Localdb MDF files are kept, as opposed to your location where you used the <D> drive for the detached MDF file.

    <connectionStrings>
          <add name="DefaultConnection" connectionString="Data Source=LAPTOP-V2QKB8O1\SQLEXPRESS;AttachDbFilename=|DataDirectory|\aspnet-MVC-20170809085441.mdf;Initial Catalog=aspnet-MVC-20170809085441;Integrated Security=True" providerName="System.Data.SqlClient" />
      </connectionStrings>

    Another connectionstring where I moved the detached MDF file, moved them to the Data directory and "Atached" the files to the DB engine using SSMS

      <connectionStrings>
        <add name="CUDataEntities" connectionString="metadata=res://*/Model.CUDataModel.csdl|res://*/Model.CUDataModel.ssdl|res://*/Model.CUDataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=LAPTOP-V2QKB8O1\SQLEXPRESS;initial catalog=CUData;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
      </connectionStrings>

    The same solution where two differnt connectionstrings are being used in the same solution using MS SQL Server Express  on the same machine with one being used for a detached MDF file state using Localdb in the MVC part of the solution. The other part of the solution is using a WCF Web service that is using a connectionsting where the MDF file is attached to the DB engine.

    https://docs.microsoft.com/en-us/sql/relational-databases/databases/attach-a-database?view=sql-server-2017

    https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/sql-server-express-localdb?view=sql-server-2017

    If using SSMS and wanting to attach the  detached MDF file, right-click the Database instance, go to Properties go to the Database setting that will show the default location were the MDF and LDF files are kept for other dfatabases that are attached to the DB engine.

    Sunday, July 14, 2019 2:09 AM
  • Well, nothing seemed to work so I've uninstalled and reloaded SQL Express.  The error message has changed to:

    "System.Data.SqlClient.SqlException:'Unable to open the physical file "D:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\eCom_Email.mdf". Operating system error 5: "5(Access is denied.)".

    I have looked all over the net that seemed to have a ton of articles; but none that I could make sense of.  Does anyone have an answer?

    Wednesday, July 17, 2019 2:43 AM
  • Hi,

    see the following link,same issue

    https://stackoverflow.com/questions/18286765/sql-server-operating-system-error-5-5access-is-denied

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 17, 2019 6:24 AM
    Moderator
  • Well, nothing seemed to work so I've uninstalled and reloaded SQL Express.  The error message has changed to:

    "System.Data.SqlClient.SqlException:'Unable to open the physical file "D:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\eCom_Email.mdf". Operating system error 5: "5(Access is denied.)".

    I have looked all over the net that seemed to have a ton of articles; but none that I could make sense of.  Does anyone have an answer?

    Oh, you did an 'Attach' of the MDF file to the DB engine using SSMS? If that's the case, the user account  you used to logon to the O/S doesn't have permissions to access the mdf file, which you're going to have to tell  the Windows O/S what permissions are needed to access the MDF file for the user account. You can look at the permissions on other MDF files and see what they have on them and duplicate. Most likely,  the permissions would be read, update and delete. 
    Wednesday, July 17, 2019 6:38 AM
  • Thanks for all the help.  I tried it all.  What finally worked was changing the "Owner" of the database file.  Once I did this everything worked.  Thanks again.

    Carl

    Wednesday, July 17, 2019 7:06 PM