how are you updating the database? dataset or parameterized query? you can always add a "SELECT @@IDENITY as ID" or "SELECT SCOPE_IDENTITY() AS ID" to retrieve the id

解决方案 »

  1.   

    select @@identity as id

    select max(id) as id from tablename
      

  2.   

    严重谢谢楼上关注!!从数据库中得到ID并且更新当前数据集,这点我是清楚的。但是此时的ID值现在WebService层上,主要问题是我如何将WebService层上的正确的ID值再返回到客户端数据集呢?在WebService上更新的方法为:
    [WebMethod]
    public DataSet UpdateDepartment(DataSet ds){....}客户端更新代码(简):
    DataSet changes=dsDepartment.GetChanges();
    s.UpdateDepartment(changes);//S为实例化WebService的类
    .........//后面应该如何处理才能让本地dsDepartment表中ID更新?【问题】此时DsDepartment.Tables[0]中的ID列如何也同时更新(数据集中仅一个表)?
      

  3.   

    see
    http://expert.csdn.net/Expert/topic/1528/1528367.xml?temp=.9534723
      

  4.   

    搞了一下午都没有搞定,郁闷
    我是通过GetDataSetChanges()的数据集更新数据,不是用存储过程。为WebService的DataAdapter上添加RowUpdated处理事件,不知为何却返回不了@@identity!?
    (按照ms-help://MS.VSCC/MS.MSDNVS.2052/cpguide/html/cpconretrievingidentityorautonumbervalues.htm中的做法,如果直接调用的话没有问题,可放在中间层就不行了)再有,若在WebService中的Changes数据集和数据源保持一致了,那客户端的ID又如何与Changes中的ID保持一致呢?在WebService中可以通过DataAdapter事件(如RowUpdated事件)或命令(SqlCommand),可是客户端因为调用的是WebMethod,如何使ID和WebService中的数据保持一致呢???
      

  5.   

    用了DataSet.Merge 方法没有try...
      

  6.   

    有试,但因本地新增加的记录没有ID值(Null),用Merge的话,那就又添加一条记录了,变成两条相同记录了(好像Merge是通过主键操作数据的,ID不一样,便增加)
      

  7.   

    how complicated is your dataset? does it have multiple tables and parent-child relationship? if it is not very complicated, on the client side, use AutoIncrementSeed = -1 and AutoIncrementStep = -1, and only send the Changed DataSet to the webserive. On the server side,  use a similar method to what was outlined in the above link to retrieve the new IDs. Then on the client side, delete all records all ID < 0, and merge the returned records, then call AcceptChanges() on the datasetotherwise, you have to requery the database and get all the data again
      

  8.   

    谢谢 saucer(思归, MS .NET MVP) 老大on the client side, use AutoIncrementSeed = -1 and AutoIncrementStep = -1,delete all records all ID < 0,and merge the returned records
    ------------------------------------
    这个办法试下先...