如何在新行开始编号并插入新的“Id”?

时间:2017-06-15 09:01:50

标签: sql-server vb.net

*我有代码自动编号*

Sub AutoCount()
        Dim daC As New SqlDataAdapter("select Max(couttime) From TbCourse where cusid='" & txtcustomerid.Text & "' ", conn)
        Dim dsC As New DataSet
        daC.Fill(dsC, "Course")
        dsC.Tables(0).Clear()
        daC.Fill(dsC, "Course")
        txtcounttime.Text = Format(CInt(dsC.Tables(0).Rows(0).Item(0)) + 1, "0")
    End Sub

我有Table Ex:

enter image description here

1 个答案:

答案 0 :(得分:0)

You need to check, is there any entry for the given cusid, if not it will return 1 (0 in case if you are +1 in the code), if the given cusid exists, it will return the MAX value for the cusid

IF NOT EXISTS (SELECT cusid FROM TbCourse WHERE cusid = 'COL2')
    SELECT 1 AS couttime
ELSE
    SELECT MAX(couttime) AS couttime FROM TbCourse WHERE cusid = 'COL2'

Here COL2 is the txtcustomerid.Text value.