简体   繁体   中英

Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding

I did some research to resolve this error. But no luck. Please help to remove this error.

Im using SQL Server 2013

Here's my code:

//Declarations
SqlCommand CMD;
SqlDataReader Reader;

private void Save(string EmplID, double MRate, double HRate, double DRate)
{
     CMD = new SqlCommand("SELECT * FROM tblEmpl WHERE ACTIVE = 1", dbConn.connection);
     Reader = CMD.ExecuteReader();
     while (Reader.Read())
     {
          CMD = new SqlCommand("UPDATE tblEmpl SET MRate = " + MRate + ", HRate = " + HRate + ", DRate = " + DRate + " WHERE EmplID = '" + EmplID + "'", dbConn.connection);
          CMD.ExecuteNonQuery();
     }
}

I also tried storing the query in Stored Procedure and pass value to the parameters but also giving the same errors.

Query is just basic as you see. I don't know why I'm getting that error.

Please help. Thank you very much.

Modify the update query as below and execute it without the reader (single update statement).

update tblEmpl 
set your fields = your values
where active = 1 
and EmplID = parameter

Try to fire the query directly from SQL. Then check your connection string.

Try using WITH (NOLOCK) and different SqlCommand instance for update query

//Declarations
SqlCommand CMD;
SqlCommand CMDUPDATE;
SqlDataReader Reader;

private void Save(string EmplID, double MRate, double HRate, double DRate)
{
     CMD = new SqlCommand("SELECT * FROM tblEmpl WITH (NOLOCK) WHERE ACTIVE = 1", dbConn.connection);
     Reader = CMD.ExecuteReader();
     while (Reader.Read())
     {
          CMDUPDATE = new SqlCommand("UPDATE tblEmpl SET MRate = " + MRate + ", HRate = " + HRate + ", DRate = " + DRate + " WHERE EmplID = '" + EmplID + "'", dbConn.connection);
          CMDUPDATE.ExecuteNonQuery();
     }
}

WITH (NOLOCK)

Following are the things I can suggest for the code:

//Declarations
SqlCommand CMD;
SqlDataReader Reader;

private void Save(string EmplID, double MRate, double HRate, double DRate)
{
     CMD = new SqlCommand("SELECT a,b FROM tblEmpl WITH (NOLOCK) WHERE ACTIVE = 1", dbConn.connection);
     Reader = CMD.ExecuteReader();
     while (Reader.Read())
     {
       //// TODO : Populate a List of model with all the records

     }
    //// Foreach records update it

    Foreach(var item in <List of models>)
    {
          CMD = new SqlCommand("UPDATE tblEmpl SET MRate = " + item.MRate + ", HRate = " + item.HRate + ", DRate = " + item.DRate + " WHERE EmplID = '" + item.EmplID + "'", dbConn.connection);
          CMD.ExecuteNonQuery();
    }
}

Following is the approach I would suggest to follow to avoid locking of database table.

It is sample code resembling the employee table and class with having some columns in it. You need to change it as per your need.

public class Employee
{
    public int Id { get; set; }

    public string Name { get; set; }

    public double Salary { get; set; }

    public DateTime DateOfBirth { get; set; }

    public bool IsActie { get; set; }
}

using (var conn = new SqlConnection("Your Db Connection String"))
{
    SqlCommand cmd = new SqlCommand("SELEC * FROM Employee WHERE IsActive = 1", conn);
    cmd.CommandType = CommandType.Text;

    conn.Open();
    using (var reader = cmd.ExecuteReader())
    {
        while (reader.Read())
        {
            var emp = new Employee();

            emp.Id = reader.GetInt32(0);
            emp.Name = reader.GetString(1);
            emp.Salary = reader.GetDouble(2);
            emp.DateOfBirth = reader.GetDateTime(3);
            emp.IsActie = reader.GetBoolean(4);
            employees.Add(emp);
        }
    }

    SqlCommand updateCommand = new SqlCommand("UPDATE Employee SET Salary=@salary WHERE Id=@id", conn);
    updateCommand.Parameters.Add(new SqlParameter("@salary", SqlDbType.Float));
    updateCommand.Parameters.Add(new SqlParameter("@id", SqlDbType.Int));

    foreach (var employee in employees)
    {
        double salary = CalculateSalary();
        updateCommand.Parameters["@salary"].Value = salary;
        updateCommand.Parameters["@id"].Value = employee.Id;
        updateCommand.ExecuteNonQuery();
    }
}

This should help you resolve your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question SQL Server Exception:Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding ExecuteQueryin linq :Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding Linq Count() timing out -Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. The statement has been terminated Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding Execute package in ssis causes Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. working on remote windows .net Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding C# entityframework core throwing Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding 'Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM