Windows服务自动停止

时间:2012-03-30 07:25:37

标签: c# stored-procedures windows-services

我想从Windows服务调用存储过程,之后我想自行停止服务。这就是我所拥有的:

public DailyChecker()
{
    InitializeComponent();
}

protected override void OnStart(string[] args)
{
    SqlConnection connection = new SqlConnection(connectionString);

    try
    {
        connection.Open();
        SqlCommand cmd = new SqlCommand("sp_ChangeState", connection);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.ExecuteNonQuery();
    }
    finally
    {
        connection.Close();
    }
}

protected override void OnStop()
{
    base.OnStop();
}

我遇到的问题是,当我启动服务时,它会在实例中自动停止并显示一个消息框。

2 个答案:

答案 0 :(得分:0)

我不确定我是否完全理解你的方法,但这应该只是一个调用base.OnStop的案例

try
{
    connection.Open();
    SqlCommand cmd = new SqlCommand("sp_ChangeState", connection);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.ExecuteNonQuery();
}
finally
{
    connection.Close();
}
base.OnStop();

答案 1 :(得分:0)

我已将serviceProcessInstaller的属性Account更改为LocalSystem。现在它正在运作