【问题标题】:Some doubts related to the C# CultureInfo and the relation with SQL Server关于 C# CultureInfo 以及与 SQL Server 关系的一些疑问
【发布时间】:2014-06-10 10:08:08
【问题描述】:

我是 C# 的新手,我对 System.Globalization.CultureInfo 类的工作方式有以下疑问。

我有以下情况。

在一个类中,我读取了一些值(使用 XPath 从 XML 中),其中包含一些信息作为一些数据字段。

所以我有类似的东西:

System.Globalization.CultureInfo cultureEN = new System.Globalization.CultureInfo("en-GB");

currentDeepSightVuln.LastUpdated = DateTime.Parse(n_alertdocument.SelectSingleNode("./x:LastUpdated", nsmgr).InnerText, cultureEN);

在我的 XML 文件中,这个字段是:

<x:LastUpdated>2014-05-21T17:00:38</x:LastUpdated>

所以它的价值是 2014-05-21T17:00:38

在执行上一个操作(初始化 LastUpdate 对象属性)时运行我的程序,我知道 **LastUpdate 属性值为:{21/05/2014 17: 00:38}.

如您所见,它具有不同于 XML 字段值的格式。我认为这很正常,因为它是根据 en-GB 设置进行转换的。

我的疑问是:如果我将我的对象保存在数据库表(Microsoft SQL Server)上,我会不会有一些问题,或者它是从 SQL Server 以更正的形式重新转换的?

编辑 1: 要将我的对象插入数据库,我使用类似的东西:

公共长插入(DataModel.Vulnerability.Vuln v) {

        _strSQL = "";
        string strSQLParametri = "";
        string query = "";

        long newId;
        long VulnerabilityAlertDocumentPkValue;

        System.Data.Common.DbCommand command;
        command = _connection.CreateCommand();
        try
        {
            _transactionBegin();
            // [VulnerabilityAlertId] insertion on the DB:
            if (v.VulnerabilityAlertId != null)
            {
                _strSQL = "INSERT INTO VulnerabilityAlertDocument ( [VulnerabilityAlertId] ";
                strSQLParametri = " VALUES (@VULNERABILITYALERTID ";
                _addParameter(command, "@VULNERABILITYALERTID ", newId);
            }

            ........................................................................
            ........................................................................
            ........................................................................

            _strSQL += ",[PUBLISHED] ";
            strSQLParametri += ", @PUBLISHED ";
            _addParameter(command, "@PUBLISHED", v.Published);

            ........................................................................
            ........................................................................
            ........................................................................

            query = _strSQL + " ) " + strSQLParametri + " );";
            command.CommandText = query;
            _executeNoQuery(command);

            VulnerabilityAlertDocumentPkValue = _getIdentity();
            Debug.WriteLine("PK della tabella VulnerabilityAlertDocumentPkValue: " + VulnerabilityAlertDocumentPkValue);

Tnx

【问题讨论】:

  • 如何从应用程序(SqlCommand)保存到数据库?以及数据库列的数据类型(datetime、varchar)是什么?
  • datetime 的值是 int 类型的,不管你如何查看它。因此,通过类型 datetime 保存它不会更改有关文化属性的值。它将被重新转换。
  • @rudym 不是一个整数; days 是整数部分; 时间是小数部分
  • @YuliamChandra 是的,我使用命令,我已经编辑了我的原始帖子,在原始帖子的末尾添加了一些关于我如何执行查询的信息
  • 通过编辑,我很困惑为什么这个问题谈到了 xml - 编辑中没有任何内容演示 xml。附带说明:如果您必须逐步构建 TSQL(而不是一次全部构建),您可能需要考虑 StringBuilder - 其中涉及很多中间 strings。跨度>

标签: c# sql sql-server globalization cultureinfo


【解决方案1】:

这实际上与文化无关,与 en-GB 无关,也与 SQL Server 无关;更简单地说,xml 以 ISO 8601 格式定义日期。到此结束。

不应使用 DateTime.Parse 指定 en-GB 和 xml。这是不正确的

正确的实现是:

DateTime when = XmlConvert.ToDateTime(...);

或者更方便,如果使用XElement 等:

DateTime when = (DateTime)el;

如果您想从 xml 中讨论 SQL 服务器中的日期远离,那么只需:将它们视为 datedatetime(而不是字符串) - 通过类型化列或类型化参数。这样就不会对格式或语言环境产生任何混淆。


通过您的编辑,这再次与 xml 无关。假设v.PublishedDateTime[PUBLISHED]datetime,那么它应该可以正常工作 - 这些值绝不是字符串DateTime / datetime 实际上是一个数字(某个定义的时间间隔到一个定义的时期)。而且由于它们从来都不是字符串,所以再次重申:文化和格式不涉及

【讨论】:

  • 嗯,你是说如果我将日期(从我的 XML 中获取)放入 DateTime 字段,然后将其放入数据库中,我就没有问题了吗?
  • 好的......如果改为正确的日期(如 2014-05-21T17:00:38)会出现什么我会有一个不正确的日期为 2014-20-10T17:00:38 ???
  • @AndreaNobili 只要您正确从xml中获取它,那么确实:应该没有问题。如果您的日期不正确,例如2014-20-10T...,那么XmlConvert 应该拒绝解析它。
  • 好的,我试过了,如果日期不正确,它会爆炸并抛出异常
【解决方案2】:

也许您对如何从/向数据库读取或写入日期时间数据感到困惑。

如果数据库中有日期时间列

create table anything
(
    published datetime
)

当你把它保存到数据库时,你需要将它保存为日期时间对象或有效的日期时间字符串,因为如果你将参数作为字符串传递,则会自动转换。

// Writes.
using (var connection = new SqlConnection("data source=.; initial catalog=master; Integrated Security=True;"))
{
    connection.Open();

    var command = new SqlCommand("insert into anything (published) values (@published)", connection);
    command.Parameters.AddWithValue("@published", "2014-05-21T17:00:38");
    // Not working as it will throw "Conversion failed when converting date and/or time from character string.".
    //command.Parameters.AddWithValue("@published", "2014-12345-21T17:00:38");
    command.ExecuteScalar();
}

但是当你从数据库中读取它时,它会自动成为日期时间对象。

// Reads.
using (var connection = new SqlConnection("data source=.; initial catalog=master; Integrated Security=True;"))
{
    connection.Open();

    var command = new SqlCommand("select * from anything", connection);
    var reader = command.ExecuteReader();
    while (reader.Read())
    {
        var publishedDate = (DateTime)reader["published"];
    }
}

【讨论】:

    猜你喜欢
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 2020-08-07
    • 1970-01-01
    • 2010-12-27
    • 1970-01-01
    • 2018-08-04
    相关资源
    最近更新 更多