Databases Reference
In-Depth Information
These directives are like any other directive in that they allow the use of types in a namespace. The next thing to
do is to define the retry policy. This policy simply defines the number of times to retry the command and the length of
time to wait between the number of retries, as well as several backoff times. Setting this is actually quite easy; a single
line of code:
RetryPolicy myretrypolicy = new RetryPolicy<SqlAzureTransientErrorDetectionStrategy>
(3, TimeSpan.FromSeconds(3));
The RetryPolicy constructor has five overloads, as shown in Figure 5-16 , but the most commonly used overload
takes two parameters; the count of retries and the wait time between retries.
Figure 5-16. Retry policy constructors
This example simply defines the number of tries as three and sets the retry interval at three seconds. With the
retry policy defined it can be applied to the connection through the ReliableSqlConnection class. This class provides
a way to reliably open a connection and execute commands through the policy just defined.
The ReliableSqlConnection class also has three overloads: the connection string, the defined retry policy to the
connection, and optionally the command.
Add to Button2 the following code, which uses the ReliableSqlConnection class and applies the retry policy to
both the connection and the command:
using (ReliableSqlConnection cnn = new ReliableSqlConnection(connString, myretrypolicy,
myretrypolicy))
{
try
{
cnn.Open();
using (var cmd = cnn.CreateCommand())
{
cmd.CommandText = "SELECT * FROM HumanResources.Employee";
using (var rdr = cnn.ExecuteCommand<IDataReader>(cmd))
{
//
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
 
Search WWH ::




Custom Search