I have a script that should select from table all rows, order by id but only one row per select. This selection is made when a button is pressed. But I think that query is wrong, it selects only first row. Why?
private void select(){using (Conexiune.getConnection()){string select = "SELECT DISTINCT * FROM questions ORDER BY id ASC LIMIT 1";SQLiteCommand cmd = new SQLiteCommand(select, Conexiune.getConnection());cmd.CommandType = CommandType.Text;SQLiteDataReader rdra = cmd.ExecuteReader(CommandBehavior.CloseConnection);try{while (rdra.Read()){textBox1.Text = rdra["question"].ToString();textBox2.Text = rdra["answer1"].ToString();textBox3.Text = rdra["answer2"].ToString();textBox4.Text = rdra["answer3"].ToString();r1 = (int)rdra["option1"];r2 = (int)rdra["option2"];r3 = (int)rdra["option3"];SimulatorManager.Intrebare = textBox1.Text;}}catch (InvalidOperationException ex){MessageBox.Show(ex.Message);}}}
Diacu PaulPosted Mar 31, 2015, 11:45 AM
Pankaj Kumar ChoudharyPosted Mar 31, 2015, 8:18 AM
select top(1) * from (select distinct * from questions )Ques order by Id asc
here Ques is nothing it is a name of the data that is retrieved from internal query
select distinct * from questions"
you can give any name as you want here i gave "Ques"....
Diacu PaulPosted Mar 31, 2015, 1:31 AM
Pankaj Kumar ChoudharyPosted Mar 30, 2015, 8:36 PM
Top command.
using top command you can rewrite your query as
select top(1) * from (select distinct * from questions )Ques order by Id asc