Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
.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

This comment has been minimized.

Copy link
Owner Author

@OdaShinsuke OdaShinsuke commented Oct 12, 2019

.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