DateTime.Now.Ticks repeating inside a loop

前端 未结 6 1112
小鲜肉
小鲜肉 2021-01-27 02:55

I am trying to generate a unique ID for the primary key of a table and I am using DateTime.Now.Ticks for it. That\'s a requirement for now we can\'t use Ident

6条回答
  •  无人共我
    2021-01-27 03:33

    The better approach is use auto-generated Id

    but If you want to stick with DataTime Tick option, then you can use this approach that will give you a unique number base on your time.

    Random rand = new Random(DateTime.Now.Millisecond);
    

    Also, you can use

    Guid guid = Guid.NewGuid();
    

    That'll also give you a unique number.

    Thanks!

提交回复
热议问题