public int getLastId() 
        {
            //openConn;       //鏈接數據庫
            string sql = "select top 1 account_id from account order by acount_id desc";
            
            
            return lastid;
        
        }上面是我的一個方法 ,請大家幫忙!

解决方案 »

  1.   

    将ID设置为自增列
    Select @ID = @@IDENTITY 
      

  2.   

           /// <summary>
            /// 返回指定sql语句的datatable
            /// </summary>
            /// <param name="sqlstr"></param>
            /// <returns></returns>
            public DataTable dataTable(string sqlstr)
            {
                DataTable dt = new DataTable();
                OleDbDataAdapter da = new OleDbDataAdapter();
                try
                {
                    openConn();
                    comm.CommandType = CommandType.Text;
                    comm.CommandText = sqlstr;
                    da.SelectCommand = comm;
                    da.Fill(dt);
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
                finally
                {
                    closeConn();
                }
                return dt;
            }        
            
            /// <summary>
            /// 查找最後一個id的值,並加上一個1 
            /// </summary>
            /// <returns></returns>
            public int getLastId() 
            {
                //openConn;       //鏈接數據庫
                string sql = "select top 1 * from account order by account_id desc";            DataTable dt = dataTable(sql);
                   if ( dt!=null)
                    if ( dt.Rows.Count>0 )
                     return (int)dt.Rows[0][0];
                   return 0;
            }
    謝謝大家,我搞定了!
      

  3.   

    謝謝一樓,原來的數據庫沒計是不有自動加上,所以現在我是自己找出最好的id,然後在加上!上面的在多載一下就ok了!
      

  4.   

    我有个笨办法:
    先查select * from table order by id desc
    然后在他外面套上SELECT TOP 1 * FROM (select * from table order by id desc)
      

  5.   

    那你取一个int值类型的字段值,比你返回一个DataTable要好的多
      

  6.   

    不知道你的想法是什么,这个方法只适合小流量数据,如果同一时刻数据大就会得到错误的ID
    一般的做法是写个存储过程,做一个输出参数OUT (int id),然后在前台接收。
      

  7.   

    select max(acount_id) from order;
      

  8.   

    原來的數據庫中,那個account_id沒有自動加一,我要做的是讀出最大的那個id,然後加上一在寫入新數據的時候用!
      

  9.   

    select   isnull(max(id),0)+1  from   order
    防止表中无记录
      

  10.   

     string sql = "select top 1 A.* from (select account_id from account order by acount_id desc) A";
      

  11.   

    謝謝謝謝   parkershouhou ,但我不知道怎麼結貼
      

  12.   

    access数据库可用iif函数:
    select iif(max(id) is null, 0) + 1 from order