且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使自动生成的id功能?

更新时间:2022-10-14 23:26:29

下载mssql管理工作室在本地安装你的数据库在你的字段上设置autorincrement需要唯一的ID设置它是不可空字段,这就是所有mssql 2012为你休息。 b $ b

I try to create a function for generate the id in sql table. but i don't know how to set parameter for select table and columns please help me

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

it show this error Incorect syntax near ..my tb value ..

Download mssql management studio attach your database locally setup autorincrement on your field which require unique id set it be not nullable field and that's all mssql 2012 make al the rest for you.