在asp.net 中,想返回刚插入数据的编号,怎样获得?(插入之后取最大Id 显然是不可取的,因为多人操作时 ,id 肯定 不能保证是正确的)
id是 自增的
很多人都说 insert intto .... ; select @@IDENTITY 用这个
可是用它 ExecuteNonQuery_ 执行是 返回影响的行数...
StringBuilder strSql = new StringBuilder();
strSql.Append("insert into PROJ_MEMBER(");
strSql.Append("PROJ_SERI_ID,PROJ_NAME,POST_ID,POST_NAME,USER_ID,DEPT_ID,DEPT_NAME,MEMBER_FUNC,TYPE_ID,TYPE_NAME,TELL,FAX,MOBILE,EMAIL,WORK_TIME,REMARK,CREATE_USER_ID)");
strSql.Append(" values (");
strSql.Append("@PROJ_SERI_ID,@PROJ_NAME,@POST_ID,@POST_NAME,@USER_ID,@DEPT_ID,@DEPT_NAME,@MEMBER_FUNC,@TYPE_ID,@TYPE_NAME,@TELL,@FAX,@MOBILE,@EMAIL,@WORK_TIME,@REMARK,@CREATE_USER_ID)");
strSql.Append(";select @@IDENTITY");
new SqlHelper().ExecuteNonQuery_(strSql.ToString(), Proj_Member(""));
谁知道麻烦写个小例子,谢谢了
用参数调用
SQL语句:
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[JobTaskInsert]') AND type='P')
DROP PROCEDURE [dbo].[JobTaskInsert]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create PROCEDURE [dbo].[Jrcrm_JobTaskInsert]
(
@JobTitle nvarchar(200),
@JobSubTitle nvarchar(1000),
@AddTime datetime,
@AddUser nvarchar(50),
@ModTime datetime,
@ModUser nvarchar(50),
@ReTurnValue int OUTPUT
)
AS
BEGIN TRAN
Insert
[JobTask]
(
[JobTitle],
[JobSubTitle],
[AddTime],
[AddUser],
[ModTime],
[ModUser],
)
Values
(
@JobTitle,
@JobSubTitle,
@AddTime,
@AddUser,
@ModTime,
@ModUser,
)
set @ReTurnValue=SCOPE_IDENTITY()
IF @@ERROR!=0
BEGIN
set @ReTurnValue=-1
ROLLBACK
END
ELSE
BEGIN
COMMIT
END
GO
#region JobTask
internal static int JobTaskInsert(JobTask m_JobTask)
{
SqlParameter[] p=
{
SqlHelper.MakeInParam("@JobTitle", SqlDbType.NVarChar, 200, SqlHelper.CheckForNullString(m_JobTask.JobTitle)),
SqlHelper.MakeInParam("@JobSubTitle", SqlDbType.NVarChar, 1000, SqlHelper.CheckForNullString(m_JobTask.JobSubTitle)),
SqlHelper.MakeInParam("@AddTime", SqlDbType.DateTime, 8, SqlHelper.CheckForNullDateTime(m_JobTask.AddTime)),
SqlHelper.MakeInParam("@AddUser", SqlDbType.NVarChar, 50, SqlHelper.CheckForNullString(m_JobTask.AddUser)),
SqlHelper.MakeInParam("@ModTime", SqlDbType.DateTime, 8, SqlHelper.CheckForNullDateTime(m_JobTask.ModTime)),
SqlHelper.MakeInParam("@ModUser", SqlDbType.NVarChar, 50, SqlHelper.CheckForNullString(m_JobTask.ModUser)),
ReTurnValue
};
NonQueryBool("Jrcrm_JobTaskInsert",p);
return Convert.ToInt32(p[p.Length-1].Value);
}
SQL helper可以在网上搜索一下,是一个封装好的类库