使用OleDb自动增加ID至Excel

时间:2018-09-08 02:09:32

标签: vb.net auto-increment

当我在excel文件中添加新数据时,我想自动递增ID。这段代码中有什么错误,错误显示为“查询表达式'MAX [ID]'中的语法错误(缺少运算符)'”

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)'error here
    cn.Open()
    Dim dr As OleDbDataReader = cm.ExecuteReader()
    If dr.HasRows Then
        dr.Read()
        Value = dr(0)
    Else
    End If
    dr.Close()

1 个答案:

答案 0 :(得分:-1)

MAX是一个函数,因此您需要在括号中添加参数:

SELECT MAX([ID]) FROM [Sheet1$]