Jack Huang
Jack Huang

Reputation: 531

SQL connection failure in LINQ-to-SQL

I'm cloning a local C#-SQL Windows Forms application to a new laptop. I've successfully cloned the C# project and back-up-and-restored the SQL database to the new machine, but the new project is unable to read/query any data from the new, cloned SQL database.

Since the SQL DB is local, the LINQ-to-SQL addressing is slightly different on account of the computer having a different name. So, I deleted the SQL table representations from the existing LINQ .dbml's graphical interface, removed the old server connection from the Server Explorer, added the new server connection, and click-and-dragged the table objects from the Server Explorer's new Data Connection object to the now-empty .dbml file.

Upon rebuilding and running the application in debug mode, I found that the dropdown menus which pull from the SQL DB are empty. Upon trying a button function which pulls from the database, I get the following error:

SqlException was unhandled 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)

(The cloned SQL server is configured to allow remote connections, TCP/IP is enabled in SQL, and the original C#-SQL application works without a hitch.)

Upvotes: 1

Views: 2754

Answers (2)

Rondel
Rondel

Reputation: 4951

Check in your Web.Config and make sure that your Connection String is set up correctly. Also check to make sure that when you initialize your DataContext that you are using the correct Connection string from your Web.Config.

Sometimes when you remove dbs from the dbml, it doesn't completely clear the web.config of the old entries. Look for a line like this in your designer.cs file:

public SomeDbDataContext() : 
            base(global::System.Configuration.ConfigurationManager.ConnectionStrings["SomeDbConnectionString"].ConnectionString, mappingSource)
    {
        OnCreated();
    }

Compare it with the connectionStrings in your web.config

<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
<add name="someDBConnectionString" connectionString="Data Source=SomeServer;Initial Catalog=SomeDB;Integrated Security=True" providerName="System.Data.SqlClient" /> 
<add name="theOLDDBConnectionString" connectionString="Data Source=SomeServer;Initial Catalog=OldDB;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

Upvotes: 2

competent_tech
competent_tech

Reputation: 44931

I would check a few things:

1) Is SQL Server started on the new machine?

2) Is the firewall configured to allow SQL connections?

3) Is the client connection mechanism (named pipes, tcp/ip) also configured in SQL Server.

4) Is there a valid network connection in the new machine (we used to have a lot of problems on laptops when they were not connected to the network).

You might have much better luck if you change the SQL Server machine name to . or (local) if you are always going to have the DB installed on the same machine as the app.

Upvotes: 1

Related Questions