自动生成ID(将数据插入数据库) [英] AutoGenerate Id ( Insert data into database)

查看:128
本文介绍了自动生成ID(将数据插入数据库)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用3层体系结构将数据插入数据库..
但是我的问题是如何自动生成prod_id而不输入文本框或标签.
页面加载时会自动更改.
请不确定我正在使用哪个数据库,但是所有rdbms数据库都具有colum-Audo功能.

像在Sql Server中一样,您可以使用int并将身份设置为true
甚至可以将数据类型声明为uniqueidentifier,并且在插入时使用NewId()生成唯一标识符.

在sql或存储过程中,返回maxid()值以使用自动@@ Identity获取ID.

如果要手动执行此操作,请使用max()函数获取最后一个ID,并将该数字加1,然后将其发送到数据库.但是请检查并发问题.

希望这会有所帮助.

欢呼


创建一个存储过程并使用此代码插入.it自动插入prod_id



<pre lang="SQL">

创建过程sp_insert_products
声明@prod_id nvarchar



设置@prod_id =(从产品中选择isull((max(cast(prod_id as int))),0)+ 1)
开始
插入tbl_products
值(您的值在这里)
结束


I am using 3 Tier Architecture for insert the data into the database..
but my problem how to generate prod_id automatic without entering into textbox or label.
when page loads,it automatic change..
plz reply

解决方案

I am not sure which database you are working on but all rdbms database has colum - Audo feature.

Like in Sql server, you can use int and set identity to true
Even you can declare the datatype as uniqueidentifier and while you insert use NewId() to generate unique identifier.

In the sql or stored procedure return the maxid() value to get the id ir use @@Identity for Auto.

If you want to do it manually, get the last id using max() function and increment the number by one and send it to database. But check the concurrent issue.

Hope this helps.

cheers


Create a store procedure and use this code to insert .it insert prod_id automatically



<pre lang="SQL">

create procedure sp_insert_products
declare @prod_id nvarchar

as

set @prod_id = ( select isnull( (max(cast( prod_id as int ))),0) + 1 from products )
begin
insert into tbl_products
values (your values goes here)
end


这篇关于自动生成ID(将数据插入数据库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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