CA1306: Set locale for data types
|
TypeName |
SetLocaleForDataTypes |
|
CheckId |
CA1306 |
|
Category |
Microsoft.Globalization |
|
Breaking Change |
Non-breaking |
A method or constructor created one or more System.Data.DataTable or System.Data.DataSet instances and did not explicitly set the locale property (DataTable.Locale or DataSet.Locale).
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 (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.
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; } } }