开发者

C# windows service sometimes hangs on Executing sql update

开发者 https://www.devze.com 2022-12-19 14:52 出处:网络
I am doing a simple update, of a bit in a table using the code below.However, sometimes the program hangs on myCommand.ExecuteNonQuery();and I don\'t know why.If Sql server is down, it will drop into

I am doing a simple update, of a bit in a table using the code below. However, sometimes the program hangs on myCommand.ExecuteNonQuery(); and I don't know why. If Sql server is down, it will drop into the catch statement as expected but sometimes, for seemingly no reason, it will just hang on the execution statement. Any Ideas.... Some of my code is below.

Thanks

SqlCommand myCommand = new SqlCommand(sqlCommand); connection = new SqlConnection(constr); myCommand.CommandType = CommandType.Text;

        myCommand.Connection = connection;

        try
        {
            connection.Open();
            myCommand.ExecuteNonQuery();
    开发者_StackOverflow中文版    }
        catch (Exception ex)
        {
            throw new Exception("There was an inserting information from the Sql server<br>" +
                "The server error was: <br>" + ex.Message + "<br>");  

        }
        finally
        {
            this.CloseConnection();
        }


A timeout is probably the cause. Though there's not enough info in your post to be certain. Try setting the command execution timeout.

0

精彩评论

暂无评论...
验证码 换一张
取 消