≡ Menu




how to get lastest inserted identity working with access database

You know the word “@@IDENTITY” it really has magic, It returns me the latest inserted rows identity in my access database using oldebcommand. For this purpose i have to execute command object two times first for insert query and second time by execute secular for getting value back

 

public void InsertReviews(string _Nick, string _Name, string _EmailAddress, string _RealStateTitle, string _Address, string _City, string _ZipCode, string _Country, string _Comments, string _FromIP, string _Status, string _Langi, string _Lit)
{
string ConnString = Util.GetConnString();
string SqlString = "Insert Into ClientInfo (Nick,Name,EmailAddress,RealStateTitle,Address,City,ZipCode,Country,Comments,FromIP,Status,Langi,Lit) Values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
  string SqlString2 = "Select @@Identity";
using (OleDbConnection conn = new OleDbConnection(ConnString))
{
using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("Nick", _Nick);
cmd.Parameters.AddWithValue("Name", _Name);
cmd.Parameters.AddWithValue("EmailAddress", _EmailAddress);
cmd.Parameters.AddWithValue("RealStateTitle", _RealStateTitle);
cmd.Parameters.AddWithValue("Address", _Address);
cmd.Parameters.AddWithValue("City", _City);
cmd.Parameters.AddWithValue("ZipCode", _ZipCode);
cmd.Parameters.AddWithValue("Country", _Country);
cmd.Parameters.AddWithValue("Comments", _Comments);
cmd.Parameters.AddWithValue("FromIP", _FromIP);
cmd.Parameters.AddWithValue("Status", _Status);
cmd.Parameters.AddWithValue("Langi", _Langi);
cmd.Parameters.AddWithValue("Lit", _Lit);
conn.Open();
cmd.ExecuteNonQuery();
cmd.CommandText = SqlString2;
  int _Count = (int) cmd.ExecuteScalar();
}
}

}

Its working for me

Comments on this entry are closed.