如果数据量比较大的话,重新查询整个数据表会影响程序性能,所以,只查询一条就好了….
/// <summary>
/// 执行Insert语句,并返回新添加的记录ID
/// </summary>
/// <returns></returns>
public object ExecuteNonQueryAndGetIdentity(string sql)
{
try
{
using (OleDbCommand com = new OleDbCommand(sql, con))
{
if (com.ExecuteNonQuery() >= 1)
{
com.CommandText = "select @@identity";
return com.ExecuteScalar();
}
else
{
throw new Exception("记录添加不成功!");
}
}
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 执行Insert语句,并返回新添加的记录ID
/// </summary>
public object ExecuteNonQueryAndGetIdentity(string sql, OleDbParameter par)
{
try
{
using (OleDbCommand com = new OleDbCommand(sql, con))
{
com.Parameters.Add(par);
if (com.ExecuteNonQuery() >= 1)
{
com.CommandText = "select @@identity";
return com.ExecuteScalar();
}
else
{
throw new Exception("记录添加不成功!");
}
}
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 执行Insert语句,并返回新添加的记录ID
/// </summary>
/// <param name="sql"></param>
/// <param name="array"></param>
/// <returns></returns>
public object ExecuteNonQueryAndGetIdentity(string sql, ParametersArray array)
{
try
{
OleDbCommand com = new OleDbCommand(sql, con);
SetParametersArray(ref com, array);
if (com.ExecuteNonQuery() >= 1)
{
com.CommandText = "select @@identity";
return com.ExecuteScalar();
}
else
{
throw new Exception("记录添加不成功!");
}
}
catch (Exception)
{
throw;
}
}
可以添加到上一篇文章所写的类中使用
文章置顶申明很给力……
你也看出来了…
可不是……太狠了……
非常时期,要用非常的手段.
不懂程序….看不太懂….. -_-!
萝卜青菜各有所爱