Search code examples
c#sql-serversqlconnectionsqlexception

SQL Server with C#


I am writing a program that connects to a SQL Server (IGOR-PC - SQL Server 10.50.4044 - instance Igor-PC\Igor)

The code is this:

SqlConnection myConnection = newSqlConnection("Server=.\\Igor;Database=Prueba");

myConnection.Open();

But 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 that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

not monitored System.Data.SqlClient.SqlException

Thanks!


Solution

  • Try This

    SqlConnection myConnection = new SqlConnection("Data Source=Igor;Initial Catalog=Prueba;Integrated Security=True");
    

    Edit2:

    enter image description here

    For the Above Picture and for TEST_DB database the Connection String should be written as

     SqlConnection myConnection = new SqlConnection("Data Source=AJMOT-PC;User ID=sa;Password=thisismypassword;Initial Catalog=TEST_DB;Integrated Security=True");
    

    If you are using the Window authentication for sql server then you don't need to declare the User ID=sa;Password=thisismypassword section.