How do I get the @@identity value without using stored procedures in C#?
I have the a table named 'sports' that has an @@identity value for the
primary key.
What I want to do is after I insert into into the 'sports' table, I want to
get the @@identity value (primary key) for what I just inserted.
I have to following code if this helps, thanks in advance:
---------------------------------------------------------
string sql = "insert into sports (name.......";
conn.Open();
// insert record into the database
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader result = cmd.ExecuteRead er();
result.Close();
// ??? GET THE IDENTITY OF WHAT WAS JUST INSERTED ???
SqlCommand ident = new SqlCommand("sel ect @@identity from sports", conn);
SqlDataReader ident_result = ident.ExecuteRe ader();
int sport_id = -1;
if (ident_result.R ead()) { ident_result.Ge tDecimal(0); }
ident_result.Cl ose();
conn.Dispose();
I have the a table named 'sports' that has an @@identity value for the
primary key.
What I want to do is after I insert into into the 'sports' table, I want to
get the @@identity value (primary key) for what I just inserted.
I have to following code if this helps, thanks in advance:
---------------------------------------------------------
string sql = "insert into sports (name.......";
conn.Open();
// insert record into the database
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader result = cmd.ExecuteRead er();
result.Close();
// ??? GET THE IDENTITY OF WHAT WAS JUST INSERTED ???
SqlCommand ident = new SqlCommand("sel ect @@identity from sports", conn);
SqlDataReader ident_result = ident.ExecuteRe ader();
int sport_id = -1;
if (ident_result.R ead()) { ident_result.Ge tDecimal(0); }
ident_result.Cl ose();
conn.Dispose();
Comment