CA1306: Set locale for data types
Item | Value |
---|---|
RuleId | CA1306 |
Category | Microsoft.Globalization |
Breaking change | Non-breaking |
Cause
A method or constructor created one or more System.Data.DataTable or System.Data.DataSet instances and did not explicitly set the locale property (System.Data.DataTable.Locale or System.Data.DataSet.Locale).
Rule description
The locale determines culture-specific presentation elements for data, such as formatting used for numeric values, currency symbols, and sort order. When you create a DataTable or DataSet, you should set the locale explicitly. By default, the locale for these types is the current culture. For data that is stored in a database or file and is shared globally, the locale should ordinarily be set to the invariant culture (System.Globalization.CultureInfo.InvariantCulture). When data is shared across cultures, using the default locale can cause the contents of the DataTable or DataSet to be presented or interpreted incorrectly.
How to fix violations
To fix a violation of this rule, explicitly set the locale for the DataTable or DataSet.
When to suppress warnings
It is safe to suppress a warning from this rule when the library or application is for a limited local audience, the data is not shared, or the default setting yields the desired behavior in all supported scenarios.
Example
The following example creates two DataTable instances.
using System;
using System.Data;
using System.Globalization;
namespace GlobalLibrary
{
public class MakeDataTables
{
// Violates rule: SetLocaleForDataTypes.
public DataTable MakeBadTable()
{
DataTable badTable = new DataTable("Customers");
DataColumn keyColumn = badTable.Columns.Add("ID", typeof(Int32));
keyColumn.AllowDBNull = false;
keyColumn.Unique = true;
badTable.Columns.Add("LastName", typeof(String));
badTable.Columns.Add("FirstName", typeof(String));
return badTable;
}
public DataTable MakeGoodTable()
{
DataTable goodTable = new DataTable("Customers");
// Satisfies rule: SetLocaleForDataTypes.
goodTable.Locale = CultureInfo.InvariantCulture;
DataColumn keyColumn = goodTable.Columns.Add("ID", typeof(Int32));
keyColumn.AllowDBNull = false;
keyColumn.Unique = true;
goodTable.Columns.Add("LastName", typeof(String));
goodTable.Columns.Add("FirstName", typeof(String));
return goodTable;
}
}
}