0

I am getting below exception while connecting ASP.NET with SQLSERVER using below connection strings.i tried two methods.

Exception

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

Additional information: 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

Connection String

    protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection con;
            con = new SqlConnection("Server=ADMIN/SQLEXPRESS,Database=test,integrated security=true");
            con.Open();

        }

              (or)

    protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection con;
            con = new SqlConnection("Server=localhost;Database=test;integrated security=sspi");
            con.Open();

        }

i am connecting SQLSERVER directly by using below server name in windows authentication.It is connecting well but it is not connected from asp.net

Server Name:ADMIN\SQLEXPRESS

dvo
  • 2,113
  • 1
  • 8
  • 19
Ram
  • 727
  • 2
  • 16
  • 33
  • 1
    Possible duplicate of [Why am I getting "Cannot Connect to Server - A network-related or instance-specific error"?](https://stackoverflow.com/questions/18060667/why-am-i-getting-cannot-connect-to-server-a-network-related-or-instance-speci) – Crowcoder May 30 '19 at 14:49
  • 2
    Remember that IIS (and IISExpress too) runs using a different user than yourself – Steve May 30 '19 at 14:52
  • Your top connection string is mangled: uses / not \, uses commas not semicolons. Your bottom connection string is mangled - doesn't cite an instance name, required for SQL Express – Caius Jard May 30 '19 at 15:04
  • try this `connectionString="Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;"` – Aristos May 30 '19 at 15:41

1 Answers1

1

A connection string for SQL server should be like this: "Server=localhost; Database=Testdb; Integrated Security=True;" and If you have Named Instance e.g localhost\SQLEXPRESS

Integrated Security=?

When false, User ID and Password are specified in the connection. When true, the current Windows account credentials are used for authentication. Recognized values are true, false, yes, no, and SSPI (strongly recommended), which is equivalent to true.check connectionstrings

SUNIL DHAPPADHULE
  • 2,755
  • 17
  • 32