如何在离线模式下提供唯一ID?
在发票形式的客户端-服务器会计应用程序中,当用户保存发票时,它会从服务器获取诸如 90134 之类的发票编号,并使用该编号保存发票
客户需要发票编号。
那么在离线模式下(比如当网络掉线时)如何提供唯一的ID?
像这样的模式使用 String Id 好吗:客户端 + 增量号?
我不想使用 GUID。
In a client-server accounting application in invoice form when a user saves an invoce it gets An invoice number like 90134 from server and saves the invoice with that number
The invoice number is needed for the customer.
So in Offline mode (like when the network dropped) how provide a unique id?
Is it good to use String Id like this pattern: client + incremental number?
I don't want to use GUID
s.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您提前知道离线期间将为每个客户生成多少发票号码,您是否能够预先分配发票号码?例如,如果每个客户在每个离线期间可能仅生成 4 张发票,您可以为每个客户分配 4 个号码的块。这可能涉及数据库中的一个额外列来存储一个值,该值指示该号码是已创建的发票还是预分配的号码。根据数据库内的结构和约束,您可能还需要存储一些虚拟数据以强制引用完整性。
缺点是您的数字块可能不会按顺序使用,甚至根本不会使用,因此您的发票编号不会按时间顺序排列。此外,如果可用号码池用完,您也会遇到问题。
If you know in advance how many invoice numbers you will generate per client during an offline period, would you be able to pre-allocate invoice numbers? e.g. if each client is likely only to generate 4 invoices per offline period, you could allocate a block of 4 numbers to each client. This may involve an extra column in your DB to store a value indicating whether the number is an invoice already created, or a preallocation of a number. Depending on the structure and constraints within your DB, you may also need to store some dummy data to enforce referential integrity.
The downsides would be that your block of numbers may not get used sequentially, or indeed at all, so your invoice numbers would not be in chronological order. Also, you would run into problems if the pool of available numbers is used up.
您可以使用Guid:
在SQL Server中是相应的类型
uniqueidentifier
。一般来说,Guid 是 128 位数字。
有关 Guid 的更多信息,您可以阅读:
http://en.wikipedia.org/wiki/Globally_unique_identifier
< a href="http://msdn.microsoft.com/en-us/library/system.guid.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.guid.aspx
You can use Guid:
In SQL server is corresponding type
uniqueidentifier
.In general the Guid is 128-bit number.
More about Guid you can read:
http://en.wikipedia.org/wiki/Globally_unique_identifier
http://msdn.microsoft.com/en-us/library/system.guid.aspx
我认为发票号码(整数)是增量的:在这种情况下,由于您无法知道最后一个发票号码,因此您可以将发票保存在本地 db/cache/xml 中,而无需发票号码并等待网络连接将新记录插入数据库(然后将生成发票号码)
I suppose the invoice number (integer) is incremental: in this case, since you have no way of knowing the last invoice number, you could save the invoice in a local db/cache/xml without the invoice Number and wait for the network connection to insert the new records in the DB (the invoice number would be generated then)
您可以在不同的范围内为每个客户端设置号码...例如:
当有连接时时不时更新它们以避免重叠.
它不是 100% 防弹,但至少比什么都没有好。
我最喜欢的仍然是
GUID
,因为它们始终是唯一的。You could start your numbers for each client at a different range... e.g.:
Update them every now and then when there is a connection to avoid overlaps.
It's not 100% bulletproof, but at least it's better than nothing.
My favorite would still be a
GUID
for this, since they're always unique.有一个解决方法,但这只是一个“肮脏的黑客”,您应该认真重新考虑在离线时接受新的数据条目,特别是在处理要插入到许多表中的唯一 ID 时。
假设您的本地数据集中有一个“orders”表和另一个“orderDetails”表:
1- 在“orders”表中添加整数类型的 tmpID 以临时标识每个唯一订单。
2- 在流程的其余部分中使用新创建的订单的 tmpID(例如将产品添加到 orderDetails 表中的当前订单)
-->连接到服务器后,在单个事务中执行以下操作
1- 在“orders”表中插入第一个订单
2- 在 SQL Server 上生成其唯一 ID
3- 搜索“orderDetails”中具有currentOrder.tmpID 的 tmpID 并将它们插入到服务器上的“orderDetails”表中
4- 提交事务并继续到下一行。
请记住,这是非常糟糕的编码,并且可能会变得非常脏并且难以维护。
There is a workaround, but it is merely a "dirty hack", you should seriously reconsider accepting new data entries while offline, especially when dealing with unique IDs to be inserted in many tables.
Say you have an "orders" table and another "orderDetails" table in your local dataset:
1- add a tmpID of type integer in your "orders" table to temporarily identify each unique order.
2- use the tmpID of your newly created order in the rest of the process (say for adding products to the current order in the orderDetails table)
--> once you are connected to the server, in a single transaction do the following
1- insert the first order in the "orders" table
2- get its uniqueID generated on your SQL server
3- search for each line in "orderDetails" that have a tmpID of currentOrder.tmpID and insert them in the "orderDetails" table on your server
4- commit the transaction and continue to the following row.
Keep in mind that this is very bad coding and that it can get real dirty and hard to maintain.
当必须按时间顺序且不丢失号码时,似乎不可能使用两个离线的不同系统创建唯一号码。
恕我直言,如果最后一个数字(在服务器上)是 10,就无法知道我是否应该返回 11 还是 12;我必须知道 11 是否已被其他人使用。
我只能想象使用临时号码,然后对这些号码重新编号,但如果发票已打印并且号码无法更改,我不知道如何完成这样的解决方案。
it looks like impossible to create unique numbers with two different systems both offline when it must be chronological and without missing numbers.
imho there is no way if the last number (on the server) was 10, to know if i should return 11 or 12; i would have to know if 11 was already used by another person.
I can only imagine to use a temporary number and later on renumber those numbers, but if the invoices are printed and the number can not be changed, i don't know how you could accomplish such a solution.