【问题标题】:Auto Increment Excel OleDb Connection Vb.net自动增量 Excel OleDb 连接 Vb.net
【发布时间】:2019-02-13 08:44:40
【问题描述】:

当我将新数据添加到 excel 时,我想自动增加 ID 并且此代码不会自动增加,它只会增加 2,我不明白为什么请帮助p 谢谢

Dim Value As Integer
    cn = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + "C:\\Users\\Barbatos\\Desktop\\Book3.xlsx " + ";Extended Properties=Excel 12.0;")
    cm = New OleDbCommand("SELECT MAX ([ID]) FROM [Sheet1$]", cn)
    cn.Open()
    Dim dr As OleDbDataReader = cm.ExecuteReader()
    If dr.HasRows Then
        dr.Read()
        Value = dr(0)
    Else
    End If
    dr.Close()
    Dim str As String
    Dim empid As Integer
    Dim newNumber As Integer
    str = "SELECT MAX([ID]) AS MAXIMUM FROM [Sheet1$]"
    Dim cmd2 As OleDbCommand = New OleDbCommand(str, cn)
    'Dim dr As OleDbDataReader
    dr = cmd2.ExecuteReader
    If dr.HasRows Then
        While dr.Read()
            If empid = IsDBNull(dr("MAXIMUM")) Then
                newNumber = CInt(Val(empid)) + 1
            End If
            If newNumber = 0 Then
                newNumber = 1
                empid = CStr(newNumber)
            ElseIf newNumber = 1 Then
                newNumber = newNumber + 1
                empid = CStr(newNumber)
            Else
                newNumber = newNumber + 1
                empid = CStr(newNumber)
            End If
        End While
    End If
    dr.Close()
    Me.Label2.Text = empid

【问题讨论】:

  • 如果您只是尝试检索单个值,那么您不应该调用ExecuteReaderExecuteScalar 方法专门用于检索单个值。
  • 我不能 100% 确定 Excel 是否支持它,但我希望如此,所以您也可以在 SQL 中使用 ISNULL 以保证返回一个数字:如果ID 列中没有值,ISNULL(MAX(ID), 0) 将返回 0。这样,您的 VB 代码就不需要额外检查了。
  • 为什么要连接 String 文字来创建连接字符串?连接两个文字从来没有任何意义。如果您确实连接任何东西,请使用连接运算符 (&) 而不是加法运算符 (+)。如果要从变量构造连接字符串,请使用String.Format、字符串插值或连接字符串生成器。

标签: vb.net auto-increment


【解决方案1】:

解决了

If dr.HasRows Then
        dr.Read()
        If IsDBNull(dr("MAXIMUM")) Then
            empid = 1
        Else
            empid = CInt(dr("MAXIMUM")) + 1
        End If
    Else
        empid = 1
    End If

【讨论】:

  • 使用 IsDBNull 函数并不是特别错误,但我通常建议不要使用 VB 运行时函数。数据阅读器有自己的IsDBNull 方法,或者你可以直接比较DBNull.Value
【解决方案2】:

总结一下我的 cmets,我会这样做:

Dim nextId As Integer

Using connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Barbatos\\Desktop\\Book3.xlsx;Extended Properties=Excel 12.0;"),
      command As New OleDbCommand("SELECT MAX([ID]) FROM [Sheet1$]", connection)
    connection.Open()

    Dim currentId = command.ExecuteScalar()

    nextId = If(currentId Is DBNull.Value, 1, CInt(currentId) + 1)
End Using

或者,如果支持,则:

Dim nextId As Integer

Using connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Barbatos\\Desktop\\Book3.xlsx;Extended Properties=Excel 12.0;"),
      command As New OleDbCommand("SELECT ISNULL(MAX([ID]), 0) FROM [Sheet1$]", connection)
    connection.Open()

    nextId = CInt(command.ExecuteScalar()) + 1
End Using

【讨论】:

    猜你喜欢
    • 2019-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 1970-01-01
    相关资源
    最近更新 更多