如何制作自动生成的id功能?

时间:2014-04-26 17:20:34

标签: vb.net function module

我尝试创建一个在sql表中生成id的函数。但我不知道如何设置选择表和列的参数请帮助我

Public Function autoid(ByVal col As String, ByVal txt As TextBox, ByVal tb As String)
    Dim cmd As New SqlCommand
    Dim id As Integer
    cn.Open()
    cmd.Connection = cn
    cmd.CommandText = " SELECT MAX('" & col & "') FROM '" & tb & "' "

    Try
        If IsDBNull(cmd.ExecuteScalar) Then

            id = 1
            txt.Text = id
        Else
            id = cmd.ExecuteScalar + 1
            txt.Text = id

        End If
        cmd.Dispose()
        cn.Close()
    Catch ex As Exception

        MsgBox(ex.ToString)

    End Try

它显示此错误Incorect syntax near ..my tb value ..

1 个答案:

答案 0 :(得分:0)

下载mssql管理工作室在您的字段上附加您的数据库本地设置自动增量,这需要唯一的ID设置它不是可以为空的字段,并且所有mssql 2012为您做其余的。