如何在C#中生成自动增量数? [英] How to Generate Auto-Increment number in C# ?

查看:238
本文介绍了如何在C#中生成自动增量数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

stuID lastName firstName性别DOB电话

st001

st002





i需要生成ID号增量自动,但如何?我问题字符串st..

解决方案

这不是Java问题 - 对于真正的甚至没有对于C#。



你应该设置一个额外的密钥,stuID是一个值,而不是一个计数密钥。如果有人删除了数据集st001,你最终会弄得一团糟。


最好留下你学生证的''st''字母;没有必要使用字符串键而不是整数自动递增的字符串。



所以你只需要使你的stuID列为整数1,用< code lang =sql> IDENTITY property。通过这种方式,您的数据库系统将负责主键的创建和唯一性。


只需要一些计数器并使用任何循环递增,并将其与 st 连接起来,下面是我将字符串显示在一个标签中的示例。



  private   void  button1_Click( object  sender,EventArgs e)
{
string 行= ;
for int i = 1 ; i < = 5 ; i ++)
{
Row + = st + i.ToString( 000)+ System.Environment.NewLine;
}
label1.Text = Row;
}





所以结果如下所示,您可以使用此代码生成ID并根据需要使用< br $>


 st001 
st002
st003
st004
st005


stuID lastName firstName sex DOB Phone
st001
st002


i need to Generate ID number Increment Auto , but how ? i problem with String "st" ..

解决方案

This is no Java question - and for real not even one for C#.

You should set up an extra key, the stuID is a value, not a counting key. You would end up with a mess if someone deletes the dataset "st001".


It''s better to leave ''st'' letters for your Student ID ; there is no point to using a string key rather than an integer auto-incremented one.

So you just have to make your stuID column an integer one, with the IDENTITY property. This way your database system will take care of the creation and uniqueness of your primary keys.


just have some counter and increment with any loop and concatenate it with st, below is the sample in which I am displaying the string into one label.

private void button1_Click(object sender, EventArgs e)
        {
            string Row = "";
            for (int i = 1; i <= 5; i++)
            {
                Row += "st" + i.ToString("000") + System.Environment.NewLine;
            }
            label1.Text = Row;
        }



so result would look like as below and you can using this code to generate ids and use as you like

st001
st002
st003
st004
st005


这篇关于如何在C#中生成自动增量数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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