public int getLastId()
{
//openConn; //鏈接數據庫
string sql = "select top 1 account_id from account order by acount_id desc";
return lastid;
}上面是我的一個方法 ,請大家幫忙!
{
//openConn; //鏈接數據庫
string sql = "select top 1 account_id from account order by acount_id desc";
return lastid;
}上面是我的一個方法 ,請大家幫忙!
Select @ID = @@IDENTITY
/// 返回指定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;
}
謝謝大家,我搞定了!
先查select * from table order by id desc
然后在他外面套上SELECT TOP 1 * FROM (select * from table order by id desc)
一般的做法是写个存储过程,做一个输出参数OUT (int id),然后在前台接收。
防止表中无记录
select iif(max(id) is null, 0) + 1 from order