简体   繁体   English

VB.NET中的autoNumber

[英]autoNumber in VB.NET

i want to migrate from VB6 to VB.NET, but still rookie. 我想从VB6迁移到VB.NET,但仍然是菜鸟。 how to make autoNumber procedure works like code in VB6 in VB.NET (VB2012), below my code so far. 到目前为止,如何使autoNumber过程变得类似于VB.NET(VB2012)中VB6中的代码。

below my previous code in VB6, this code will work when addbutton_click, it will display CDEUSR005, since there are 4 record in table before, CDEUSR001, CDEUSR002, CDEUSR003, CDEUSR004. 在我之前在VB6中的代码下面,此代码将在addbutton_click时起作用,它将显示CDEUSR005,因为之前的表中有4条记录,分别为CDEUSR001,CDEUSR002,CDEUSR003,CDEUSR004。 Image sample that works in VB6 在VB6中工作的图像样本

Sub autoNumber()
    Rs_User.Requery
    Set Rs_User = New ADODB.Recordset
    strSQL = "SELECT userCode FROM tblUser ORDER BY userCode"
    Rs_User.Open strSQL, Conn, adOpenDynamic, adLockBatchOptimistic
    If Rs_User.BOF Then
        NewCode = "CDEUSR001"
        Exit Sub
    Else
        Rs_User.MoveLast
        NewCode = Rs_User!userCode
        userCode = Right(userCode, 3)
        userCode = Val(userCode) + 1
        If Len(userCode) > 5 Then
            Exit Sub
        End If
    End If
    userCode = "CDEUSR" & Format(NewCode, "000")
 End Sub

this is my VB.NET(VB2012) code, when the add button, it will show CDEUSER001, not display CDEUSR005. 这是我的VB.NET(VB2012)代码,当添加按钮时,它将显示CDEUSER001,而不显示CDEUSR005。 Image sample that wont' works in VB.NET 在VB.NET中无法正常工作的图像示例

Sub autoNumber()
    sql = New Odbc.OdbcCommand("SELECT userCode FROM tblUser ORDER BY userCode", conn)
    dr = sql.ExecuteReader
    dr.Read()

    If (xxxx which has same function with Rs_User.BOF) Then
        NewCode = "CDEUSR001"
        Exit Sub
    Else
        NewCode = Microsoft.VisualBasic.Right(dr.GetString(0), 3)
        NewCode = Val(NewCode) + 1
        If Len(NewCode) > 5 Then
            Exit Sub
        End If
    End If
    NewCode = "CDEUSR" & Format(NewCode, "000")
End Sub

The difference between ADODB in VB6 and OdbcConnection in VB.NET is that OdbcConnection often uses OdbcDataReader.HasRows property as ADODB.Recordset.BOF or ADODB.Recordset.EOF counterpart. 在VB6和ADODB之间的差OdbcConnection在VB.NET是OdbcConnection经常使用OdbcDataReader.HasRows属性作为ADODB.Recordset.BOFADODB.Recordset.EOF对应物。

Your VB.NET code should be modified as this ( Using block added as common convention when initializing OdbcConnection ): 您应该这样修改您的VB.NET代码(在初始化OdbcConnection时, Using作为常规约定添加的代码块):

Sub autoNumber()
    Using conn As New OdbcConnection(...)
        Using sql As New OdbcCommand("SELECT userCode FROM tblUser ORDER BY userCode", conn)
           Using dr As OdbcDataReader = sql.ExecuteReader()

               ' similar like Recordset.EOF or BOF
               If dr.HasRows = False Then 
                   NewCode = "CDEUSR001"
                   Exit Sub
               Else
                   dr.Read()
                   ' dr("userCode") may also work here
                   NewCode = Right(dr.GetString(0), 3)
                   NewCode = Val(NewCode) + 1
                   If Len(NewCode) > 5 Then
                      Exit Sub
                   End If
               End If
            NewCode = "CDEUSR" & Format(NewCode, "000")
            End Using
        End Using
    End Using
End Sub

Note: Unlike ADODB.Recordset , OdbcDataReader doesn't have row indexing to determine last row, but DataSet does. 注意:与ADODB.Recordset不同, OdbcDataReader没有行索引来确定最后一行,但DataSet却没有。 You can retrieve the last record in DataSet as given below: 您可以按如下所示检索DataSet的最后一条记录:

Dim ds As DataSet = New DataSet()

Using conn As New OdbcConnection(...)
   Using sql As New OdbcCommand("SELECT userCode FROM tblUser ORDER BY userCode", conn)
       Dim adapter As OdbcDataAdapter = New OdbcDataAdapter(sql)
       adapter.Fill(ds)

       ' here to get last row from DataSet
       ' related: stackoverflow.com/questions/2099379/
       NewCode = Right(ds.Tables(0)(ds.Tables(0).Rows.Count - 1)("userCode"), 3)

       ' other stuff

   End Using
End Using

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM