Last active
October 12, 2019 06:11
-
-
Save OdaShinsuke/c5fa67183f8d4d23f54fcc51dba6e79a to your computer and use it in GitHub Desktop.
.NET Core SqlConnection ConnectRetryCount not work?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Data.SqlClient; | |
using System.Diagnostics; | |
namespace NETCore | |
{ | |
class Program | |
{ | |
static string connstr = @"Data Source=localhost;Initial Catalog=master;Integrated Security=True;Connect Timeout=5;Encrypt=False;"; | |
static void Main(string[] args) | |
{ | |
Test_ConnectRetry(); | |
Console.ReadKey(); | |
} | |
static void Test_ConnectRetry() | |
{ | |
Stopwatch s = null; | |
try | |
{ | |
using (var conn = new SqlConnection(connstr + "ConnectRetryCount=5;ConnectRetryInterval=5;")) | |
using (var cmd = new SqlCommand(@"select 1", conn)) | |
{ | |
conn.Open(); | |
Console.WriteLine(cmd.ExecuteScalar()); | |
Console.WriteLine("net stop <sql server service>"); | |
Console.ReadKey(); // manually SQL Server service stop | |
Console.WriteLine("SQL Server Service stoped!"); | |
s = Stopwatch.StartNew(); | |
cmd.ExecuteScalar(); | |
Console.WriteLine("not run"); | |
} | |
} | |
catch (Exception ex) | |
{ | |
s.Stop(); | |
Console.WriteLine(ex.Message); | |
Console.WriteLine(s.ElapsedMilliseconds); // about 7000 | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Data.SqlClient; | |
using System.Diagnostics; | |
namespace NETFramework472 | |
{ | |
class Program | |
{ | |
static string connstr = @"Data Source=localhost;Initial Catalog=master;Integrated Security=True;Connect Timeout=5;Encrypt=False;"; | |
static void Main(string[] args) | |
{ | |
Test_ConnectRetry(); | |
Console.ReadKey(); | |
} | |
static void Test_ConnectRetry() | |
{ | |
Stopwatch s = null; | |
try | |
{ | |
using (var conn = new SqlConnection(connstr + "ConnectRetryCount=5;ConnectRetryInterval=5;")) | |
using (var cmd = new SqlCommand(@"select 1", conn)) | |
{ | |
conn.Open(); | |
Console.WriteLine(cmd.ExecuteScalar()); | |
Console.WriteLine("net stop <sql server service>"); | |
Console.ReadKey(); // manually SQL Server service stop | |
Console.WriteLine("SQL Server Service stoped!"); | |
s = Stopwatch.StartNew(); | |
cmd.ExecuteScalar(); | |
Console.WriteLine("not run"); | |
} | |
} | |
catch (Exception ex) | |
{ | |
s.Stop(); | |
Console.WriteLine(ex.Message); | |
Console.WriteLine(s.ElapsedMilliseconds); // over 30000 | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
.NET Framework 4.7.2 + System.Data.SqlClient 4.7.0
.NET Core 3.0 + System.Data.SqlClient 4.7.0