简体   繁体   中英

Cannot open a SQL connection to a SQL Server: The network path was not found

I am developing a .NET based web application and I want to connect to a SQL Server, which I created using SQL Server Management Studio 2014. I already followed tutorials from ITWorld and CodeProject with no success. I got an error "Network Path was not found" . Although when I test it on Project Properties - > Settings -> Connection Properties, The result is "Test Connection succeeded". Executing Query directly via SQL Management Studio works too.

How do I fix this problem?

Web.config

  <connectionStrings>
    <add name="SQL_Server"
      connectionString="Data Source=192.168.60.130;Initial Catalog=TestServer;Persist Security Info=True;User ID=name;Password=password"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

Default.aspx.cs

 protected void Page_Load(object sender, EventArgs e)
 {
            string sqlConn = "SQL_Server";
            ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings[sqlConn];

            using (SqlConnection conn = new SqlConnection(settings.ConnectionString))
            {
                conn.Open(); //this is line 31 as shown from the Stack Trace error below
                SqlCommand command = new SqlCommand("SELECT * from [SQL_DB].[dbo].[users]", conn);

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {

                        Console.WriteLine(reader);
                    }
                }
                Console.ReadLine();
                Console.Clear();
            }
}

Stack Trace:

[Win32Exception (0x80004005): The network path was not found]

[SqlException (0x80131904): Network-related or instance-specific error when connecting to SQL Server. The server was not found or can not be accessed. Verify that the instance name is correct and that SQL Server allows remote connections. (provider: Named Pipes Provider, error: 40 - Could not open connection with SQL Server)]
   System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling, SqlAuthenticationProviderManager sqlAuthProviderManager) +1431
   System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +1085
   System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) +70
   System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) +964
   System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) +109
   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) +1529
   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +156
   System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) +258
   System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +312
   System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) +202
   System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +413
   System.Data.SqlClient.SqlConnection.Open() +128
   VF.BC._Default.Page_Load(Object sender, EventArgs e) in C:\Users\VM_User\source\repos\SQLProject\Default.aspx.cs:31
   System.Web.UI.Control.OnLoad(EventArgs e) +106
   System.Web.UI.Control.LoadRecursive() +68
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3785

There is noting wrong with your code, It should work. Please check your network setting and access rights. I had the same problem after wasting hours on that I found that it was my antivirus. In short it can be network port or firewall.

There are many causes of this error message which are given below

  • Antivirus,
  • Firewall
  • SQL Server Network configuration

Please check if you have the port in the connection string "Data Source=xxxx,1433"

For detail, please check this video. Resolving SQL Server Connection Errors

Please try this:-

string connectionString = 
ConfigurationManager.ConnectionStrings["SQL_Server"].ConnectionString.ToString();

SqlConnection con = new SqlConnection(@connectionString);

Let me know if this helps.

So I finally found the issue. The issue was actually the firewall between the web server and the SQL server. I just realized that they are on a completely different IP address. I tested using a UDL Tool and the web server could not access the SQL server.

I contacted the Network admin and the issue is now resolved.

I should have tested this way earlier. Thank you everyone here for your time and support. Sorry for causing confusion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM