用于根据主键生成数字的代码 [英] code for generating numbers according to primary key

查看:79
本文介绍了用于根据主键生成数字的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我想生成与主键相对应的自动编号

喜欢

如果pk为1,则否为000001

如果pk是10:000010

如果100:000100

数字中的总位数应该相同..如何在c#中对其进行编码?


在此先感谢

Hi all

I want to generate auto number corresponding to primary keys

like

if pk is 1 the no will be 000001

if pk is 10 :000010

if 100: 000100

the total digits in a number should be same..how to code for it in c#?


Thanks in advance

推荐答案

您需要做的就是向ToString调用中添加格式信息:
All you need to do is add formatting information to your ToString call:
int i = 100;
Console.WriteLine(i.ToString("D6"));


另外,您也可以使用String.Format:


Alternatively, you could do it with String.Format:

int i = 100;
Console.WriteLine(string.Format("{0:D6}", i));


您正在使用哪个数据库?您可能想检查是否可以直接在数据库中执行类似的操作.
Which database are you using? You might want to check if you can do something similar in the database directly.


某些rdbms引擎(例如MS SQL)使用序列 [
Some rdbms engines like MS SQL use IDENTITY[^]

While others use sequences[^]

It''s usually a good idea to let the rdbms generate the key for you to avoid concurrency issues because many users access and change data in a relational database, the database manager must be able both to allow users to make these changes and to ensure that data integrity is preserved.

Concurrency refers to the sharing of resources by multiple interactive users or application programs at the same time.

Regards
Espen Harlinn


这篇关于用于根据主键生成数字的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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