Tuesday, October 5, 2010

Using the DateTimeFormatInfo to retrieve Culture Specific Date Values

This Blog entry discusses how to use the System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat object to access culture specific date values.

Retrieving Culture Specific Day Names
Access the DayNames property or AbbreviatedDayNames property to access a list of day names for the particular culture.
Example:

DateTimeFormatInfo d = Thread.CurrentThread.CurrentCulture.DateTimeFormat;
foreach (string abbreviatedDayName in d.AbbreviatedDayNames)
Console.WriteLine (abbreviatedDayName);

Retrieving Culture Specific Month Names
Access the MonthNames property or AbbreviatedMonthNames property to access a list of month names for the particular culture.
Example:

DateTimeFormatInfo d = Thread.CurrentThread.CurrentCulture.DateTimeFormat;
foreach (string abbreviatedMonthName in d.AbbreviatedMonthNames)
Console.WriteLine (abbreviatedDayName);

Play with it:

This tutorial allows you to retrieve culture specific date values in French:

1.       Set the CurrentCulture to French (Canada)
Thread.CurrentThread.CurrentCulture = new CultureInfo("FR-CA");

2.       Declare a DateTimeFormatInfo and set it to the CurrentThread’s DateTimeFormat property
DateTimeFormatInfo d = Thread.CurrentThread.CurrentCulture.DateTimeFormat;           

3.       Write the day names for French (Canada)
foreach (string dayName in d.DayNames)
Console.WriteLine(dayName);

4.       Write the month names for French (Canada)
foreach (string monthName in d.MonthNames )
Console.WriteLine(monthName);

Editing a Connection String through the DataLink properties screen

This Blog entry discusses how to edit a connection string using the Data Link Properties screen.

To edit a database connection string through the Data Link Properties screen, you would need the following references:
-          ADODB
-          MSDASC

Declare an object of type MSDASC.DataLinks and assign it a new instance.
Example:
MSDASC.DataLinks dataLink = new MSDASC.DataLinksClass();

Declare an ADODB Connection object.
Example:
ADODB._Connection connection;

Set  the connection object to a new ConnectionClass.
Example:
connection = new ADODB.ConnectionClass();

Set the connectionString property of the connection to the connectionString to be edited.
Example:
connection.ConnectionString = connectionString;


Call the PromptEdit method of the datalink and assign it to the ADODB connection and pass the connection as a reference parameter.
Example:
dataLink.PromptEdit(ref oConnection)

Access the connection string property of the connection object
return connection.ConnectionString.ToString();

Example Code:
MSDASC.DataLinks dataLinks = new MSDASC.DataLinksClass();
ADODB._Connection connection;
connection = new ADODB.ConnectionClass();
connection.ConnectionString = connectionString;
dataLink.PromptEdit(ref oConnection)
return connection.ConnectionString.ToString();

Obtain a Connection String through the Data Link Properties Screen

This Blog entry discusses how to obtain a connection string from users using the Data Link Properties screen.

To obtain a database connection string through the Data Link Properties screen, you would need the following references:
-          ADODB
-          MSDASC

Declare an object of type MSDASC.DataLinks and assign it a new instance.
Example:
MSDASC.DataLinks dataLink = new MSDASC.DataLinksClass();

Declare an ADODB Connection object.
Example:
ADODB._Connection connection;

Call the PromptNew method of the datalink and assign it to the ADODB connection. This will return an ADODB connection object.
Example:
connection = (ADODB._Connection)dataLinks.PromptNew();

Access the connection string property of the connection object
return connection.ConnectionString.ToString();

Example Code:
MSDASC.DataLinks dataLinks = new MSDASC.DataLinksClass();
ADODB._Connection connection;
connection = (ADODB._Connection)dataLinks.PromptNew();
return connection.ConnectionString.ToString();