2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-04-28 16:49:39 +02:00
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++``).
This rule raises an issue when ``++System.Data.DataTable++`` or ``++System.Data.DataSet++`` instances are created without explicitly setting the locale property (``++DataTable.Locale++`` or ``++DataSet.Locale++``).
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2021-04-28 16:49:39 +02:00
----
using System;
using System.Data;
namespace MyLibrary
{
public class Foo
{
public DataTable CreateTable()
{
DataTable table = new DataTable("Customers"); // Noncompliant table.Locale not set
DataColumn key = table.Columns.Add("ID", typeof(Int32));
key.AllowDBNull = false;
key.Unique = true;
table.Columns.Add("LastName", typeof(String));
table.Columns.Add("FirstName", typeof(String));
return table;
}
}
}
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2021-04-28 16:49:39 +02:00
----
using System;
using System.Data;
using System.Globalization;
namespace MyLibrary
{
public class Foo
{
public DataTable CreateTable()
{
DataTable table = new DataTable("Customers");
table.Locale = CultureInfo.InvariantCulture;
DataColumn key = table.Columns.Add("ID", typeof(Int32));
key.AllowDBNull = false;
key.Unique = true;
table.Columns.Add("LastName", typeof(String));
table.Columns.Add("FirstName", typeof(String));
return table;
}
}
}
----
2021-04-28 18:08:03 +02:00
2021-09-20 15:38:42 +02:00
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Set the locale for this ["DataTable" | "DataSet"].
=== Highlighting
"DataTable" or "DataSet" creation
2021-09-20 15:38:42 +02:00
endif::env-github,rspecator-view[]