自动生成类别ID不起作用

时间:2011-12-08 02:52:47

标签: asp.net ado.net

我是使用aspnet的新手,我有关于自动生成id的问题,任何人都可以判断我的代码是否存在问题...要求是每当用户点击添加lbl_categorycode.Text应该自动生成。提前感谢:D

Imports System.Data.SqlClient
Imports System.Data

Partial Class ADDCATEGORY
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim cmd As New SqlCommand
        cmd.CommandText = "INSERT INTO CategoryTable (CategoryID, ProductCategory) VALUES ( '" & lbl_categorycode.Text & "' , '" & txt_productcategory.Text & "')"
        cmd.Connection = cn
        cmd.Connection.Open()
        cmd.ExecuteNonQuery()
        cmd.Connection.Close()
        MsgBox("Record Added")
        Call gencategorycode()
        Call clear()

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Call gencategorycode()
    End Sub
    Private Sub gencategorycode()
    Dim cmd1 As New SqlCommand
    Dim rdr As SqlDataReader

    cmd1.Connection = cn
    cmd1.Connection.Open()
    cmd1.CommandText = "Select count(*) as s from CategoryTable "
    rdr = cmd1.ExecuteReader

    If rdr.HasRows = True Then
        rdr.Read()
        lbl_categorycode.Text = Format(CInt(rdr(0).ToString) + 1, "ACECATN000000")

    Else
        lbl_categorycode.Text = "ACECATN0000001"
    End If
    cmd1.Connection.Close()
End Sub
Sub clear()
    txt_productcategory.Text = ""
End Sub

结束班

2 个答案:

答案 0 :(得分:0)

您不能假设rowCount+1将成为下一个Id.let,假设您在数据库中有4条记录,其中包含ID 1,2,3,4。然后说你删除id为2的记录。然后你的计数等于3.所以根据你的算法,下一个记录id将是4.由于数据库中已经存在id为4的记录,因此不能继续工作。

所以你可以做的是,让数据库生成标识列。如果您使用的是SQL Server数据库,则可以为列启用Identity规范。您可以使用SQL命令SCOPE_IDENTITY(http://msdn.microsoft.com/en-us/library/ms190315.aspx)插入最后一个标识值。

答案 1 :(得分:0)

您想将此ACECATN000000复制到ACECATN000001中吗?

Imports System.Data
Imports System.Data.SqlClient

Partial Class _Default
    Inherits System.Web.UI.Page
    Dim con As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename='|DataDirectory|example.MDF';Integrated Security=True;Connect Timeout=30;User Instance=True")
    Dim cmd As New SqlCommand

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim selected As String = Left(DropDownList1.SelectedItem.ToString, 1)
        Dim maximumfinaldata As String = ""
        cmd = New SqlCommand("SELECT MAX(CategoryID) FROM CategoryTable WHERE (SUBSTRING(CategoryID,1,1)) ='" & Left(DropDownList1.SelectedItem.ToString, 1) & "'", con)
        con.Open()
        Dim result As SqlDataReader = cmd.ExecuteReader
        While result.Read
            ''it will reproduce C002 AS A MAX VALUE because the maxdatavalue from mycode was 002
            Dim temp = result.GetValue(0)
            Dim number = CInt(Right(result.GetValue(0), 3))
            If number > 0 And number < 9 Then
                number += 1
                maximumfinaldata = "00" & CStr(number)
            ElseIf number > 9 And number < 99 Then
                number += 1
                maximumfinaldata = "0" & CStr(number)
            ElseIf number > 99 Then
                number += 1
                maximumfinaldata = CStr(number)
            End If
        End While
        MsgBox(maximumfinaldata)
        con.Close()
    End Sub
End Class

看看