Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

This is a sample code to select all records from a table. Can someone show me how to select the last record of that table?

select * from table

When I use: SELECT * FROM TABLE ORDER BY ID DESC LIMIT I get this error: Line 1: Incorrect syntax near 'LIMIT'. This is the code I use:

private void LastRecord()

{

    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HELPDESK_OUTLOOKConnectionString3"].ToString());

    conn.Open();

    SqlDataReader myReader = null;

    SqlCommand myCommand = new SqlCommand("SELECT * FROM HD_AANVRAGEN ORDER BY " +

                "aanvraag_id DESC LIMIT 1", conn);

    myReader = myCommand.ExecuteReader();

    while (myReader.Read())

    {

        TextBox1.Text = (myReader["aanvraag_id"].ToString());

        TextBox1.Text += (myReader["wijziging_nummer"].ToString());

        TextBox1.Text += (myReader["melding_id"].ToString());

        TextBox1.Text += (myReader["aanvraag_titel"].ToString());

        TextBox1.Text += (myReader["aanvraag_omschrijving"].ToString());

        TextBox1.Text += (myReader["doorlooptijd_id"].ToString());

        TextBox1.Text += (myReader["rapporteren"].ToString());

        TextBox1.Text += (myReader["werknemer_id"].ToString());

        TextBox1.Text += (myReader["outlook_id"].ToString());

    }

}

1 Answer

0 votes
by (40.7k points)

If you are using SQL Server, then try this:

SELECT TOP 1 * FROM Table ORDER BY ID DESC

For MySql, use this:

SELECT * FROM Table ORDER BY ID DESC LIMIT 1

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...