Export (0) Print
Expand All

Set locale for data types

TypeName

SetLocaleForDataTypes

CheckId

CA1306

Category

Microsoft.Globalization

Breaking Change

NonBreaking

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).

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.

To fix a violation of this rule, explicitly set the locale for the DataTable or DataSet.

It is safe to exclude 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.

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;
        }
    }
}

Community Additions

ADD
Show:
© 2014 Microsoft