【发布时间】:2016-06-20 12:23:52
【问题描述】:
我遇到一个用 C# .NET 编写的应用程序的 SQL 连接超时非常奇怪的情况。
SqlCommand.ExecuteNonQuery() 用于在 SQL Server 中依次执行多个脚本,一个接一个。每个脚本都包含一个只创建一个表的命令(根本没有数据更新/插入/删除操作)。出于某种原因,在其中一个脚本中,SqlCommand.ExecuteNonQuery 引发了超时异常。
当我在 SQL Server Management Studio 中执行所有这些表的创建时,它们几乎可以立即执行。
有没有人知道从应用程序创建表时可能导致超时的原因是什么?
所有 sql 脚本如下所示:
SQL:
create table dbo.Test
(
Code varchar(10) not null
, Name varchar(50) not null
, other columns...
primary key
, unique key
, foreign key
)
脚本是使用以下代码从 C# 发布的:
try
{
using (SqlConnection conSQL = new SqlConnection ("[connection string]"))
{
using (SqlCommand cmdSQL = new SqlCommand(sSQL, conSQL))
{
cmdSQL.CommandTimeout = iTimeOut;
conSQL.Open();
cmdSQL.ExecuteNonQuery(); // this is where it jumps to catch part and
// throws out timeout exception
conSQL.Close();
}
}
}
catch(Exception ex)
{
throw (ex);
}
这发生在测试服务器上,这意味着当应用程序执行这些脚本时,服务器上没有发生任何其他事情。
【问题讨论】:
-
我们需要查看您的相关c#代码和sql查询。
-
试试
cmdSQL.CommandTimeout = 0; -
你同意sql脚本很简单吧?我只是不明白为什么这个脚本会超时,而其他脚本不会。需要注意的是,Test sql server 上没有运行其他任何东西,iTimeOut 值为 30 秒。
标签: c# sql asp.net sql-server timeout