I need a function which executes an INSERT statement on a database and returns the Auto_Increment primary key. I have the following C# code but, while the INSERT statement w
If you would like to retrieve value of auto running number of transaction that you're inserting and your environment following 1. Database is MsAccess. 2. Driver is Jet4 with connection string like this "Provider=Microsoft.Jet.OLEDB.4.0;Password={0};Data Source={1};Persist Security Info=True" 3. use Oledb
You can apply my example to your code
OleDbConnection connection = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Password={0};Data Source={1};Persist Security Info=True",dbinfo.Password,dbinfo.MsAccessDBFile);
connection.Open();
OleDbTransaction transaction = null;
try{
connection.BeginTransaction();
String commandInsert = "INSERT INTO TB_SAMPLE ([NAME]) VALUES ('MR. DUKE')";
OleDbCommand cmd = new OleDbCommand(commandInsert , connection, transaction);
cmd.ExecuteNonQuery();
String commandIndentity = "SELECT @@IDENTITY";
cmd = new OleDbCommandcommandIndentity, connection, transaction);
Console.WriteLine("New Running No = {0}", (int)cmd.ExecuteScalar());
connection.Commit();
}catch(Exception ex){
connection.Rollback();
}finally{
connection.Close();
}