2

I'm working on a local project and trying to connect to a local database. I have this class in it the connection string:

public class SQLConnection
{
    public static string connectionString =
        @"Data Source=TZVIKA-PC\SQLEXPRESS;Initial Catalog=WorldCup;Integrated Security=True";

    public static WorldCupDBDataContext wcDataContext = 
        new WorldCupDBDataContext(connectionString);

    public static WorldCupDBDataContext GetDataContextInstance()
    {
        return wcDataContext;
    }
}

when I try to execute this line in a windows form:

 teamBindingSource.DataSource = database.Teams.OrderBy(team => team.TeamId);

I get the following sql exception:

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)

My friend ran the code and it works on his pc(when changing the connection string of course to a relevant one)

Why is it not working for me?

user2023203
  • 546
  • 1
  • 12
  • 19
user2674835
  • 139
  • 1
  • 3
  • 12

2 Answers2

9

A good (and easy) way to test if your SQL connection is working is to use a .UDL file. Simply create an empty text (.txt) file on your desktop and rename the extension to .udl. Double click and you'll be presented with a SQL connection window, fill in the details, test connection and then (when it's working) save. Close the window and rename the file extension to .txt. Open and inside you'll find your working connection string.

Damon
  • 3,004
  • 7
  • 24
  • 28
  • it's not exactly as what you said, in fact there is a dialog appeared, we can choose some type of connection, mostly are OLEDB, while there is not SQL Server there (at least that's what I got on my machine), you then have to select the server in the list or type the server name into the combobox... it's not helpful to do that way at all. – King King Nov 30 '13 at 13:22
  • Just did a quick search and found this (http://www.sophos.com/en-us/support/knowledgebase/65525.aspx) describing the same technique. Depending on the configuration of your sql server you may have to type in the server name. Remember to include the instance name in the server name i.e. from your example TZVIKA-PC\SQLEXPRESS, TZVIKA-PC is the server and SQLExpress is the instance. So just enter TZVIKA-PC\SQLEXPRESS in the server name. If it's valid you'll be able to select the DB from the list. Hope that helps. – Damon Nov 30 '13 at 15:18
  • Give this man a cookie. I've been trying to connect to a local DB for AGES (many instances of various sql servers) The above worked just fine! – Иво Недев Oct 08 '16 at 00:28
1

Enable remote connections for SQL Server Express 2012 Most common problems are: 1) Your sql server express is not running. Check it in "sql server configuration manager" 2) server runs but don't allow remote connection. Use sql management studio to configure it.

Community
  • 1
  • 1
Danila Polevshchikov
  • 2,228
  • 2
  • 24
  • 35
  • 1
    While the higher-rated answer has a decent suggestion, the linked answer in this one provided the real answer for me: set the port(s) in the TCP/IP properties under "SQL Server Network Configuration". – DonBoitnott Jul 23 '18 at 12:41