M
Micheal
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.ExecuteReader();
result.Close();
// ??? GET THE IDENTITY OF WHAT WAS JUST INSERTED ???
SqlCommand ident = new SqlCommand("select @@identity from sports", conn);
SqlDataReader ident_result = ident.ExecuteReader();
int sport_id = -1;
if (ident_result.Read()) { ident_result.GetDecimal(0); }
ident_result.Close();
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.ExecuteReader();
result.Close();
// ??? GET THE IDENTITY OF WHAT WAS JUST INSERTED ???
SqlCommand ident = new SqlCommand("select @@identity from sports", conn);
SqlDataReader ident_result = ident.ExecuteReader();
int sport_id = -1;
if (ident_result.Read()) { ident_result.GetDecimal(0); }
ident_result.Close();
conn.Dispose();