自动编号代码:
Dim cmd1 As New SQLCommand
Dim cmd2 As New SQLCommand
Dim Key As Integer
Dim nm As String = e.DataTable.name
cmd1.ConnectionName =
cmd2.ConnectionName =
cmd1.commandText = "Select Count(*) From [编号] Where 表名 = '" & nm & "'"
If cmd1.ExecuteScalar = 0 Then '如果编号表不存在前缀的行,那么增加一行
cmd1.commandtext = "Insert Into 编号 (表名,序号) Values('" & nm & "',1)"
cmd1.ExecuteNonQuery
End If
cmd1.commandText = "Select [序号] From [编号] Where 表名 = '" & nm & "'"
Do
Key = cmd1.ExecuteScalar() '从后台获得顺序号
cmd2.commandText = "Update [编号] Set [序号] = " & (Key + 1) & " Where [序号] = " & Key & " And 表名 = '" & nm & "'"
If cmd2.ExecuteNonQuery() > 0 Then '更新顺序号
Exit Do '更新成功则退出循环
End If
Loop
但是出现过两回: 【编号表】重新增加了本来已经存在的表名的新行 ,也就是说 If cmd1.ExecuteScalar = 0 返回来0了。。。