关于c# CultureInfo以及与SQL Server的关系的一些疑问
本文关键字:关系 Server SQL CultureInfo 关于 | 更新日期: 2023-09-27 18: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:为了将我的对象插入到数据库中,我使用如下命令:
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
这实际上与文化无关,与en-GB无关,与SQL server无关;更简单地说,xml按照ISO 8601格式定义日期。到此为止。
不应该使用
DateTime.Parse
用xml指定en-GB
。不正确
正确的实现是:
DateTime when = XmlConvert.ToDateTime(...);
或者更方便地使用XElement
等:
DateTime when = (DateTime)el;
如果您想从xml中讨论SQL server 中的日期,那么只需:将它们视为date
或datetime
(而不是字符串)-通过类型化列或类型化参数。这样就不会在格式或区域设置上有任何混淆。
在您的编辑中,这与xml无关。假设v.Published
是DateTime
, [PUBLISHED]
是datetime
,那么它应该工作得很好-这些值永远不是字符串。DateTime
/datetime
实际上是一个数字(一个定义的时间间隔)。因为它们从来不是字符串,所以:文化和格式不涉及。
也许你很困惑如何从/到数据库读取或写入日期时间数据。
如果数据库中有datetime列
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();
}
但是当你从数据库中读取它时,它会自动变成datetime对象。
// 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"];
}
}