【发布时间】:2010-03-09 13:41:56
【问题描述】:
我使用 Sql Compact3.5 作为我的数据库和 C# .NET 在不同的系统中,我得到的日期时间格式不同。在 Windows XP 中,它以以下格式检索日期时间:MM-dd-yyyy HH:mm:ss,在媒体中心中,它以以下格式检索:MM/dd/yyyy hh:m:ss。有什么方法可以使日期时间格式不受文化影响,或者我可以在 sql compact 中设置日期时间格式,所以让它成为任何只使用该格式的 PC 吗???
例子:
//TimeOfCall is passed as String using the format DateTime.Now.ToString("MM-dd-yyyy HH:mm:ss");
using (SqlCeConnection con = new SqlCeConnection(ConString))
{
using (SqlCeCommand SqlceCmd = new SqlCeCommand(
"Insert into myreports(TimeOfCall,Status) values(?,?)", con))
{
if (con.State == ConnectionState.Closed)
con.Open();
SqlceCmd.Parameters.Add(new SqlCeParameter("@TimeOfCall", strTimeOfCall));
SqlceCmd.Parameters.Add(new SqlCeParameter("@Status", strStatus));
int RowsaAffected = SqlceCmd.ExecuteNonQuery();
con.Close();
return RowsaAffected;
}
}
在重新检索记录时,以这种方式使用查询:
//FromTime and ToTime are passeed in the same format as while storing
using (SqlCeConnection con = new SqlCeConnection(ConString))
{
using (SqlCeDataAdapter SqlceDA = new SqlCeDataAdapter("Select TimeOfCall from myreports where TimeOfCall between '" + strFromTime + "' and '" + strToTime + "' order by TimeOfCall", con))
{
if (con.State == ConnectionState.Closed)
con.Open();
SqlceDA.Fill(dtReports);
con.Close();
return dtReports;
}
}
我希望它很清楚
【问题讨论】:
标签: c# datetime sql-server-ce globalization