日期:2014-05-19  浏览次数:20781 次

我想让一条sql记录添加的时候id能自增1
string   time1   =   DateTime.Now.ToString( "yyyy-MM-dd   ");
string   time2   =   DateTime.Now.AddMonths(2).ToString( "yyyy-MM-dd   ");
string   cmdtext1   =   "declare   @i   as   int   ";
cmdtext1   +=   "select   set   @i=max(id)+1   from   lendrtnrec   ";
cmdtext1   +=   "insert   into   lendrtnrec(id,userid,bookid,lendtime,bertntime)values(@i, "+   readerid   +   " ', ' "   +   bookid   +   " ', ' "   +   time1   +   " ', ' "   +   time2   +   " ') ";
如上有错误,请大家帮忙看下,错在哪里?麻烦给详细代码,新手上路,多多帮助!

------解决方案--------------------
CREATE TABLE [dbo].[LaborReport](
[LaborReportID] [int] IDENTITY(1,1) NOT NULL,
.....
------解决方案--------------------
楼上正解,利用自增列,方便又不用你操心。
------解决方案--------------------
最好把这块需求封装在存储过程中

select set @i=max(id)+1 from lendrtnrec
========================================
select @i= (max(id)+1) from lendrtnrec
------解决方案--------------------
在数据库里用企业管理器把ID设成自增长关键码
------解决方案--------------------
打开表的设计,把数据类型设置为int
"标识 "选择 "是 ", "标识种子 "为1, "标识递增量 "设置为1.OK
------解决方案--------------------
cmdtext1 += "insert into lendrtnrec(id,userid,bookid,lendtime,bertntime)values(@i, "+ readerid + " ', ' " + bookid + " ', ' " + time1 + " ', ' " + time2 + " ') ";
少了一个 '改为
cmdtext1 += "insert into lendrtnrec(id,userid,bookid,lendtime,bertntime)values(@i, ' "+ readerid + " ', ' " + bookid + " ', ' " + time1 + " ', ' " + time2 + " ') ";