id字段在sql数据库中自动增长

解决方案 »

  1.   

    select max(id) from ......
    很容易就能获得当前最大的id
      

  2.   

    select @@IDENTITY as xxx在INSERT后写上这句。不行的话就再加上FROM 表名
      

  3.   

    用stored procedure添加数据,并立即返回@@identity
      

  4.   

    参考 IBUYSPY 的例子:注意 CustomerID 是自增字段////////////////////////////////////////////////////////////////
                SqlParameter parameterCustomerID = new SqlParameter("@CustomerID", SqlDbType.Int, 4);
                parameterCustomerID.Direction = ParameterDirection.Output;
                myCommand.Parameters.Add(parameterCustomerID);            try {
                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    myConnection.Close();                // Calculate the CustomerID using Output Param from SPROC
                    int customerId = (int)parameterCustomerID.Value;                return customerId.ToString();
                }
                catch {
                    return String.Empty;
                }
    //////////////////////////////////////////////////////////////////////////////CREATE Procedure CustomerAdd
    (
        @FullName   nvarchar(50),
        @Email      nvarchar(50),
        @Password   nvarchar(50),
        @CustomerID int OUTPUT
    )
    ASINSERT INTO Customers
    (
        FullName,
        EMailAddress,
        Password
    )VALUES
    (
        @FullName,
        @Email,
        @Password
    )SELECT
        @CustomerID = @@Identity
    GO