如何在C#中生成包含字母和数字的产品ID [英] How to generate product id that contains letter and numbers in C#

查看:108
本文介绍了如何在C#中生成包含字母和数字的产品ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在c#中创建一行代码,我将用它来提供我添加到产品表中的每个新产品?我使用的是MySQL数据库。前两个应该是一个字母,后跟一个数字和反斜杠然后是POS这个词。例如PC01 / POS。

这是我到目前为止所尝试的,虽然它不是我真正需要的。



什么我试过了:



 //自动产品编号
静态字符串IncrementID(字符串startValue,int numNonDigits)
{
string nonDigits = startValue.Substring(0,numNonDigits);
int len = startValue.Length - numNonDigits;
int number = int.Parse(startValue.Substring(numNonDigits));
number ++;
if(number> = Math.Pow(10,len))number = 1; //再次从1
开始返回String.Format({0} {1:D+ len.ToString()+},nonDigits,number);
}
private void Genarate_productcode()
{
product_code.Text = IncrementID(QIEpl / PO / 0000009,9); //生成QIEpl / PO / 0000010 // C00011 PCI0001
}

解决方案

除了 CHill60 [ ^ ]我强烈建议在MySQL服务器端使用字符串前缀创建产品ID。请参阅:如何使MySQL表成为主带有一些前缀的键自动增量 - Stack Overflow [ ^ ]


我个人不会在C#代码中生成ID我会使用MySQL数据库为我生成id:请参阅 MySQL :: MySQL 5.7参考手册:: 3.6.9使用AUTO_INCREMENT [ ^ ]



我肯定永远不会使用依赖于我递增之前数字的方法,因为它不能在多个中工作 - 用户环境。



一旦你有了您可以根据需要显示数字ID (未测试!)

  String  .Format(  PC {0} // POS,id.ToString(D2)); 

虽然我也不会将输出限制为1到99 ..使用D5至少。



如果你真的,真的想将文本版本存储在数据库中,那么你可以使用一个触发器( MySQL:INSERT INSERT触发器 [ ^ ])捕获id并生成文本版本



[OP评论后编辑]

尝试以下内容(再次注意,未经测试!)

 创建  TRIGGER  product_id 
AFTER INSERT
ON test FOR 每行
更新测试设置 textid = concat(< span class =code-string>' PC',LPAD(NEW.id, 5 ' 0'),' / POS' WHERE id = NEW.id;

说明:

触发器被称为 product_id ,它会在表格中插入一行,或者行插入表格 test

触发器中只有一个语句,所以我没有使用 BEGIN ... 结束,但您已经证明您知道如何在评论中使用您的代码 - 我只是在懒惰(并展示一个观点)。 (注意,如果你有多个语句,你需要在创建触发器时更改分隔符 - 请参阅文档)

触发器将转到 UPDATE test 使用 NEW.id 。特殊表 NEW 包含...新行。你无法更新它。还有另一个特殊的表 OLD ,其中也包含旧值。



对不起,我没有能够正确测试这个并且这不是进一步辅导你的正确位置,但至少现在你有一些搜索条件来帮助你进一步研究。



parts = startValue.Split(' / );
// 可以安全地假设第三个拆分字符串是您要查找的数字吗? / span>
int number;
if Int32 .TryParse(parts [ 2 ], out number))
{
number ++;
}
其他
{
如果数字不' t正确解析,将其设置为零
number = 0;
}
number =(number> max)? 1:数字;
返回string.Format(PC {0:D9} / POS,数字);
}


How can I create a line of code in c# that I will use to give every new product that I add to my product table?I am using MySQL database.The first two should be a letter followed by a number and back slash and then the word POS. for example PC01/POS.
This is what I have tried so far although it's not what I really need.

What I have tried:

//auto product number
static string IncrementID(string startValue, int numNonDigits)
{
    string nonDigits = startValue.Substring(0, numNonDigits);
    int len = startValue.Length - numNonDigits;
    int number = int.Parse(startValue.Substring(numNonDigits));
    number++;
    if (number >= Math.Pow(10, len)) number = 1; // start again at 1
    return String.Format("{0}{1:D" + len.ToString() + "}", nonDigits, number);
}
private void Genarate_productcode()
{
    product_code.Text = IncrementID("QIEpl/PO/0000009", 9); // produces QIEpl/PO/0000010 // C00011 PCI0001
}  

解决方案

In addition to solution 1 by CHill60[^] i'd strongly suggest to "create" product id with string prefix on MySQL server side. See: How to make MySQL table primary key auto increment with some prefix - Stack Overflow[^]


I personally would not generate the ID in the C# code I would use the MySQL database to generate the id for me: See MySQL :: MySQL 5.7 Reference Manual :: 3.6.9 Using AUTO_INCREMENT[^]

I certainly would never use a method that relies on me incrementing the "previous" number as it will not work in a multi-user environment.

Once you have that numeric Id you can display it however you want e.g. (Not tested!)

String.Format("PC{0}//POS", id.ToString(D2));

although I would also not limit my output to 1 to 99 .. use D5 at least.

If you really, really want to store the text version on the database then you could use a trigger (MySQL: AFTER INSERT Trigger[^]) to capture the id and generate the text version

[EDIT after OP comment]
Try something like the following (note again, untested!)

CREATE TRIGGER product_id
 AFTER INSERT
 ON test FOR EACH ROW
 	UPDATE test set textid = concat('PC', LPAD(NEW.id, 5, '0'), '/POS') WHERE id=NEW.id;

Explanation:
The trigger is called product_id and it fires after a row is, or rows are, inserted into the table test
There is only one statement in the trigger so I haven't used BEGIN ... END, but you have already demonstrated that you know how to do that with your code in the comments - I'm just being lazy (and demonstrating a point). (Note, if you do have more than one statement you will need to change the delimiter when creating your trigger - see the documentation)
The trigger is going to UPDATE the table test using NEW.id. The special table NEW contains ... the new rows. You can't update it. There is another special table OLD which contains the old values too.

I'm sorry I haven't been able to test this properly and this isn't the correct place to tutor you further, but at least now you have some search terms to help your further research.

[EDIT - further code samples after OP comments. This solution is hidden in the reams of comments below].
Both of these will work

DELIMITER //

CREATE TRIGGER product_id
 AFTER INSERT
 ON test 
 BEGIN
      String.Format("PC{0}//POS", id.ToString(D2));
 END; //
DELIMITER ;

or

CREATE TRIGGER product_id
 AFTER INSERT
 ON test 
      String.Format("PC{0}//POS", id.ToString(D2));


Specifically, if you want to include a BEGIN and END you MUST use DELIMITER as well!


That's a whacky question, but here ya go:

public static string IncrementID2(string startValue, int numNonDigits)
{
    int max = 999999999;
    string[] parts = startValue.Split('/');
    // is it safe to assume that the third splitted string is the number you're looking for?
    int number;
    if (Int32.TryParse(parts[2], out number))
    {
        number++;
    }
    else
    {
        if the number doesn't parse correctly, set it to zero
        number = 0;
    }
    number = (number > max) ? 1 : number;
    return string.Format("PC{0:D9}/POS", number);
}


这篇关于如何在C#中生成包含字母和数字的产品ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆