简体   繁体   English

尝试从远程计算机上的程序连接到SQL Server时出错

[英]Error when trying to connect to SQL Server from program on remote computer

I am trying to set up a system for employees to use in the office so they can run certain programs from a website. 我正在尝试建立一个供员工在办公室使用的系统,以便他们可以从网站上运行某些程序。

They will be able to login and go to a reports page an click a button to run a reports program on the utility server. 他们将能够登录并进入报告页面,单击按钮以在实用程序服务器上运行报告程序。 The program will connect to SQL Server to get the login information and then login to another site and programmatically create the reports and download them. 该程序将连接到SQL Server以获取登录信息,然后登录到另一个站点并以编程方式创建报告并下载。

If I run the program as a stand alone program on the server it runs fine. 如果我在服务器上将程序作为独立程序运行,它将运行良好。 I even have it running as a scheduled task on the server itself and it runs fine. 我什至让它作为服务器上的计划任务运行,并且运行良好。 It is only when I try to run the program from the website that I get the error stating the program but it is terminated due to an unhandled exception. 只有当我尝试从网站上运行该程序时,我才会收到说明该程序的错误,但由于未处理的异常而终止。 Here is the stack trace from the error in the event viewer. 这是事件查看器中错误的堆栈跟踪。

Application: Trending Reports.exe 应用程序:趋势报告.exe
Framework Version: v4.0.30319 框架版本:v4.0.30319
Description: The process was terminated due to an unhandled exception. 说明:由于未处理的异常,进程已终止。
Exception Info: System.Data.SqlClient.SqlException 异常信息:System.Data.SqlClient.SqlException

Stack: at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(System.Data.Common.DbConnection, System.Data.ProviderBase.DbConnectionFactory, System.Threading.Tasks.TaskCompletionSource 1<System.Data.ProviderBase.DbConnectionInternal>, System.Data.Common.DbConnectionOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(System.Data.Common.DbConnection, System.Data.ProviderBase.DbConnectionFactory, System.Threading.Tasks.TaskCompletionSource 1, System.Data.Common.DbConnectionOptions) at System.Data.SqlClient.SqlConnection.TryOpenInner(System.Threading.Tasks.TaskCompletionSource 1<System.Data.ProviderBase.DbConnectionInternal>) at System.Data.SqlClient.SqlConnection.TryOpen(System.Threading.Tasks.TaskCompletionSource 1) at System.Data.SqlClient.SqlConnection.Open() at Trending_Reports.Program.Main(System.String[]) 堆栈:位于System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(System.Data.Common.DbConnection,System.Data.ProviderBase.DbConnectionFactory,System.Threading.Tasks.TaskCompletionSource 1<System.Data.ProviderBase.DbConnectionInternal>, System.Data.Common.DbConnectionOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(System.Data.Common.DbConnection, System.Data.ProviderBase.DbConnectionFactory, System.Threading.Tasks.TaskCompletionSource 1,System.Data.Common.DbConnectionOptions)的1<System.Data.ProviderBase.DbConnectionInternal>, System.Data.Common.DbConnectionOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(System.Data.Common.DbConnection, System.Data.ProviderBase.DbConnectionFactory, System.Threading.Tasks.TaskCompletionSource System的1<System.Data.ProviderBase.DbConnectionInternal>) at System.Data.SqlClient.SqlConnection.TryOpen(System.Threading.Tasks.TaskCompletionSource 1)的System.Data.SqlClient.SqlConnection.TryOpenInner(System.Threading.Tasks.TaskCompletionSource 1<System.Data.ProviderBase.DbConnectionInternal>) at System.Data.SqlClient.SqlConnection.TryOpen(System.Threading.Tasks.TaskCompletionSource .Data.SqlClient.SqlConnection.Open()位于Trending_Reports.Program.Main(System.String [])

Here is the code I am using to start the program. 这是我用来启动程序的代码。

var processToRun = new[] { @"D:\test\Trending Reports.exe" };

var connection = new ConnectionOptions();
connection.Impersonation = System.Management.ImpersonationLevel.Impersonate;
connection.Username = "Username";
connection.Password = "password";

var wmiScope = new ManagementScope(String.Format("\\\\{0}\\root\\cimv2", "Server"), connection);
wmiScope.Connect();

var wmiProcess = new ManagementClass(wmiScope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
wmiProcess.InvokeMethod("Create", processToRun);

The program itself is using multi-threading. 该程序本身正在使用多线程。 Could this be causing the issue? 这可能是造成问题的原因吗? Or could it be a user issue to where its not using my user profile on the remote computer? 还是在不使用远程计算机上的用户配置文件的情况下出现了用户问题?

Have you configured the SQL server to accept remote connections? 您是否已将SQL Server配置为接受远程连接? This is a common cause for errors. 这是导致错误的常见原因。

You can follow the following steps: 您可以按照以下步骤操作:

  • Enable remote connections on the instance of SQL Server that you want to connect to from a remote computer. 在要从远程计算机连接到的SQL Server实例上启用远程连接。
  • Configure SQL server to listen on static port 配置SQL Server在静态端口上侦听
  • Turn on the SQL Server Browser service. 打开SQL Server浏览器服务。
  • Configure the firewall to allow network traffic that is related to SQL Server and to the SQL Server Browser service. 配置防火墙以允许与SQL Server和SQL Server Browser服务相关的网络流量。

This link here has a step-by-step tutorial with pictures (Microsoft SQL Server 2012 Express). 链接在此处包含有关图片的分步教程(Microsoft SQL Server 2012 Express)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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