First we set the instance of the connection with the database and then we create a sql command. After this, we open the connection and execute the command directly to the database.
If everything executes correctly, we will obtain the first available key. If not, an exception is going to be raised.
public int GetOid()
{
OleDbConnection conn = ConnectionMgr.GetInstance().GetConnection();
OleDbCommand cmd = new OleDbCommand("Select * From Oid", conn);
int oidNum = 0;
try
{
conn.Open();
oidNum = (int)cmd.ExecuteScalar();
oidNum++;
cmd = new OleDbCommand("UPDATE Oid SET lastOid=" + oidNum, conn);
cmd.ExecuteNonQuery();
}
catch (OleDbException ex)
{
throw new DatabaseException("Error updating oid", ex);
}
finally
{
conn.Close();
}
return oidNum;
}
1 comment:
Thanks for this informative blog. These is also a really informative website www.ShareTermPapers.com where you can share your opinions and a lot of good projects and e-Books available for NCERT, MBA, BBA, MCA, BCA and B.Tech students. Interview Tips and study materials are also available. Do check it out........
Post a Comment