![](/img/trans.png)
[英]SQL Server Exception: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
我做了一些研究來解決此錯誤。 但是沒有運氣。 請幫助消除此錯誤。
我正在使用SQL Server 2013
這是我的代碼:
//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();
}
}
我還嘗試將查詢存儲在存儲過程中,並將值傳遞給參數,但也給出相同的錯誤。
如您所見,查詢只是基本的。 我不知道為什么會收到該錯誤。
請幫忙。 非常感謝你。
如下所示修改更新查詢,並在不使用讀取器的情況下執行該查詢(單個更新語句)。
update tblEmpl
set your fields = your values
where active = 1
and EmplID = parameter
嘗試直接從SQL觸發查詢。 然后檢查您的連接字符串。
嘗試使用WITH(NOLOCK)和其他SqlCommand實例進行更新查詢
//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();
}
}
以下是我對代碼的建議:
//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();
}
}
以下是我建議遵循的避免數據庫表鎖定的方法。
它是類似於employee表和類的示例代碼,其中有一些列。 您需要根據需要進行更改。
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();
}
}
這應該可以幫助您解決問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.