如何使自动生成的id功能? [英] How to make auto generated id function?

查看:92
本文介绍了如何使自动生成的id功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 公共函数autoid(ByVal col As 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& '

尝试
如果IsDBNull(cmd.ExecuteScalar)然后

id = 1
txt.Text = id
否则
id = cmd.ExecuteScalar + 1
txt.Text = id

End If
cmd.Dispose()
cn.Close()
Catch ex例外

MsgBox(ex.ToString)

结束尝试

它显示这个错误 incorect syntax near ..my tb value ..

解决方案

下载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.

这篇关于如何使自动生成的id功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆