Skip to content

Instantly share code, notes, and snippets.

@OdaShinsuke
Last active October 12, 2019 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OdaShinsuke/c5fa67183f8d4d23f54fcc51dba6e79a to your computer and use it in GitHub Desktop.
Save OdaShinsuke/c5fa67183f8d4d23f54fcc51dba6e79a to your computer and use it in GitHub Desktop.
.NET Core SqlConnection ConnectRetryCount not work?
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
}
}
}
}
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
}
}
}
}
@OdaShinsuke
Copy link
Author

.NET Framework 4.7.2 + System.Data.SqlClient 4.7.0
.NET Core 3.0 + System.Data.SqlClient 4.7.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment