我用的是ACCESS數據庫,
When我要新增1個新User的時候,我怎樣才能得到這個新User的ID值呢?(UserID為自動編號,主鍵)在ASP中,
我可以用 newUserID = rs("UserID"),得到剛剛新增的User的ID值的!在ASP.NET中,我用的是OleDBCommand,我怎樣實現這個功能呢?我不用存儲過程的,只用内嵌的SQL語句的。
INSERT INTO 。。要是用2條SQL語句的話,
1,INSERT INTO //插入User
2,SELECT //ID值 要是在多用戶的時候,產生了並發,那麽你得到的ID值,就有可能不是我剛剛生成的ID值了,有可能是別的用戶所生成的ID值了!!!有辦法解決嗎?謝謝!

解决方案 »

  1.   

    SELECT         TOP 1 ID
    FROM             BOMNOTICE
    ORDER BY  ID DESC
      

  2.   

    将insert into 和 SELECT TOP 1 ID FROM BOMNOTICE ORDER BY ID DESC
    用一个事务提交不就可以了吗?
      

  3.   

    Microsoft Access 不支持存储过程或批命令处理,因此无法将输出参数映射到上例所示表中的源列。但是,Microsoft Access 2000 或更高版本支持 @@IDENTITY 属性在“插入”(INSERT) 后检索“自动编号”字段的值。使用 RowUpdated 事件,您可以确定“插入”(INSERT) 是否已发生,检索最新的 @@IDENTITY 值,然后将该值放入 DataSet 中本地表的标识列。以下代码示例显示如何将一个新值插入 Microsoft Access 2000 Northwind 数据库的 Categories 表中。该示例使用 RowUpdated 事件来填充在向 Categories 表中插入记录时 Jet 引擎和 Access 数据库所生成的“自动编号”值。请注意,这仅适用于 Jet 4.0 OLE DB 提供程序和 Microsoft Access 2000 或更高版本。using System;
    using System.Data;
    using System.Data.OleDb;public class Sample
    {
      static OleDbConnection nwindConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" +
                                                      @"Data Source=c:\Program Files\Microsoft Office\Office\Samples\northwind.mdb;");  public static void Main() 
      {
        // Use the DataAdapter to fill and update the DataSet.
        OleDbDataAdapter catDA = new OleDbDataAdapter("SELECT CategoryID, CategoryName FROM Categories ORDER BY CategoryID", nwindConn);    catDA.InsertCommand = new OleDbCommand("INSERT INTO Categories (CategoryName) Values(?)", nwindConn);
        catDA.InsertCommand.CommandType = CommandType.Text;    catDA.InsertCommand.Parameters.Add("@CategoryName", OleDbType.Char, 15, "CategoryName");    nwindConn.Open();
     
        // Fill the DataSet.
        DataSet catDS = new DataSet();
        catDA.Fill(catDS, "Categories");    // Add a new row.
        DataRow newRow = catDS.Tables["Categories"].NewRow();
        newRow["CategoryName"] = "New Category";
        catDS.Tables["Categories"].Rows.Add(newRow);    // Include an event to fill in the Autonumber value.
        catDA.RowUpdated += new OleDbRowUpdatedEventHandler(OnRowUpdated);    // Update the DataSet.
        catDA.Update(catDS, "Categories");    nwindConn.Close();
      }  protected static void OnRowUpdated(object sender, OleDbRowUpdatedEventArgs args)
      {
        // Include a variable and a command to retrieve the identity value from the Access database.
        int newID = 0;
        OleDbCommand idCMD = new OleDbCommand("SELECT @@IDENTITY", nwindConn);    if (args.StatementType == StatementType.Insert)
        {
          // Retrieve the identity value and store it in the CategoryID column.
          newID = (int)idCMD.ExecuteScalar();
          args.Row["CategoryID"] = newID;
        }
      }
    }
      

  4.   

    如果增加user的那个用户帐号没有并发的可能那么可以
    SELECT         TOP 1 ID
    FROM             BOMNOTICE 
    WHERE USER=*** 
    ORDER BY  ID DESC