I am using windows forms C# and SQL. I have a table consists of two column:
Column1 = myID (primary key and unique ID)
Column2 = DateTime.
The following code inserts date/Time into the table:
private void button1_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("Data Source=PCN-TOSH;Initial Catalog=mydb;Integrated Security=True");
cn.Open();
SqlCommand cm = new SqlCommand("Insert into TableDate_Time (DateTime ) values (@DateTime)");
cm.Parameters.Add("@DateTime", SqlDbType.DateTime);
cm.Parameters["@DateTime"].Value = DateTime.Now;
cm.Connection = cn;
cm.ExecuteNonQuery();
// something like: var varID = the current myID value
}
My Question is: How can I save the last row value of myID column into a variable whenever I click the button? any idea? Thank you
IDENTITYcolumn or is it auniqueidentifercolumn?SCOPE_IDENTITY()will not work with that.NEWID()?