【发布时间】: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