Get latest inserted row (MSSQL, C#)

Problem: I Have a MSSQL table, where one column is the date of when each row was inserted. I want to get certain column value of the latest inserted row.

1. Create MSSQL query
2. Connect to db and get result

Example in C#:

string query = "SELECT TOP 1 mycolumn from table ORDER BY  date desc";
string value = "";

 //database connection
string strConnection = ConfigurationManager.ConnectionStrings["DbConnectionString"].ConnectionString;
SqlConnection Connection = new SqlConnection(strConnection);

Connection.Open();
SqlDataAdapter Command = new SqlDataAdapter();
Command.SelectCommand = new SqlCommand(query, Connection);
 try {
       value = (string)Command.SelectCommand.ExecuteScalar();
       }
        catch
        {
            //error
        }
        finally {
            Connection.Close();
        }

Comments

Name: 
Cancel