1

I'm currently trying to connect from Visual Studio 2013 C# application to a SQL Server 2012 instance but I'm running into troubles. Namely error 26 that no connection could be created.

My question there is what could still be wrong with what I'm doing (I'll post the code snippets I use below and then also what I controlled so far / steps I took):

Code

I created an application config which has the following line:

<connectionStrings>
   <add name="DefaultConnectionString" 
        connectionString="Data Source=TH-HP\SQLEXPRESS;Initial Catalog=BB;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False"  
        providerName="System.Data.SqlClient"/>
</connectionStrings>  

I use an intermediary class I call Database to create the connection strings and run the commands.

When initializing it I use the following command (which sets the private variable connectionStr):

Database db = new Database(Properties.Settings.Default.ConnectionString);

Then when I try to open the connection:

SqlConnection con = new SqlConnection(this.connectionStr);
con.Open();

The error happens.

Steps

(as note both the program and the SQL Server instance are running on the same machine)

  • Correct servername
  • Correct catalogue name
  • SQL Server accepts remote connections
  • Server Browser is running
  • Server itself is running (connected to it via management console)

Like I said I'm running out of ideas what to check next.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Thomas
  • 2,886
  • 3
  • 34
  • 78
  • I would be interested in seeing the constructor code of the class Database. – Steve Jun 01 '14 at 09:14
  • The constructor only has this.connectionStr = connectionStr (with connectionStr being a string private variable). I use that class mostly as a wrapper around commandds like executereader so that I dont need to open a connection manually each time. – Thomas Jun 01 '14 at 09:27

1 Answers1

1

this link may help: How to connect to local instance of SQL Server 2008 Express

also make a try with changing ConnectionString to:

<add name="DefaultConnectionString" connectionString="Data Source=TH-HP;Initial Catalog=BB;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False" providerName="System.Data.SqlClient"/>

or

<add name="DefaultConnectionString" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=BB;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False" providerName="System.Data.SqlClient"/>
Community
  • 1
  • 1
Vahid Hassani
  • 676
  • 1
  • 6
  • 20
  • Can you please also **explain** what you're changing, and why? – marc_s Jun 01 '14 at 09:11
  • I guess it may can't find your sqlexpress instance so I recommend to change the connectionstring in web.config. does it solve the problem? – Vahid Hassani Jun 01 '14 at 09:14
  • 1
    the above would have worked yepp. Found oout the problem was different than what I thought (VS modified my app.config on each startup which didnt show in the open editor window I had......thusu I modified the connection string time and again in the editor but it didnt take that one and instead the "wrong" connectionstring which lead to my problem. I will mark your answer as accepted though as it would have helped if the problem was what I thought it initially was (a connection string problem). tnx – Thomas Jun 01 '14 at 09:16