我想在c#中自动生成数字。例如。 E001,E002 ...... C001,C002 ...... [英] I want auto generation of numbers in c#. Eg. E001,E002......C001,C002......

查看:78
本文介绍了我想在c#中自动生成数字。例如。 E001,E002 ...... C001,C002 ......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C#中自动生成数字。实际上我有E和C的EmpCode,其次是3digit no。我有一个下拉框用于选择E或C.one文本框中我想要的数字选择值

(自动生成).Eg.E001,E002 .....
C001,C002 ........

此代码的SQL命令...

I want auto generation of numbers in C#. Actually i have EmpCode with E and C followed by 3digit no. I have One Drop down box is for selecting E or C.one Text box for number in which i want Select Value
(automatically generated).Eg.E001,E002.....
C001,C002........
And SQL Command for this code...

推荐答案

好吧,你的第一个要求是微不足道的:在用户选择上,将所选字母与循环中生成的数字的字符串表示连接起来,并使用生成的字符串填充另一个下拉菜单。

和这段代码的SQL命令对我来说毫无意义。
Well, your first requirements is trivial: on user selection, concatenate the selected letter with the string representation of numbers generated in a loop and use the resulting strings for populating another drop down.
"And SQL Command for this code" requirement makes no sense to me.


CodeProject中有很多例子,所以我鼓励你在这里做进一步的搜索其他想法,但是...



不要将格式化的EmpCode存储在您的数据库中。



为什么?员工C007从E型员工转变为C型员工?你需要将他们的ID从E100更改为......无论下一个数字是C类型。您不能假设您可以为同一名员工使用C100。



现在让自己进入现实世界。员工John,员工目前是员工E099,但将工作转移到C ......世界。没问题,除非您必须将他的员工编号更改为C100,因为已经采用了C099!约翰现在很困惑 - 他的员工人数是099还是100?老工资单怎么样?工资单也必须改变,以及任何其他引用约翰的数据。



另一个原因。



贵公司非常成功,员工众多。最后,Janet成为C组的第1000名员工。您将EmpId分配给C1000的Janet。嗯......它不适合,或者更糟的是存储为C100 - 现在谁C100指的是......珍妮特或约翰?



好吧,你想,我只会给EmpIds写一个字母后跟四个数字。很棒,但是你必须更新你已经拥有的999条数据库并让大家知道 - 所以珍妮特和约翰再次感到困惑!



我可以继续,但这就够了。



总结 - 如果要进行格式化,请在用户界面中进行



以下是如何实现自动生成数字的示例

There are many examples of this here in CodeProject so I do encourage you to do some further searching here to get other ideas, however ...

Don't store the formatted EmpCode on your database.

Why? Say Employee C007 moves from being a 'E' type of employee to an 'C' type of employee? You will need to change their ID from "E100" to ... whatever the next number is for 'C'-types. You can't assume that you can use 'C100' for the same employee.

Now put yourself into the real world. A Person, Employee John, is currently employee "E099" but moves jobs into the "C..." world. No problem, except you have had to change his Employee number to "C100" because "C099" is already taken! John is now confused - is his Employee Number 099 or 100? What about old payslips? Payroll has to change too, and any other data that refers to John the person.

Another reason.

Your company is very successful with lots of employees. Finally it takes on Janet as the 1000th employee in the "C" group. You allocate an EmpId to Janet of "C1000". Hm... it doesn't fit, or worse gets stored as "C100" - Now who does "C100" refer to ... Janet or John?

Ok you think, I'll just make EmpIds a letter followed by four digits. Great, but you have to go update those 999 records you already have on the database and let everybody know - so Janet and John get confused all over again!

I could go on but that's enough.

In summary - If there is formatting to be done, do it in the UI

Here is an example of how you could achieve your automatically generated numbers
create table demo
(
	AutoId int identity(1,1),
	EmployeeType char(1),
	OtherData varchar(max)
)

insert into demo values
('E','some other data1'),
('E','some other data2'),
('C','some other data3'),
('C','some other data4')

,它提供

1	E	some other data1
2	E	some other data2
3	C	some other data3
4	C	some other data4

但查询

SELECT EmployeeType + RIGHT(REPLICATE('0', 3) + CAST(AutoId as varchar), 3), OtherData
from demo

将产生

E001	some other data1
E002	some other data2
C003	some other data3
C004	some other data4

如果绝对必须有C001 - 999 E001 - 999然后我建议您预先分配数字,并在使用后更新每个候选号码。例如。生成数字

If you absolutely must have C001 - 999 and E001 - 999 then I suggest you pre-allocate the numbers and just update each candidate number once it has been used. E.g. to generate the numbers

CREATE TABLE EmployeeNumbers
(
	id int identity(1,1),
	EmployeeType char(1),
	EmployeeNo int,
	Used int
)
;WITH q AS
(
    SELECT  1 AS num
    UNION ALL
    SELECT  num + 1 FROM q WHERE num < 999
)
insert into EmployeeNumbers
SELECT 'E', num, 0 FROM q 
UNION
SELECT 'C', num, 0 FROM q option( maxrecursion 999)



如果使用这种方法并且允许多个用户试图立即访问该表。


Take care if using this approach and allow for more than one user trying to access the table at once.


这篇关于我想在c#中自动生成数字。例如。 E001,E002 ...... C001,C002 ......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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