I've made my entire database for my application using only VS2012 express (I'm using localdb), so it handled creating the database as well as creating datasets and table adapters for my application to use. It's worked very well so far. I've also made wrapper classes to allow asynchronous database reads, which is also working great.
Looking at my table adapters in the designer, when I added my insert methods (for example on the Races
table) it used the following query:
INSERT INTO Races (ChampionshipID, Driver1, Driver2, RaceTime)
VALUES (@ChampionshipID,@Driver1,@Driver2,@RaceTime);
SELECT ID, ChampionshipID, Driver1, Driver2, RaceTime FROM Races WHERE (ID = SCOPE_IDENTITY())
I see it does a select afterwards to return the row that was just inserted. That's perfect for what I need. However, the actual method it generates has the following signature:
int Insert1(long ChampionshipID, long Driver1, long Driver2, DateTime RaceTime)
It's returning an int
and not the expected DataRow
or DataTable
, nor does it take a DataTable
as a parameter to insert it into.
Am I doing something wrong? How can I implement this functionality?
The answer is no because, insert method uses the
ExcecuteNonQuery
method ofADO.NET
, that's why your insert method is returning anint
with the id of the record inserted.You can try querying something like
select @@identity
just after inserting, so then you'll be able to get the record later.