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 UIHere is an example of how you could achieve your automatically generated numberscreate 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'),它提供1Esome other data12Esome other data23Csome other data34Csome other data4但查询SELECT EmployeeType + RIGHT(REPLICATE('0', 3) + CAST(AutoId as varchar), 3), OtherDatafrom demo将产生E001some other data1E002some other data2C003some other data3C004some 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 EmployeeNumbersSELECT 'E', num, 0 FROM q UNIONSELECT '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 ......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-03 06:52