Re: Getting Autoincrement ID in ADO.NET



One of the principle complaints about ADO classic was how it generated DML
action commands at runtime. While this approach worked for several simple
situations, it fell apart quickly when you stepped over the line. While ADOc
gave you several options to determine how concurrency checks were made,
developers found that they could not use the Update method because of other
limitations.

ADO.NET on the other hand is totally agnostic of how you have built your
tables and relationships or how you want to perform concurrency checks. This
means that all of the code auto-generated by ADOc has to be either coded by
hand in ADO.NET at design time or you have to depend on the CommandBuilder
which has fewer options than ADOc. Many developers eventually migrate to
creating the DML UpdateCommand, InsertCommand and DeleteCommand SQL on their
own.

In your example, you'll need to tell ADO.NET that the column in question is
autoincrementing unless you populated the schema from a database table that
already has this attribute set. When the new row is created, ADO.NET
supplies a value. However, post Update, you're right, you need to fetch the
current value from the server (or DBMS like JET). Each DBMS has it's own
technique--I discuss these in my article on "Managing an @@Identity Crisis"
(see http://www.betav.com/msdn_magazine.htm).

My book "ADO.NET and ADO Examples and Best Practices" discusses these issues
in detail.

hth

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

<tascienu@xxxxxxxxxxxx> wrote in message
news:1126451832.964285.59150@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> But really no one has answered my question as far. I am moving to
> ADO.NET from ADO. And i just want to do the same thing i was doig in
> ADO, but using the new technology. New technologies should be able to
> do what Old technology did (easily), and even do it better (Am I wrong
> here?). THEY SAID THAT .NET WILL CUT OUR DEVELOPMENT TIME. SO, I WANT
> TO SEE THAT.
>
> I have found a partial solution using ExecuteScalar.
>
> CmdConn.ExecuteScalar("INSERT INTO tblCustomers(Name)VALUES('j. smith')
> ; SELECT @@IDENTITY")
>
> But when I am doing my inserts, i don't usually use execute. I do
> DataAdapter.Update( Dataset );
>
> In ADO, I did this easily:
>
> recordSet rs
> rs.AddNew
> rs("Name")="J. Smith"
> rs.UpdateBatch()
> NewID = rs("ID").value
>
> there you go. 3 lines of code, and you are there. Now, I don't mean to
> say that ADO.NET is bad. I think it's better. I just need to learn the
> new way to do the same thing. As I am migrating my code, I usually do
> this:
>
> SQLDataAdapter da
> Dataset ds
> Datarow dr = ds.Tables(0).NewRow
> dr("Name")="J. Smith"
> da.Update(ds)
> NewID = dr("ID") <-- Unlike in ADO, this one contains NOTHING.
>
> so, I need some pointers as to how i could accomplish this... Is my
> question clear ???
>


.



Relevant Pages

  • ADO query through a view - how control DML constructions
    ... I use TADODataSet with query statemet through a view in DB, but DML ... constuctions generated by ADO are for the underling tables.Can i force ADO ...
    (borland.public.delphi.database.ado)
  • Re: DAO vs ADO
    ... ADO will be around for a while, but it's definitely not the way of the ... wouldn't necessarily recommend new development in deprecated ... Access isn't a deprecated technology ...
    (microsoft.public.access.conversion)
  • Re: DAO vs ADO
    ... ADO will be around for a while, but it's definitely not the way of the ... wouldn't necessarily recommend new development in deprecated ... Access isn't a deprecated technology ...
    (microsoft.public.access.adp.sqlserver)
  • Re: Selecting Databases
    ... > I want to change my fondemental base from BDE to one another Technology, ... > Which Technology recommended, dbExpress or ADO? ... The BDE is not recommended for new projects now, ... dbExpress is lightweight, ...
    (borland.public.delphi.database.ado)
  • Re: Getting Autoincrement ID in ADO.NET
    ... I am moving to ... ADO, but using the new technology. ... I have found a partial solution using ExecuteScalar. ...
    (microsoft.public.dotnet.framework.adonet)