超时已过期。在Azure sql上完成操作之前经过的超时时间

时间:2015-07-14 16:42:19

标签: sql-server asp.net-mvc azure azure-sql-database azure-elastic-scale

我需要在global.asax应用程序启动事件上的windows azure上创建一个sql数据库,但是我收到了这个错误:

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超时还是由于sql没有响应而导致Web请求超时?

2 个答案:

答案 0 :(得分:5)

您可以通过执行以下操作来设置命令超时,您看到的错误是命令超时,很可能您的创建数据库花费的时间超过30秒,这是默认的超时值。

例如,将超时设置为300秒 cmd.CommandTimeout = 300

https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.commandtimeout(v=vs.110).aspx

答案 1 :(得分:4)

当我们遇到这个问题时,事实证明我们的数据库功能不足。性能监视器显示DTU在我们收到错误期间被最大化。 Azure SQL Database Operation Timeout