问题描述
如何在VB.NET中实现相同的目标,而在SQL Server中是如此轻松地做到这一点。
How do I achieve the same in VB.NET which is so easily done in SQL Server.
选择CAST(GETDATE()as VARBINARY(8))-以十六进制表示当前时间
现在我的问题是如何在VB.NET中创建相同的字符串,以便可以像这样在SQL Server中进行比较-
Now my question is how can I create the same string in VB.NET so that I can compare in SQL Server as such -
当GETDATE()= CAST(0X00009F5E00D8DF7C AS DATETIME)时选择情况,然后'TRUE'否则为'FALSE'结束-0X00009F5E00D8DF7C将是我转换为VB.NET时得到的值.NOW()十六进制
推荐答案
我不得不从SQL Server的十六进制转换dbscript中的某些日期格式字符串转换为标准日期时间字符串(与TSQL到MySQL脚本转换一起使用)。我使用了一些在这里查找并得出的代码:
I had to convert some dates in dbscript from SQL Server's hex format string to standard datetime string (for use with TSQL to MySQL script translation). I used some codes I looked up in here and came up with:
static string HexDateTimeToDateTimeString(string dateTimeHexString)
{
string datePartHexString = dateTimeHexString.Substring(0, 8);
int datePartInt = Convert.ToInt32(datePartHexString, 16);
DateTime dateTimeFinal = (new DateTime(1900, 1, 1)).AddDays(datePartInt);
string timePartHexString = dateTimeHexString.Substring(8, 8);
int timePartInt = Convert.ToInt32(timePartHexString, 16);
double timePart = timePartInt * 10 / 3;
dateTimeFinal = dateTimeFinal.AddMilliseconds(timePart);
return dateTimeFinal.ToString();
}
static string HexDateToDateString(string dateHexString)
{
int days = byte.Parse(dateHexString.Substring(0, 2), NumberStyles.HexNumber)
| byte.Parse(dateHexString.Substring(2, 2), NumberStyles.HexNumber) << 8
| byte.Parse(dateHexString.Substring(4, 2), NumberStyles.HexNumber) << 16;
DateTime dateFinal = new DateTime(1, 1, 1).AddDays(days);
return dateFinal.Date.ToString();
}
也许没有进行优化,但是显示了这个想法。
Maybe not optimized, but shows the idea.
这篇关于在VB.NET中将DateTime转换为等效的十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!