问题描述
我需要在global.asax应用程序启动事件的Windows Azure上的Windows上创建一个sql数据库,但是出现此错误:
I need to create one sql database on windows azure on the global.asax application start event, however I got this error:
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. This failure occurred while attempting to connect to the routing destination. The duration spent while attempting to connect to the original server was - [Pre-Login] initialization=296; handshake=324; [Login] initialization=0; authentication=1; [Post-Login] complete=94;
我的代码如下:
private void SetupSSM() {
SqlConnectionStringBuilder connStrBldr = new SqlConnectionStringBuilder
{
UserID = SettingsHelper.AzureUsernamedb,
Password = SettingsHelper.AzurePasswordDb,
ApplicationName = SettingsHelper.AzureApplicationName,
DataSource = SettingsHelper.AzureSqlServer
};
bool created=DbUtils.CreateDatabaseIfNotExists(connStrBldr.ConnectionString, SettingsHelper.Azureshardmapmgrdb);
if(created)
{
Sharding sharding = new Sharding(SettingsHelper.AzureSqlServer, SettingsHelper.Azureshardmapmgrdb, connStrBldr.ConnectionString);
}
}
public static bool CreateDatabaseIfNotExists(string connectionString, string databaseName)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand(
string.Format("SELECT * FROM sys.databases WHERE [name]=\'{0:S}\'", databaseName),
conn);
if (cmd.ExecuteScalar() == null)
{
SqlCommand cmd2 = new SqlCommand(
string.Format("CREATE DATABASE [{0:S}];", databaseName),
conn);
cmd2.ExecuteNonQuery();
return true;
}
else
return false;
}
}
如何增加超时时间?"?是sql超时还是Web请求超时(由于sql没有响应?)?
How can I increase the timeout>? is it sql timeout or a web request timeout due to sql not responding?
推荐答案
您可以通过执行以下操作来设置命令超时,您看到的错误是命令超时,很可能您的创建数据库花费的时间超过30秒,这是默认超时值.
You can set the command timeout by doing the following, the error you are seeing is command timeout, most likely your create DB is taking longer than 30 seconds which is the default timeout value.
例如,将超时设置为300秒cmd.CommandTimeout = 300
For example, set timeout to 300 secondscmd.CommandTimeout = 300
这篇关于超时时间已到.在Azure sql上完成操作之前经过的超时时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!