Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot connect to SQL Server in Visual Studio

I installed SQL Server and Visual Studio and tried to do my university project.

I can connect to SQL Server in Management Studio but can not connect in Visual Studio!

  • Remote connecting is enabled
  • all services are running
  • and everything is ok but I dont know why I get this error :

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 the ...

What's wrong with this code?

 SqlConnection sqcon = new SqlConnection("Data Source=.;Initial Catalog=Badan_Sazi;Integrated Security=True");

 SqlCommand com = new SqlCommand();
 com.Connection = sqcon;
 com.CommandText = "SELECT tbl_morabi.family   as 'lname'  FROM tbl_morabi";

 com.Connection.Open();

I also tried ., localhost, my pc name ... same error anyway

  • Visual Sudio 2012
  • SQL Server Express 2012
like image 947
hrh_fourtyseven Avatar asked Sep 16 '25 02:09

hrh_fourtyseven


1 Answers

SQL Server Express defaults to a SQLEXPRESS instance name

So try to use .\SQLEXPRESS or (local)\SQLEXPRESS as your server+instance name.

And if that doesn't work: what server name do you use in Management Studio to connect to your SQL Server instance?? Use that name!

like image 198
marc_s Avatar answered Sep 17 '25 18:09

marc_s