Web API SQL 连接仅在调试时有效
本文关键字:调试 有效 API SQL 连接 Web | 更新日期: 2023-09-27 17:55:35
我正在尝试使 ASP.NET Web API连接到SQL Server,但它不起作用。我正在从Visual Studio的Web API模板构建它,当我在调试模式下运行代码时,它会打开一个浏览器到localhost:54723,它就像我想要的那样工作。
当我将相同的确切代码发布到真实网站时,就会出现问题。它不再打开与SQL数据库的连接,而是给了我这个冗长的错误(重要的部分似乎被标记为ExceptionMessage):
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
</ExceptionMessage>
<ExceptionType>System.Data.SqlClient.SqlException</ExceptionType>
<StackTrace>
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover) at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData) at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) at System.Data.SqlClient.SqlConnection.Open() at WebApplication2.Services.HelpdeskHealthHistoryRepository.getAllHelpdeskHealthHistoryInfo() at lambda_method(Closure , Object , Object[] ) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.AuthenticationFilterResult.<ExecuteAsync>d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()
</StackTrace>
<InnerException>
<Message>An error has occurred.</Message>
<ExceptionMessage>The network path was not found</ExceptionMessage>
<ExceptionType>System.ComponentModel.Win32Exception</ExceptionType>
<StackTrace/>
</InnerException>
</Error>
我搜索了这个网站和其他网站,发现了许多潜在的解决方案,但似乎都没有奏效。我以前从 Eclipse 中的 Java 程序连接到这个数据库,所以我知道我可以从外部连接到它。
我怀疑这个问题与数据库不允许访问外国计算机有关,如本文所述:Visual Studio 调试对外部数据库的访问,但如果我希望任何计算机能够访问它,我该如何解决这个问题?
您需要将 SQL Server 配置为接受远程连接。
SSMS
- 打开 SQL 管理工作室,右键单击左侧的服务器名称 窗格,然后选择属性。
- 在左窗格中选择"连接",并确保选中"允许远程连接到此服务器"复选框
SQL Server Configuration Manager
- 打开SQL Server
- Configuration Manager,然后单击左窗格中的"SQL Server Services"。
- 在中心窗格中,有一列列出每个正在运行的服务的进程 ID。在 SQL Server 的行中查找 PID。通过在命令提示符下键入以下命令来标识 PID 正在侦听的端口:
netstat -ano | find /i “PID-Number-Of-SQL-Server”
- 如果在步骤 3 中执行的命令没有结果,则是因为 TCP/IP 协议已禁用,必须启用。在 SQL Server Configuration Manager 中,单击左窗格中的"SQL Server 网络配置",右键单击"TCP/IP 协议"并选择"启用"选项。
- 重新启动 SQL Server 服务,并确定分配给 SQL 服务的进程 ID。
- 在命令提示符下执行命令:
netstat -ano | find /i “6524”
在 SQL Server - Configuration Manager 中,单击左窗格中的 SQL Server 网络配置,右键单击 TCP/IP 协议并选择选项"属性"。转到 IP 地址选项卡并向下滚动到 APAll 部分。删除 TCP 动态端口的值,然后输入 TCP 端口的端口 1433。 重新启动 SQL Server 服务,
- 标识分配给 SQL 服务的新进程 ID,然后在命令提示符下执行命令:
netstat -ano | find /i “3948”
。
打开 SQL Server - 配置管理器并单击左窗格中的"SQL Server Services",右键单击 SQL Server 浏览器服务并选择"属性"。
- 转到"服务"选项卡,对于"启动模式"选项,将启动类型更改为"自动"。单击"开始"按钮以启动 SQL 浏览器服务。确认 SQL Server 浏览器服务已启动并正在运行。
视窗防火墙
- TCP 端口 1433 的端口异常。在"新建入站规则向导"对话框中,使用以下信息创建端口例外:
- 选择端口
- 选择 TCP 并指定端口 1433
- 允许连接
- 选择所有三个配置文件(域,私有和公共)
- 将规则命名为"SQL – TCP 1433"
- UDP 端口 1434 的端口异常。再次单击"新建规则",并使用以下信息创建另一个端口例外:
- 选择端口
- 选择 UDP 并指定端口 1434
- 允许连接
- 选择所有三个配置文件(域,私有和公共)
- 将规则命名为"SQL – UDP 1434
- sqlservr.exe 的程序例外。再次单击"新建规则",并使用以下信息创建程序例外:
- 选择程序
- 单击"浏览"以选择以下位置的"sqlservr.exe":[C:''Program Files''Microsoft SQL Server''MSSQL11.''MSSQL''Binn''sqlservr.exe] 其中 SQL 实例的名称。
- 允许连接
- 选择所有三个配置文件(域,私有和公共)
- 将规则命名为 SQL – sqlservr.exe
- sqlbrowser 的程序例外.exe 再次单击"新建规则",并使用以下信息创建另一个程序例外:
- 选择程序
- 单击"浏览"以选择以下位置的 sqlbrowser.exe:[C:''Program Files (x86)''Microsoft SQL Server''90''Shared''sqlbrowser.exe]。
- 允许连接
- 选择所有三个配置文件(域,私有和公共)
- 将规则命名为 SQL – sqlbrowser.exe
来源
http://blog.citrix24.com/configure-sql-express-to-accept-remote-connections/