我想从SQL Server中的datetime字段读取并将其存储为int64,因为datetime在SQL Server中存储为64位。我会做类似的事吗?
DateTime dt = sqlDataReader.GetDateTime(0);
byte[] bt = BitConverter.GetBytes(dt);
// unfortunately, GetBytes() does not take DateTime as an argument
long ldt = (Convert.ToInt64(bt[0]) << 56)
+ (Convert.ToInt64(bt[1]) << 48)
+ (Convert.ToInt64(bt[2]) << 40)
+ (Convert.ToInt64(bt[3]) << 32)
+ (Convert.ToInt64(bt[4]) << 24)
+ (Convert.ToInt64(bt[5]) << 16)
+ (Convert.ToInt64(bt[6]) << 8)
+ (Convert.ToInt64(bt[7]));
答案 0 :(得分:0)
直接从SQL服务器获取日期值作为unixtimestamp样式的INT:
select datediff(second, {d'1970-01-01'}, yourdatefield) as seconds
它可以帮助你摆弄你的应用程序。
如果要在tsql中实现mysql样式的“unix_timestamp()”函数,此博客条目将显示如何为其创建函数:http://skinn3r.wordpress.com/2009/01/26/t-sql-datetime-to-unix-timestamp/
答案 1 :(得分:0)
为什么不dt.ToBinary()
?
使用System.GC.GetTotalMemory
,似乎DateTime
使用212个字节而long
使用8个字节。