# Wednesday, May 30, 2012

 

In .NET applications, Connection Strings are easily stored in and retrieved from a web.config file or an app.config file. Add a <connectionStrings> section to the config file, directly within the <configuration> section. Within the <connectionStrings> section, place an <add> tag for each connection string you wish to store and retrieve. This <add> tag contains two important attributes: name and connectionString. The name attribute will be used to look up an entry, so each <add> tag should have a unique name. The connectionString attribute contains the connection string you will use to connect to a database. If your application needs to connect to a number of different data types, it is worth setting the Provider property as well. This is the name of a database provider installed on the current machine, that will allow you to connect to a database.
An example is shown below:

<configuration>
  <connectionStrings>
    <add name="MyApp_Dev" 
        connectionString="Data Source=Server01;Initial Catalog=AwesomeDB_Dev;Integrated Security=True"/> <add name="MyApp_QA"
        connectionString="Data Source=Server01;Initial Catalog=AwesomeDB_QA;Integrated Security=True"/> <add name="MyApp_Prod"
        connectionString="Data Source=Server01;Initial Catalog=AwesomeDB;Integrated Security=True"/> </connectionStrings> </configuration>

To retrieve a connection string by its associated name attribute, use the utilities found in the System.Configuration namespace of the System.Configuration assembly. First, set a reference to System.Assembly by selecting the menu option Project | Add Reference, selecting the System.Assembly in the Component Name column of the .NET tab of the Add Reference dialog; and, clicking the OK button. Then, add the following using statement to the top of your class file.

This namespace contains the static ConfigurationManager class, which exposes a number of methods for retrieving configuration information. ConfigurationManager.ConnectionStrings returns a collection containing all connection strings settings in the config file of the current assembly. Once we have this collection, we can retrieve a single item by specifying its name. Doing so returns a ConnectionStringSettings object that contains properties retrieved from a single <add> element of the config file. The important property is ConnectionString, which can be used with ADO.NET to connect to a database. Knowing the Provider property may also be useful if your application connects to different types of data.
Sample code is below.

ConnectionStringSettingsCollection cssCollection =
    ConfigurationManager.ConnectionStrings;
ConnectionStringSettings devCsSettings = cssCollection["MyApp_Dev"];
string devCs = devCsSettings.ConnectionString;
Wednesday, May 30, 2012 5:21:00 PM (GMT Daylight Time, UTC+01:00)
# Monday, May 28, 2012
Monday, May 28, 2012 10:34:00 PM (GMT Daylight Time, UTC+01:00)
# Friday, May 25, 2012

An App.config file or a Web.config file are great places to store configurable information – information that generally doesn’t change; but we want to be able to change easily (i.e., without rebuilding and redeploying the application.) Examples include connection strings (stored in the config file’s <connectionStrings> section) and application-wide name-value pairs (stored in the config file’s <appSettings> section).

We can add more flexibility by moving a section to an external file and linking to that file from the config file.

By splitting the file, we can manage and deploy only those settings separate from the rest of the configuration.

To do so, we create a new text file and copy that section into that file; then use the configSource attribute of the section tag in the original config file to point to the new file.

For example, the following app.config contains all the application’s connection strings and application settings

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="MyApp_Dev" connectionString="Data Source=SQL071;Initial Catalog=Awesome_Dev;Integrated Security=True"/>
    <add name="MyApp_QA" connectionString="Data Source=SQL071;Initial Catalog=Awesome_Dev;Integrated Security=True"/>
    <add name="MyApp_Prod" connectionString="Data Source=SQL071;Initial Catalog=Awesome_Dev;Integrated Security=True"/>
  </connectionStrings>
  <appSettings>
    <add key="CompanyName" value="The Awesome Company"/>
    <add key="PhoneNumber" value="(513) 555-4444"/>
  </appSettings>
</configuration>

We can accomplish the same functionality as the above app.config by creating 2 files: connections.config and appSettings.config and adding the following code to each file, respectively

connections.config:

<connectionStrings>
  <add name="MyApp_Dev" connectionString="Data Source=SQL071;Initial Catalog=Awesome_Dev;Integrated Security=True"/>
  <add name="MyApp_QA" connectionString="Data Source=SQL071;Initial Catalog=Awesome_Dev;Integrated Security=True"/>
  <add name="MyApp_Prod" connectionString="Data Source=SQL071;Initial Catalog=Awesome_Dev;Integrated Security=True"/>
</connectionStrings>

appSettings.config:

<appSettings>
  <add key="CompanyName" value="The Awesome Company"/>
  <add key="PhoneNumber" value="(513) 555-4444"/>
</appSettings>

Then, point to these files in the app.config, as shown below:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings configSource="connections.config" />
  <appSettings configSource="appSettings.config" />
</configuration>
One caveat to doing this: The configSource files (connections.config and appSettings.config in our example) must be in the same folder as the config file. We can accomplish this by selecting each configSource file in Solution Explorer and setting its Copy to Output directory property to either “Copy always” or “Copy if newer”.
Friday, May 25, 2012 3:19:00 PM (GMT Daylight Time, UTC+01:00)
# Thursday, May 24, 2012

Here is Phil Japikse’s presentation on Testing Code From The Pit of Despair at the March 2012 Great Lakes Area .NET User Group (GANG).

Thursday, May 24, 2012 5:06:00 PM (GMT Daylight Time, UTC+01:00)
# Monday, May 21, 2012
Monday, May 21, 2012 5:06:00 PM (GMT Daylight Time, UTC+01:00)
# Thursday, May 17, 2012

In October, the Great Lakes Area .NET User Group (GANG) celebrated 10 years this past October with an all-day event. Here is Godfrey Nolan’s presentation on Executable Requirements or BDD in .NET.

.Net | Agile | ALM | Video
Thursday, May 17, 2012 4:54:00 PM (GMT Daylight Time, UTC+01:00)
# Wednesday, May 16, 2012

The SQL Server master database contains many dynamic views that contain information about the current state of the SQL Server. One such view is dm_os_workers, which lists all active threads run by SQL Server and information about each thread. Of particular interest are the error columns:
One or more of the following bit columns will return 1 if there is anything is going wrong with a thread:

  • is_sick
  • is_in_cc_exception
  • is_fatal_exception
  • is_inside_catch

One limitation of this view is that it does not retain any history. If you want to keep a history of threads, you can create a Database to hold that history and the following SQL to copy the results of dm_os_workers to a table in that database. The following SQL copies the dm_os_workers view results to the dbo.ThreadsHistory table in the Instrumentation database.

IF  NOT EXISTS 
    (SELECT * 
    FROM Instrumentation.sys.objects 
    WHERE object_id = OBJECT_ID(N'[Instrumentation].[dbo].[ThreadsHistory]') 
    AND type IN (N'U'))
BEGIN
SELECT
        GETDATE() AS TimeLogged,
        worker_address, 
        status, 
        is_preemptive, 
        is_fiber, 
        is_sick, 
        is_in_cc_exception, 
        is_fatal_exception, 
        is_inside_catch, 
        is_in_polling_io_completion_routine, 
        context_switch_count, 
        pending_io_count, 
        pending_io_byte_count, 
        pending_io_byte_average, 
        wait_started_ms_ticks, 
        wait_resumed_ms_ticks, 
        task_bound_ms_ticks, 
        worker_created_ms_ticks, 
        exception_num, 
        exception_severity, 
        exception_address, 
        locale, 
        affinity, 
        state, 
        start_quantum, 
        end_quantum, 
        last_wait_type, 
        return_code, 
        quantum_used, 
        max_quantum, 
        boost_count, 
        tasks_processed_count, 
        fiber_address, 
        task_address, 
        memory_object_address, 
        thread_address, 
        signal_worker_address, 
        scheduler_address, 
        processor_group
    INTO [Instrumentation].[dbo].[ThreadsHistory]
    FROM sys.dm_os_workers 
    WHERE 1=0
END

DECLARE @TimeNow AS DATETIME
SELECT @TimeNow = GETDATE() 

INSERT INTO Instrumentation.dbo.ThreadsHistory
(
    TimeLogged,
    worker_address, 
    status, 
    is_preemptive, 
    is_fiber, 
    is_sick, 
    is_in_cc_exception, 
    is_fatal_exception, 
    is_inside_catch, 
    is_in_polling_io_completion_routine, 
    context_switch_count, 
    pending_io_count, 
    pending_io_byte_count, 
    pending_io_byte_average, 
    wait_started_ms_ticks, 
    wait_resumed_ms_ticks, 
    task_bound_ms_ticks, 
    worker_created_ms_ticks, 
    exception_num, 
    exception_severity, 
    exception_address, 
    locale, 
    affinity, 
    state, 
    start_quantum, 
    end_quantum, 
    last_wait_type, 
    return_code, 
    quantum_used, 
    max_quantum, 
    boost_count, 
    tasks_processed_count, 
    fiber_address, 
    task_address, 
    memory_object_address, 
    thread_address, 
    signal_worker_address, 
    scheduler_address, 
    processor_group
)
(
    SELECT
        @TimeNow,
        worker_address,
        status, 
        is_preemptive, 
        is_fiber, 
        is_sick, 
        is_in_cc_exception, 
        is_fatal_exception, 
        is_inside_catch, 
        is_in_polling_io_completion_routine, 
        context_switch_count, 
        pending_io_count, 
        pending_io_byte_count, 
        pending_io_byte_average, 
        wait_started_ms_ticks, 
        wait_resumed_ms_ticks, 
        task_bound_ms_ticks, 
        worker_created_ms_ticks, 
        exception_num, 
        exception_severity, 
        exception_address, 
        locale, 
        affinity, 
        state, 
        start_quantum, 
        end_quantum, 
        last_wait_type, 
        return_code, 
        quantum_used, 
        max_quantum, 
        boost_count, 
        tasks_processed_count, 
        fiber_address, 
        task_address, 
        memory_object_address, 
        thread_address, 
        signal_worker_address, 
        scheduler_address, 
        processor_group
    FROM sys.dm_os_workers 
)
You can use SQL Agent to schedule a job that runs this every 60 seconds (or however frequently you want) to keep a history of the threads being generated by SQL. This history can tell you if threads are generating exception and if thread counts are increasing.

Steve Latsch contributed to this article.
Wednesday, May 16, 2012 3:05:00 PM (GMT Daylight Time, UTC+01:00)
# Tuesday, May 15, 2012

In October, the Great Lakes Area .NET User Group (GANG) celebrated 10 years this past October with an all-day event. Here is Richard Hale Shaw’s presentation from that meeting, in which he helps developers better get into “the zone”, where they can write code more efficiently.

Tuesday, May 15, 2012 3:43:00 PM (GMT Daylight Time, UTC+01:00)
# Monday, May 14, 2012
Monday, May 14, 2012 6:27:00 PM (GMT Daylight Time, UTC+01:00)
# Sunday, May 13, 2012

Here is Darrell Hawley's presentation at GANG10, the October 1 event celebrating 10 years of the Great Lakes Area .NET User Group. Darell tells a parable describing how projects can be run right or wrong.

Sunday, May 13, 2012 3:36:00 PM (GMT Daylight Time, UTC+01:00)
# Saturday, May 12, 2012

Here is Bill Wagner's presentation at GANG10, the October 1 event celebrating 10 years of the Great Lakes Area .NET User Group. Bill talks about asynchronous programming, including the new features coming in C# 5.

.Net | C# | Video
Saturday, May 12, 2012 3:39:00 PM (GMT Daylight Time, UTC+01:00)
# Friday, May 11, 2012

The Great Lakes Area .NET User Group celebrated 10 years this past October with an all-day event, kicked off by a Leon Gersing Keynote. Here is a video of that keynote

Friday, May 11, 2012 4:39:00 PM (GMT Daylight Time, UTC+01:00)
# Monday, May 7, 2012
Monday, May 7, 2012 4:19:00 PM (GMT Daylight Time, UTC+01:00)
# Friday, May 4, 2012

At Codeslingers last night, someone pulled out some coding Katas. For those who don’t know, a Kata is a coding exercise that is designed to practice your programming skills, rather than to solve a particular business problem. I was handed the classic “FizzBuzz” problem. The assignment:

Create a function that will print the integers from 1 to 100 with the following exceptions:

  • If a number is divisible by 3, print the word “Fizz” in place of that number.
  • If a number is divisible by 5, print the word “Buzz” in place of that number.
  • If a number is divisible by both 3 and 5, print the word “FizzBuzz” in place of that number.

The output should look something like the following:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16

I started with a C# console application because that is the language with which I am most familiar. It was able to finish the following in under 2 minutes. It took me 5 minutes to write the unit tests.

class Program
{
    static void Main(string[] args)
    {
        for (int i = 1; i < 100; i++)
        {
            var p = FizzBuzz(i);
            Console.WriteLine(p);
        }
        Console.ReadLine();
    }

    protected static string FizzBuzz(int i)
    {
        if (i % 15 == 0)
            return "FizzBuzz";
        if (i % 3 == 0)
            return "Fizz";
        if (i % 5 == 0)
            return "Buzz";
        return i.ToString();
    }
}

I only occasionally code in JavaScript, so I tackled that language next. Someone recommended using http://jsfiddle.net/
as an online IDE for writing and sharing JavaScript, so I tried it and liked it. Of course, JavaScript is a dynamic language and one of my big challenges was spelling things correctly without all the help Visual Studio provides when writing in a statically-typed language. In my case, I misspelled the id of a div, which cost me at least 15 minutes. I created the following boilerplate HTML:

<html>
    <body>
        <div id="fizzbuzz"></div>
    </body>
</html>

Then, I used the following JavaScript (plus a bit of jQuery) to output the FizzBuzz results:

for (i = 1; i <= 100; i++) {
    $("#fizzbuzz").append(function() {
        var newLine = i;
        if (i % 3 === 0) {
            newLine = "Fizz";
        }
        if (i % 5 === 0) {
            newLine = "Buzz";
        }
        if (i % 15 === 0) {
            newLine = "FizzBuzz";
        }
        var newDiv = $("<div>").text(newLine);
        return newDiv;
    });
}

A simple program like this provides a fun way to practice an old language and to learn a new language. Next up, I’ll try this program with F# and Ruby, since I have very little experience with these languages.

Friday, May 4, 2012 12:40:00 AM (GMT Daylight Time, UTC+01:00)
# Wednesday, May 2, 2012
Wednesday, May 2, 2012 5:56:00 PM (GMT Daylight Time, UTC+01:00)