question

Elado avatar image
0 Votes"
Elado asked Elado edited

Error: 40- could not open a connection to SQL Server - in Visual Studio 2019


Hello everyone,
I can't connect to SQL Server inside Visual Studio 2019.
The Microsoft SQL by itself is connecting, its only inside visual studio.

I get error 40. I followed the steps and restart SQL in "Services" but that didn't help.

118668-sql-conncetion-missing.png118676-sql-sevices.png


What can I do to solve this ?

Thanks in advanced.



Error: 40- could not open a connection to SQL Server - in visual studio 2019


[1]: https://ibb.co/FqF6dsP

[4]: https://ibb.co/R6RVgFM


sql-server-generaldotnet-csharpwindows-server
· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

See the following which provides diagnostic steps to follow


0 Votes 0 ·
Elado avatar image Elado karenpayneoregon ·

It's too old, nothing is like that in windows today.
its very different and I cant go through those steps, sorry.
Not solving my problem.

0 Votes 0 ·

Perhaps running the following PowerShell script might help



 $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
 $SqlConnection.ConnectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=True" 
 $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
 $SqlCmd.CommandText = 'SELECT @@SERVERNAME AS ServerName'
 $SqlCmd.Connection = $SqlConnection 
 $SqlConnection.Open()
 $rdr = $SqlCmd.ExecuteReader()
 while($rdr.Read())
 {
   $rdr["ServerName"].ToString()
 }
 $SqlConnection.Close()


118679-figure1.png


0 Votes 0 ·
figure1.png (69.0 KiB)
Show more comments

The error usually indicates an error entering the server\instance; (localdb)\MSSQLLocalDB. If you are trying to connect to the local db then a dot or (local) will work.

VS 2019 has a feature named "SQL Serer Object Explorer" (under the View menu) which enumerates the SQL instance found on the local machine.

118626-capture.png

Select the instance you are trying to connect and configure the authentication according to your configuration.


0 Votes 0 ·
capture.png (27.1 KiB)
Show more comments

1 Answer

TomPhillips-1744 avatar image
2 Votes"
TomPhillips-1744 answered Elado edited

That error is a generic "could not connect to server" message and does not help in any way diagnose the actual problem.

What exactly did you enter for the server name? Try "./sqlexpress", since your screen shot appears you have installed an instance named "sqlexpress".


I suggest you start here:
https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/troubleshoot-connecting-to-the-sql-server-database-engine?view=sql-server-ver15#testing-a-local-connection

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you, you helped me.

I needed to add my server name, I thought it need to be added automatically.
Forgot about that, silly me.

Elad

0 Votes 0 ·