http://community.csdn.net/Expert/topic/3266/3266815.xml?temp=.604046

解决方案 »

  1.   

    Sqlserver的话
    再执行
    select @@Identity
      

  2.   

    如果是多个人同时对数据库进行操作的话那就会有问题啊,在你返回最大值的时候时候也许别的人也对这个数据库进行了操作,那返回的ID值就有可能不是我刚才插入的数据的那个ID值了
      

  3.   

    create proc  ....@id int output...select @id=@@Identity
      

  4.   

    #region//执行不返回结果的sql语句,返回@@IDENTITY
    public string ExecSQLNonQuery(string SQLStr,out int iRow)
    {
    iRow = 0;
    try
    {
    aConnection.Open();
    }
    catch(SqlException e)
    {
    return e.Message;
    }
    try
    {
    aCommand=new SqlCommand(SQLStr,aConnection);
    //定义事务
    SqlTransaction PubsTransaction =aConnection.BeginTransaction();
    aCommand.Transaction=PubsTransaction;
    try
    {
    aCommand.ExecuteNonQuery(); //如果解析不成功
    PubsTransaction.Commit();
    SqlCommand GetIDCmd = new SqlCommand(" select @@IDENTITY",aConnection);
    iRow =  int.Parse(((Decimal)GetIDCmd.ExecuteScalar()).ToString());
    return "OK";
    }
    catch(SqlException e)
    {
    try
    {
    PubsTransaction.Rollback();
    }
    catch{}

    return e.Message;
    }
    }
    finally
    {
    aConnection.Close();
    }
    }
    #endregion
      

  5.   

    谁可以给出这个存储过程吗?我不太会存储过程!select @@Identity怎样用啊?