我无法修复自动ID生成器

时间:2017-01-29 07:24:05

标签: vb.net

我能够在表单加载时成功生成id,但是当我离开该表单并尝试再次加载时,生成的id与前一个相同

Try
        Dim num As Integer
        conn.ConnectionString = String.Format("server=localhost;user id=root;password=root;database=rishab")
        conn.Open()
        sqlcommand = New MySqlCommand("select max(ID) from inc", conn)
        sqlcommand.ExecuteNonQuery()

        If IsDBNull(sqlcommand.ExecuteScalar) Then
            num = 1
            id5.Text = num
        Else
            num = sqlcommand.ExecuteScalar + 1
            id5.Text = num
        End If
        sqlcommand.Dispose()
        conn.Close()
        conn.Dispose()

    Catch ex As Exception

    End Try


Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    autogenerate()

End Sub

1 个答案:

答案 0 :(得分:-1)

如果我理解正确,您需要将num Integer声明为public。然后它将保留最新值,当您重新加载表单时,它将从最新值开始计算。在表单类开始之前执行此操作,或者我将在包含所有其他公共字符串,布尔值和整数的模块中个人添加此内容。

''In a module add -
Public num As Integer 
''Do not add a value here as in Public num as Integer = 0 as it will give the value 0 every time you make a call to it.
''Also remember to handle the value throughout your app, change back to 0 or leave it to the last value etc.