c# Insert into Sql

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<P>Is there anyway to get the ID of an item inserted into a database at the time of insertion?</P>
<P>Heres what I mean, I have a database table with 5 columns. First column is called ID, where sql automatically assigns a number. When I make the call from c# it looks something like this.</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px
<P><FONT face="Courier New, Courier, Monospace" size=1>string sqlIns = "INSERT INTO table (name, information, other) VALUES (@name, @information, @other)";</FONT></P>
<P><FONT face="Courier New" size=1>db.Open();
try
{
SqlCommand cmdIns = new SqlCommand(sqlIns, db.Connection);
cmdIns.Parameters.Add("@name", info);
cmdIns.Parameters.Add("@information", info1);
cmdIns.Parameters.Add("@other", info2);
cmdIns.ExecuteNonQuery();
cmdIns.Dispose();
cmdIns = null;
}
catch(Exception ex)
{
throw new Exception(ex.ToString(), ex);
}
finally
{
db.Close();
}</FONT></P></BLOCKQUOTE>
<P dir=ltr><FONT face="Geneva, Arial, Sans-serif Now within this statement I am wondering if I can gather the "ID" field. Because some items will be deleted from this table, I would not be able to use the scalar command. Please advise or send me to a tutorial that explains how to do this. Thank you much in advance.</FONT></P>

View the full article
 
Back
Top