简体   繁体   English

验证和排序具有不同格式的各种序列号?

[英]Validate and sort a variety of serial numbers with different formats?

I am faced with the following issue. 我面临以下问题。 I need to capture a range of serial numbers in c# to be stored in a database. 我需要捕获c#中要存储在数据库中的一系列序列号。 If these serial numbers are just numbers, it is fine, but I have ran into serial numbers which are alpha-numeric such as: 如果这些序列号只是数字,那很好,但是我遇到了字母数字的序列号,例如:

56AAA71064D6 and 56AAA7105A25 56AAA71064D6和56AAA7105A25

How can I accommodate the different possibilities that serial number ranges may be in? 如何适应序列号范围可能存在的不同可能性?

Assuming that all serial numbers have some order, how can I parse the sequential part of it? 假设所有序列号都有一定顺序,我该如何解析其顺序部分?

The database takes the serial numbers as strings already. 数据库已经将序列号作为字符串。

If your database assumes the serial number is numeric, you either need to change that part of the DB to allow alphanumeric values, or else define a new mapping table to correlate the external serial number with a unique numeric ID so that you can continue to use numbers for the database's internal representation of a serial 'number'. 如果您的数据库假定序列号为数字,则您需要更改数据库的该部分以允许字母数字值,或者定义新的映射表以将外部序列号与唯一的数字ID相关联,以便您可以继续使用数据库的内部“序列”号的数字。

You'd also have to add support to hand out a new, unique internal serial number for a given alphanumeric input in a concurrency-safe way. 您还必须添加支持,以并发安全的方式为给定的字母数字输入分发新的唯一内部序列号。

EDIT: What I mean by this is that when a new alphanumeric serial number has to be registered in the DB, you should do this with a transaction that atomically adds a record to the original (numeric) serial number list and adds a second record in the new table that correlates the numeric value with the alphanumeric value. 编辑:我的意思是,当必须在数据库中注册新的字母数字序列号时,您应该使用一个将原子记录添加到原始(数字)序列号并在其中添加第二条记录的事务来执行此操作。将数值与字母数字值相关联的新表。 If this is not atomic then the scheme breaks down. 如果这不是原子的,则该方案将失败。 Removals have to be handled atomically also. 清除也必须原子处理。

If your serial numbers have same width then its easy... 如果您的序列号具有相同的宽度,那么它很容易...

you have to use string.compare method to get the range. 您必须使用string.compare方法来获取范围。

int MaxLength = 20;

private string Pad(string val){
   if(val.Length < 20){
      val = new String('0', MaxLength - val.Length) + val;
   }
   return val;
}

public bool IsBetween(string num, string start, string end){
    num = Pad(num);
    start = Pad(num);
    end = Pad(num);
    return String.Compare(num,start)>=0 && String.Compare(num,end)<=0;
}

And if you sort all serial numbers, then you can easily pickup range. 而且,如果您对所有序列号进行排序,那么您可以轻松地提取范围。

If they are not of same width then you have to padd 0 before smaller length strings to make them all same size. 如果它们的宽度不同,则必须在较小长度的字符串之前填充0,以使它们都具有相同的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM