id生成器代码问题 [英] id generator code problem

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

问题描述

你做编码器..我面临着我的id号码生成器代码的问题

i我正在构建数据库项目,我想让id自动生成它我自己做了但不是我想要的它工作...我希望它从001开始增加每次+1喜欢这个002,003直到它达到100等等

它现在如何工作就像这从1开始然后2 ,3,4,5,6 ......这是代码。

谢谢。

hw u doing coders .. am facing problem with my code of id number generator
i am building database project and i want to make the id generates it self automaticaly i did it but not how i want it to work ...i want it to Start from 001 and increases every time +1 like this 002,003 untill it reach 100 and so on
how it works now is like this starts from 1 and then 2,3,4,5,6......here is the code.
thank you.

Sub GenerateSerialNo()
        Dim odr As SqlDataReader
        Dim sql As String
        sql = "select max(bookid)as SNo from tbremovedbooks"
        Try
            conn.Open()
            'Application.DoEvents()
            cmd = New SqlCommand(sql, conn)
            odr = cmd.ExecuteReader
            If odr.Read() Then
                iNumOfRecords = DS.Tables("tbremovedbooks").Rows.Count + 1
                'Displaying 1 if there is no data in the table
                If iNumOfRecords = 1 Then
                    txtbookid.Text = "1"
                Else
                    'Ordering the data Serial if more than one record is in the table
                    txtbookid.Text = odr("SNo") + 1
                End If
            End If
            odr.Close()
            cmd.Connection.Close()
            conn.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

推荐答案

为什么你不能把你的ID保存为整数,只需更改格式当你显示它时的数字?



基本上你没有什么可以赢得varchar主键。



因此,在数据库中,主键为int(1,2,3,4,...)。



当你想显示ID时:
Why on hell don''t you keep your ID as integer, and just change the format of the number when you are displaying it ?

Basically you have nothing to win to have a varchar primary key.

So, in the database, primary key as int (1, 2, 3, 4, ...).

When you want to display ID :
string formattedId = id.ToString("000");





简单有效。



Simple, and effective.


这篇关于id生成器代码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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