【发布时间】:2012-04-23 21:54:49
【问题描述】:
我正在尝试覆盖默认的 SqlConnection 超时 15 秒,但收到一条错误消息,提示
无法分配属性或索引器,因为它是只读的。
有没有办法解决这个问题?
using (SqlConnection connection = new SqlConnection(Database.EstimatorConnection))
{
connection.Open();
using (SqlCommand command = connection.CreateCommand())
{
command.CommandType = CommandType.StoredProcedure;
connection.ConnectionTimeout = 180; // This is not working
command.CommandText = "sproc_StoreData";
command.Parameters.AddWithValue("@TaskPlanID", order.Projects[0].TaskPlanID);
command.Parameters.AddWithValue("@AsOfDate", order.IncurDate);
command.ExecuteNonQuery();
}
}
【问题讨论】:
-
您的意思是在您尝试打开连接后设置连接超时吗?你是说命令超时吗?
-
我真的,真的不明白他们为什么将属性设为只读。应该是读写的,IMO。
-
连接后属性为只读。请参阅其他回复以进行讨论。 ConnectionTimeout 会在尝试连接时影响超时。为 Azure SQL DB 增加此值通常很有用。运行查询时,CommandTimeout 会影响超时。
-
@Sam Pearson 在连接打开之前它是读写的,即当 connection.Open();叫做。从此时开始,计时器已经开始运行。
标签: c# .net sql-server sqlconnection