我正在开发一个基于.NET的Web应用程序,我想连接到使用SQL Server Management Studio 2014创建的SQL Server。我已经按照ITWorld和CodeProject的教程进行操作,没有成功。我收到错误“找不到网络路径” 。尽管当我在项目属性->设置->连接属性上对其进行测试时,结果是“测试连接成功”。通过SQL Management Studio直接执行查询也可以。
如何解决此问题?
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();
}
}
堆栈跟踪:
[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
答案 0 :(得分:1)
您的代码有错,应该可以。请检查您的网络设置和访问权限。在浪费数小时后,我遇到了同样的问题,我发现这是我的防病毒软件。简而言之,它可以是网络端口或防火墙。
答案 1 :(得分:0)
此错误消息的原因很多,如下所示
请检查连接字符串“ Data Source = x.x.x.x,1433”中是否具有端口
有关详细信息,请查看此视频。 Resolving SQL Server Connection Errors
答案 2 :(得分:0)
请尝试以下操作:-
string connectionString =
ConfigurationManager.ConnectionStrings["SQL_Server"].ConnectionString.ToString();
SqlConnection con = new SqlConnection(@connectionString);
让我知道这是否有帮助。
答案 3 :(得分:0)
所以我终于找到了问题。问题实际上是Web服务器和SQL Server之间的防火墙。我只是意识到它们位于完全不同的IP地址上。我使用UDL Tool进行了测试,并且Web服务器无法访问SQL Server。
我联系了网络管理员,问题已解决。
我应该早先用这种方法进行测试。谢谢大家在这里的时间和支持。很抱歉造成混乱。