LastInsertID - Maybe im a dumb! :P

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for MySQL
Post Reply
Optimiza
Posts: 5
Joined: Tue 06 Feb 2007 18:19

LastInsertID - Maybe im a dumb! :P

Post by Optimiza » Tue 06 Feb 2007 18:31

Hi,

Im used to code with mySQL from PHP and for retrieve the LastID from a table after an Insert usually I did a table with counters to assure it on a multiuser environment.

But now, sad, im coding an app for .Net, and using directly the commands from your library there is no problem to do it, its easy as well you put it in a public var.

But now im trying to get it after a Adapter update.

Easy code:
- Binding addnew
- A form filled from scrath, with binding to a ds.
- Binding endupdate
- TableAdaptor Update
- And then...

Dunno whats the best way to get the last inserted id form there, think it should be easy but cant find solution, thinking i dont wanna do a ugly code doing a nasty select.. etc...

Can you show me the light and give me the best easier solution for it, thinking that it can be a multiuser app.

Thanx in advance,

Alexey
Posts: 2756
Joined: Mon 13 Mar 2006 07:43

Post by Alexey » Wed 07 Feb 2007 13:29

Code: Select all

select last_insert_id();
This statement returns the last inserted id in the current connection. If there are many users, you should consider using lock table statement. For more information please consult MySQL documentation.

Optimiza
Posts: 5
Joined: Tue 06 Feb 2007 18:19

Post by Optimiza » Wed 07 Feb 2007 14:28

I did it already, I knew it, just i was wondering because Visual Studio is doing the update via adaptor i thought it has those function. Also when you configure the adaptor Visual Studio asked me about do it saying something like "Do you want to Visual Studio makes a Select after insert update to retrieve identity field".... I said YES, but i dont know where is that data which he says will retrieve.

Alexey
Posts: 2756
Joined: Mon 13 Mar 2006 07:43

Post by Alexey » Fri 09 Feb 2007 07:24

There is a property InsertId in MySqlCommand class. You can use it (MySqlDataAdapter.InsertCommand.InsertId).

Optimiza
Posts: 5
Joined: Tue 06 Feb 2007 18:19

Post by Optimiza » Fri 09 Feb 2007 07:59

Luv ya! (jk)

Thats the thing is was searching.

Many many thanx

btw: I bought your software.

Alexey
Posts: 2756
Joined: Mon 13 Mar 2006 07:43

Post by Alexey » Fri 09 Feb 2007 08:17

You are welcome.

Optimiza
Posts: 5
Joined: Tue 06 Feb 2007 18:19

Post by Optimiza » Fri 09 Feb 2007 08:36

Yeh its there,

But its a private value.

TableAdapter.Adapter.InserCommand.InserId

I can see it in inspect but dont know how to reach it without hardcode something.

mmmm...

Sorry about the time im cosuming, just I think that case will happens to many ppl and its a common issue.

Alexey
Posts: 2756
Joined: Mon 13 Mar 2006 07:43

Post by Alexey » Fri 09 Feb 2007 09:03

Please send me (alexeyi at crlab dot com) your project to see what data binding you use.

tsoukai
Posts: 1
Joined: Wed 04 Apr 2007 19:26

Accesing private properties in a TableAdapter

Post by tsoukai » Wed 04 Apr 2007 19:41

I am using .net 2.0. The TableAdapter class is a partial class (and you have the source code for it) so you can add any additional functionality to it. Simply create (and include in your project) a file containing whatever code you want to add to the class.

The following code adds public properties for the InsertID of the Command objects. (Look in your xxxDataSet.Designer.cs file for the name of the class and the namespace for your TableAdapter.)

Code: Select all

namespace xxx.xxxDataSetTableAdapter
{
      public partial class xxxTableAdapter
      {
          public long InsertID
          {
               get
               {
                    return (this._adapter.InsertCommand.InsertId);
               }
          }
      }
}
Then you can access them like this after updating the dataset:
long insertID = this.xxxTableAdapter.InsertID;

Happy Trying, :D

Post Reply