且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在VB.NET中自动生成id

更新时间:2022-10-14 23:17:31

这取决于您想要做什么,以及如何存储信息。仔细思考这个问题很重要:弄错它会导致一些可怕的后果。



例如,您的第一个问题很简单:您是如何使用它的?这是一个单用户应用程序还是多用户?这产生了巨大的差异:多用户唯一ID需要比单个用户更多的关注!

数据存储系统也将影响您生成它的方式:如果您使用数据库,那么您可能会让它为你做大部分繁重的工作,因为他们通常有创建独特价值的系统,然后你可以用它作为你特定身份证格式的基础。



您需要考虑的其他问题:SR18位来自哪里?什么时候改变了?它是如何变化的? 18是今年的最后两位数吗?如果是这样,那么什么时候改变? 1月1日,或财政年度结束时(英国4月1日为税务目的,但如果公司财政年度取决于公司何时开始交易,可以是任何日期)? SR位何时发生变化,为什么?该怎么办?



它不像这行代码那么简单,甚至不是这个方法或这个算法 - 需要非数字自动ID值在你开始使用代码之前要仔细研究出来!


I want my id like SR18/1 and then SR18/2....like this.what will be my code for increment..

What I have tried:

I cant be able to generate SR18/2

It depends on exactly what you want to do, and how you are storing the information. And it's important to think carefully about this: get it wrong and it can cause some horrible effects.

For example, your first problem is simple: how are you using this - is this a single user app, or multiuser? This makes a huge difference: multiuser unique IDs need a heck of a lot more care than single user!
The data storage system will also affect how you generate it: if you are using a database, then you can probably let it do most of the "heavy work" for you as they generally have systems for creating unique values which you can then use as a basis for your specific ID format.

Other problems you need to think about: where does the "SR18" bit come from? When does that change? How does it change? Is the "18" the last two digits of the year? If so, when does that change? January 1st, or at the end of the financial year (April 1st in the UK for tax purposes, but can be any date if it's a companies financial year depending on when the company started trading)? When does the "SR" bit change, and why? What to?

It's not as simple as "this line of code", or even "this method" or "this algorithm" - non-numeric automatic ID values need to be carefully worked out long before you start with code!