Hemant Kumar
Hemant Kumar

Reputation: 4611

Can we get the Inserted Record ID?

I have a problem like retriving the Inserted Record Identity Value.Here I am inserting the record using DatSet.Suppose I have a table GAME where I have deleted all the records and I am inserting the record using dataset as given below

SqlDataAdapter sqlAdapter = new SqlDataAdapter("select * from GAME", sqlCon);
            DataSet ds = new DataSet();
            DataRow dr;
            sqlAdapter.FillSchema(ds, SchemaType.Source, "GAME");
            sqlAdapter.Fill(ds, "GAME");
            dr = ds.Tables["GAME"].NewRow();
            SqlCommandBuilder objSqlcmdBuilder = new SqlCommandBuilder(sqlAdapter);
            dr["NAME"] = "TABLE TENNIS";
            dr["PlayedDate"] = "01/02/2010";
            ds.Tables["GAME"].Rows.Add(dr);
            sqlAdapter.Update(ds, "GAME");
            MessageBox.Show(Convert.ToString(dr["GameId"]), "DataSet Demo");

Here always I am getting the GameId as '0'. But I Need to get the Inserted Record Identity Value.Is there any possible way to get using DataSet?I know the process to get inserted record Id value using @@Identity e.t.c.

Upvotes: 1

Views: 215

Answers (1)

Niladri Biswas
Niladri Biswas

Reputation: 4171

Use OUTPUT Clause e.g. Output.Inserted, Output.Updated , Output.Deleted. I could have done a small example for you but I found it is already there [SQL SERVER – 2005 – OUTPUT Clause Example and Explanation with INSERT, UPDATE, DELETE] . Hope this will be helpful.

Upvotes: 1

Related Questions