我是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: 要将我的对象插入db,我使用类似的东西:
public long insert(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
答案 0 :(得分:6)
这实际上并不是关于文化,不是关于en-GB,也不是关于SQL服务器的;更简单地说,xml定义了ISO 8601格式中指定的日期。那就结束了。
您 不应使用 DateTime.Parse
使用xml指定en-GB
。那是不正确。
正确的实施方式是:
DateTime when = XmlConvert.ToDateTime(...);
或者更方便的是,如果使用XElement
等:
DateTime when = (DateTime)el;
如果你想在xml中讨论SQL server away 中的日期,那么只需:将它们视为date
或datetime
(而不是字符串) - 通过键入列或作为类型参数。那么格式或语言环境就不会有任何混淆。
通过编辑,再次与xml无关。假设v.Published
是DateTime
,[PUBLISHED]
是datetime
,那么它应该可以正常工作 - 这些值绝不是字符串。 DateTime
/ datetime
实际上是数字(某个已定义的时间段定义的时间间隔)。而且因为它们永远不是字符串,所以:文化和格式不会涉及。
答案 1 :(得分:2)
也许您对如何从/向数据库读取或写入日期时间数据感到困惑。
如果您在数据库中有datetime列
create table anything
(
published datetime
)
将数据保存到数据库时,需要将其保存为datetime对象或有效的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"];
}
}