如何生成唯一的付款编号?

时间:2021-12-01 09:41:10

When you pay by paypal you receive payment number (as this PAY-8YW005202J095132TLI5UVIE) and I think this number is unique.
The question is how I can generate as this unique id, I work on system and this system save all transaction in DB into Transaction table and I want generate transaction number I build structure for this payment as this

当您通过PayPal付款时,您会收到付款号码(如此PAY-8YW005202J095132TLI5UVIE),我认为此号码是唯一的。问题是我如何生成这个唯一的id,我在系统上工作,这个系统将DB中的所有事务保存到Transaction表中,我希望为此付款生成事务编号I构建结构

(year part)(month part)(day in the month part)(*)

the problem in (*) part I want generate random and unique number for each day.
I work on EF6 with C#.Net

(*)部分中的问题我希望每天生成随机和唯一的数字。我使用C#.Net处理EF6

3 个解决方案

#1


-2  

I think you can use Random feature in C#. Random rnd = new Random(); rnd.Next(); // This is always unique as the value depends on date and time, which is continuously changing. Since Random usage has some disadvantages and as many people suggested, i am updating my answer.

我想你可以在C#中使用Random功能。随机rnd = new Random(); rnd.Next(); //这始终是唯一的,因为值取决于日期和时间,这是不断变化的。由于随机使用有一些缺点,并且有很多人建议,我正在更新我的答案。

Here is another way which you can generate unique key: DateTime.Now.ToString("yyMMddHHmmssff"); Here are more details about string format: msdn.microsoft.com/en-us/library/zdtaw1bw.aspx

这是另一种可以生成唯一键的方法:DateTime.Now.ToString(“yyMMddHHmmssff”);以下是有关字符串格式的更多详细信息:msdn.microsoft.com/en-us/library/zdtaw1bw.aspx

#2


3  

You can use Guid (global unique identifier). Try this:

您可以使用Guid(全局唯一标识符)。尝试这个:

var randomPart = Guid.NewGuid().ToString();

also, you can control it presentation, like this:

另外,你可以控制它的演示,如下所示:

var p = Guid.NewGuid().ToString("N")
    .Substring(0, 5); // note: trimming chars from guid make it less unique

this will create a string without dashes and 5 chars only.

这将创建一个没有短划线和5个字符的字符串。

Edit: Another options is base64 string. You may try something like this:

编辑:另一个选项是base64字符串。你可以尝试这样的事情:

var buffer = new byte[42]; // length is up to you
var random = RNGCryptoServiceProvider.Create();
random.GetBytes(buffer);
var randomPart = Convert.ToBase64String(buffer);

#3


2  

Never use Random() to create a unique identifier. Its results are not predictably unique. There's a GUID (Globally Unique Identifier) object for a reason, and this is the reason.

切勿使用Random()创建唯一标识符。其结果不可预测的独特之处。出于某种原因,有一个GUID(全局唯一标识符)对象,这就是原因。

var id = new Guid(); //call the ToString() method on this to get it as a string

That said, there is a chance you'll get duplicate Guid keys. The chance is less likely than someone throwing a ball in space and hitting a comet without aiming, but the chance is there. if you're particularly worried about that tiny chance, do something like this pseudocode:

也就是说,你有可能获得重复的Guid密钥。这个机会不如有人在太空中投球并在没有瞄准的情况下击中彗星的可能性,但机会就在那里。如果你特别担心这个微小的机会,做一些像这样的伪代码:

var id = new Guid();
while(myGuidArrayThatIGotFromMyDatabase.Contains(id))
{ id = new Guid(); }

And if you end up in an infinite loop with that code because you keep matching existing data, I'm going to have to ask you to mine Bitcoin, be my official representative in Vegas, and pick a bunch of lottery tickets for me.

如果你因为不断匹配现有数据而最终陷入无限循环,我将不得不要求你挖掘比特币,成为我在拉斯维加斯的官方代表,并为我挑选一堆彩票。

Also, while I mention above that you can .ToString() this to get the string equivalent of the GUID, it's important to note that not only does Guid(string value) exist as a Guid constructor, but also that a SQL Server database (and some others) will all support a Guid as a column value type in tables, which can eliminate your need to use strings for identifiers in the first place.

另外,虽然我在上面提到过你可以使用.ToString()来获取与GUID等效的字符串,但重要的是要注意Guid(字符串值)不仅作为Guid构造函数存在,而且还存在SQL Server数据库(所有人都支持Guid作为表格中的列值类型,这可以消除首先使用字符串作为标识符的需要。

#1


-2  

I think you can use Random feature in C#. Random rnd = new Random(); rnd.Next(); // This is always unique as the value depends on date and time, which is continuously changing. Since Random usage has some disadvantages and as many people suggested, i am updating my answer.

我想你可以在C#中使用Random功能。随机rnd = new Random(); rnd.Next(); //这始终是唯一的,因为值取决于日期和时间,这是不断变化的。由于随机使用有一些缺点,并且有很多人建议,我正在更新我的答案。

Here is another way which you can generate unique key: DateTime.Now.ToString("yyMMddHHmmssff"); Here are more details about string format: msdn.microsoft.com/en-us/library/zdtaw1bw.aspx

这是另一种可以生成唯一键的方法:DateTime.Now.ToString(“yyMMddHHmmssff”);以下是有关字符串格式的更多详细信息:msdn.microsoft.com/en-us/library/zdtaw1bw.aspx

#2


3  

You can use Guid (global unique identifier). Try this:

您可以使用Guid(全局唯一标识符)。尝试这个:

var randomPart = Guid.NewGuid().ToString();

also, you can control it presentation, like this:

另外,你可以控制它的演示,如下所示:

var p = Guid.NewGuid().ToString("N")
    .Substring(0, 5); // note: trimming chars from guid make it less unique

this will create a string without dashes and 5 chars only.

这将创建一个没有短划线和5个字符的字符串。

Edit: Another options is base64 string. You may try something like this:

编辑:另一个选项是base64字符串。你可以尝试这样的事情:

var buffer = new byte[42]; // length is up to you
var random = RNGCryptoServiceProvider.Create();
random.GetBytes(buffer);
var randomPart = Convert.ToBase64String(buffer);

#3


2  

Never use Random() to create a unique identifier. Its results are not predictably unique. There's a GUID (Globally Unique Identifier) object for a reason, and this is the reason.

切勿使用Random()创建唯一标识符。其结果不可预测的独特之处。出于某种原因,有一个GUID(全局唯一标识符)对象,这就是原因。

var id = new Guid(); //call the ToString() method on this to get it as a string

That said, there is a chance you'll get duplicate Guid keys. The chance is less likely than someone throwing a ball in space and hitting a comet without aiming, but the chance is there. if you're particularly worried about that tiny chance, do something like this pseudocode:

也就是说,你有可能获得重复的Guid密钥。这个机会不如有人在太空中投球并在没有瞄准的情况下击中彗星的可能性,但机会就在那里。如果你特别担心这个微小的机会,做一些像这样的伪代码:

var id = new Guid();
while(myGuidArrayThatIGotFromMyDatabase.Contains(id))
{ id = new Guid(); }

And if you end up in an infinite loop with that code because you keep matching existing data, I'm going to have to ask you to mine Bitcoin, be my official representative in Vegas, and pick a bunch of lottery tickets for me.

如果你因为不断匹配现有数据而最终陷入无限循环,我将不得不要求你挖掘比特币,成为我在拉斯维加斯的官方代表,并为我挑选一堆彩票。

Also, while I mention above that you can .ToString() this to get the string equivalent of the GUID, it's important to note that not only does Guid(string value) exist as a Guid constructor, but also that a SQL Server database (and some others) will all support a Guid as a column value type in tables, which can eliminate your need to use strings for identifiers in the first place.

另外,虽然我在上面提到过你可以使用.ToString()来获取与GUID等效的字符串,但重要的是要注意Guid(字符串值)不仅作为Guid构造函数存在,而且还存在SQL Server数据库(所有人都支持Guid作为表格中的列值类型,这可以消除首先使用字符串作为标识符的需要。