使用用户自定义类型

  

       

#region 生成ID

一些复合类型进行范式分解是没有必要的,尤其是一些统一模型的情况下

一些复合类型进行范式分解是没有必要的,尤其是一些统一模型的情况下

public static string CreateGuid(int index)
{
//CAST(CAST(NEWID() AS BINARY(10)) + CAST(GETDATE() AS BINARY(6)) AS
UNIQUEIDENTIFIER)
byte[] guidArray = Guid.NewGuid().ToByteArray();
DateTime now = DateTime.Now;

  

     

DateTime baseDate = new DateTime(1900, 1, 1);

DECLARE @i TimeBalance

  1. SET NOCOUNT
    ON

TimeSpan days = new TimeSpan(now.Ticks – baseDate.Ticks);

SET @i = CAST(‘0/102’ AS TimeBalance)

  1. DECLARE @i TimeBalance

  2. SET @i =
    CAST(‘D/2015-1-1/7’
    AS
    TimeBalance)

  3. SELECT @i

  4. SELECT @i.ToString()

  5. SELECT @i.ToTimeString()

  6. SELECT @i.Distance()
    AS
    Distance;

  7.  

  8. WITH a
    AS
    (
    SELECT
    ‘100’ s ,

  9.                         ” m

  10.                UNION
    ALL

TimeSpan msecs = new TimeSpan(now.Ticks – (new DateTime(now.Year,
now.Month, now.Day).Ticks));
byte[] daysArray = BitConverter.GetBytes(days.Days);
byte[] msecsArray =
BitConverter.GetBytes((long)((msecs.TotalMilliseconds + index * 10) /
3.333333));

SELECT @i

  1.                SELECT
    ‘281474976710693’ s ,

  2.                         ’37次’ m

  3.                UNION
    ALL

Array.Copy(daysArray, 0, guidArray, 2, 2);
//毫秒高位
Array.Copy(msecsArray, 2, guidArray, 0, 2);
//毫秒低位
Array.Copy(msecsArray, 0, guidArray, 4, 2);
return new System.Guid(guidArray).ToString();
}

SELECT @i.ToString()

  1.                SELECT
    ‘Y/2015-1-5/1’ s ,

  2.                         ‘包年’ m

  3.                UNION
    ALL

/// <summary>
/// 创建一个按时间排序的Guid
/// </summary>
/// <returns></returns>
public static string CreateGuid()
{
//CAST(CAST(NEWID() AS BINARY(10)) + CAST(GETDATE() AS BINARY(6)) AS
UNIQUEIDENTIFIER)
byte[] guidArray = Guid.NewGuid().ToByteArray();
DateTime now = DateTime.Now;

SELECT @i.ToTimeString()

  1.                SELECT
    ‘M/2015-1-5/11’ s ,

  2.                         ‘包个月’ m

  3.                UNION
    ALL

DateTime baseDate = new DateTime(1900, 1, 1);

SELECT @i.Distance()

  1.                SELECT
    ‘D/2015-1-1/7’ s ,

  2.                         ‘包天’ m

  3.                UNION
    ALL

TimeSpan days = new TimeSpan(now.Ticks – baseDate.Ticks);

SELECT @i.Distance2(‘mi’)

  1.                SELECT
    ‘1500438729850887’ s ,

  2.                         ‘包天’ m

  3.                UNION
    ALL

TimeSpan msecs = new TimeSpan(now.Ticks – (new DateTime(now.Year,
now.Month, now.Day).Ticks));
byte[] daysArray = BitConverter.GetBytes(days.Days);
byte[] msecsArray =
BitConverter.GetBytes((long)(msecs.TotalMilliseconds / 3.333333));

  

  1.                SELECT
    ‘H/2015-1-5 8:30/10’ s ,

  2.                         ‘包小时’ m

  3.              )

  4.     SELECT
    m + ‘ ‘ + s ,

  5.             t.ToString() string ,

Array.Copy(daysArray, 0, guidArray, 2, 2);
//毫秒高位
Array.Copy(msecsArray, 2, guidArray, 0, 2);
//毫秒低位
Array.Copy(msecsArray, 0, guidArray, 4, 2);
return new System.Guid(guidArray).ToString();
}

SET @i = CAST(‘2015-1-1/2016-8-21’ AS TimeBalance)

  1.             t.ToHex() hex ,

  2.             t.ToTimeString() timeString
    ,

  3.             t.ToValue()
    value
    ,

  4.             t.Distance() distance

public static DateTime GetDateTimeFromGuid(string strGuid)
{
Guid guid = Guid.Parse(strGuid);

SELECT @i.ToString() string ,

  1.     FROM
    (
    SELECT
    s ,

  2.                         m ,

  3.                         CAST
    (s
    AS
    TimeBalance) t

  4.               FROM
    A

  5.             ) tt

DateTime baseDate = new DateTime(1900, 1, 1);
byte[] daysArray = new byte[4];
byte[] msecsArray = new byte[4];
byte[] guidArray = guid.ToByteArray();

发表评论

电子邮件地址不会被公开。 必填项已用*标注