帮一个朋友导数据时就发生自动编号的主键发生重复,花了我大半天的时间。累死了。给他们写了一个小工具,生成时间戳的代码:
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
使用方法:
直接调用IdentityGenerator.GetIdentity()
如果使用数据实体的化可以设计一个基类
1 public class BaseData{
2 private long _identity;
3 public class BaseData(){
4 this._identity = IdentityGenerator.GetIdentity();
5 }
6 //继承后在实体中定义其属性名,返回 或设置该值,如果名字相同可以将其改为public
7 protected long Identity{
8 get{return this._identity;}
9 set{
10 if(this._identity!=value)
11 this._identity = value;
12 }
13 }
14 //重新设置Id
15 public void ResetIdentity(){
16 this._identity = IdentityGenerator.GetIdentity();
17 }
18
19 }
2 private long _identity;
3 public class BaseData(){
4 this._identity = IdentityGenerator.GetIdentity();
5 }
6 //继承后在实体中定义其属性名,返回 或设置该值,如果名字相同可以将其改为public
7 protected long Identity{
8 get{return this._identity;}
9 set{
10 if(this._identity!=value)
11 this._identity = value;
12 }
13 }
14 //重新设置Id
15 public void ResetIdentity(){
16 this._identity = IdentityGenerator.GetIdentity();
17 }
18
19 }
简单才好!
优点:使用bigint型字段、比Guid好看些,大范围内是按顺序排列的,基本不会在前面的序列插入,短时间内(1秒钟)可能有无序产生,不过影响不是太大。简单省事
缺点:仍然存在重复的可能性,对于并发处理量超大的请仔细斟酌再三考虑。
仍然有人批评生成的3214578921之类的id太难看了,而且长:(
我想对于在浏览器上靠猜测
view.aspx?id=124下一个帖子125是什么的人可能不管用了,因为相邻两个id间距不等,至少也有好几千的:)可以起到 一个保护性作用吧。
裸机里面用记事本敲代码真麻烦,到此为止吧。