create a storeprocedure like
create procedure AddRecord
(
   param datatype
   ....
)
as
    insert into table_name(..) values(@....)
return @@identitythen use SqlCommand.Parameter get the return value, look at the following:
SqlCommand cmd = new SqlCommand(...);
SqlParameter p = cmd.Parameters.Add("ReturnValue", SqlDbType.Int);
p.Direction = ParameterDirection.ReturnValue;
....
//Get then value
int iRes = (int)cmd.Parameters("ReturnValue").Value;

解决方案 »

  1.   

    select top 1 id from table_name order by id desc
      

  2.   

    select top 1 id from table_name order by id desc
    該方法可以
      

  3.   

    return @@identity==================================
    弯弯的月亮小小的船,小小的船,两头尖,我在小小的船里坐,只看见闪闪的
    星星蓝蓝的天.
      

  4.   

    lesstif() 的方法可以,
    select最大值是不行的,如果以前加的数据被删掉,id最大数不会改变的,新插入的行id跟你去最大值加1不一样
      

  5.   

    sqlString = "INSERT INTO 表名(字段名1,字段名2,------)"
     sqlString &= " VALUES (值1,值2---------)"
     sqlString &= ";SELECT " 自增字段名 " FROM "表名
     sqlString &= " WHERE (" 自增字段名 "= @@IDENTITY)"
     mySqlCommand.CommandText = sqlString
     mySqlCommand.Connection = myConn.dbConn
     mySqlCommand.Connection.Open()
     lngNewRecordID = mySqlCommand.ExecuteScalar
     这时lngNewRecordID就是新插入的值