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

Lui*_*cia 4 sql-server asp.net-mvc azure azure-elastic-scale azure-sql-database

我需要在global.asax应用程序启动事件的Windows Azure上的Windows上创建一个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;
Run Code Online (Sandbox Code Playgroud)

我的代码如下:

   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;
            }
        }
Run Code Online (Sandbox Code Playgroud)

如何增加超时时间?是sql超时还是Web请求超时(由于sql没有响应)?

Sat*_*SFT 5

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

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

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

  • ..实际上我遇到了同样的问题,我的SqlStatement.CommandTimeout设置为900sec。并且有时仍然会引起问题。它似乎与DTU和Azure DB中的记录数(数据库大小)有关。BASIC 5DTU 2GB DB选项似乎是最差的。由于某些原因,即使某些SP和/或查询之前已被快速执行很多次,它们也只是挂起而没有明显的原因。我认为这可能是小型Azure DB的过程并发问题。 (2认同)