简体   繁体   English

在启动时应用程序引发SQL异常

[英]Application throws SQL exception when running as startup

I am working on a C# winforms Desktop application. 我正在使用C#winforms桌面应用程序。 The application has two processes. 该应用程序有两个过程。 Users need to invoke only first process and first process will invoke the second process. 用户只需要调用第一个进程,第一个进程将调用第二个进程。 The first process makes all web service calls to get the data and stores on local database. 第一个过程进行所有Web服务调用以获取数据并将其存储在本地数据库中。 The second application is UI based and show data that is there in the local database. 第二个应用程序基于UI,并显示本地数据库中存在的数据。 I am using SQL Server 2008 R2 Express for local database. 我将SQL Server 2008 R2 Express用于本地数据库。

This model works fine when the user clicks on the desktop icon or invokes from start menu. 当用户单击桌面图标或从开始菜单调用时,此模型可以正常工作。 But my requirement is to start the application at windows startup. 但是我的要求是在Windows启动时启动应用程序。 So I put the shortcut to first application in windows startup. 因此,我将快捷方式放在Windows启动中的第一个应用程序中。 In this case I see SQL exception for the database calls made on the second process saying Login failed for user sa . 在这种情况下,我看到在第二个进程上进行的数据库调用发生SQL异常,称用户sa登录失败。

The code for starting the second process is as below called from Form Load of 1st process 从第一个流程的Form Load调用下面的代码来启动第二个流程

string MAIN_APP = "second.exe";
System.Diagnostics.Process.Start(MAIN_APP);
GetDataFromServer();

I do not have any clue why it works when invoked from desktop and fails in startup. 我不知道为什么从桌面调用它并在启动失败时会起作用。 I tried setting user context as administrator in application manifest but that is not solving the issue. 我尝试将用户上下文设置为应用程序清单中的管理员,但这不能解决问题。

Any pointers on what could be wrong? 关于可能出什么问题的任何指示?

Code on the second application are as below: 第二个应用程序上的代码如下:

try
{
    string sqlStr = "select remember_pass, imgfile from user_profile where  userid = '" + txtUserId.Text + "'";
    DataSet ds = new DataSet();

    SqlConnection con = new SqlConnection(mainConnectionString)
    SqlDataAdapter da = new SqlDataAdapter(str, con);
    da.Fill(ds);
}
catch(Exception ex)
{
   logger.log("Caught Exception. Reason {0}", ex.Message);
}

Note: I rectified the mistakes in the problem statement. 注意:我纠正了问题陈述中的错误。

I think that your service start before SQL Server is ready to handle connection. 我认为您的服务在SQL Server准备处理连接之前启动。

Set your service as "Automatic (Delayed)" so that it'll start after the other services 将您的服务设置为“自动(延迟)”,以便在其他服务之后启动

More info on delayed start : http://blogs.technet.com/b/askperf/archive/2008/02/02/ws2008-startup-processes-and-delayed-automatic-start.aspx 有关延迟启动的更多信息: http : //blogs.technet.com/b/askperf/archive/2008/02/02/ws2008-startup-processes-and-delayed-automatic-start.aspx

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

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