How to make a particular record stay at the end of sql server database

时间:2015-06-30 13:31:29

标签: sql mfc

I am making a dialog based application in MFC, where in there is a need to read the database and display the contents from database, i managed to find the record and display it in a list box by comparing unique Id from edit box and database, but the problem is I cannot display the last record for some reason it's not happening, I am successfully able to display from 1 to n-1 so I decided to put nth record as dummy so I just need a method in which I can make that dummy record to always stay at the last or end of the database, Thank you

{
int flag = 0;
    CString ID;
    CDatabase database;
    CString sqlString=L"SELECT *FROM FM_REGISTER";
    wchar_t connstring[500] = L"Driver={SQL SERVER};Server=FCS-LOKANATH\\SQLEXPRESS;DataBase=FM;Integrated Security=True";
    database.Open(NULL, 0, 0, connstring);
    m_InfoID.GetWindowText(ID);
    m_InfoList.ResetContent();//this is a report command
        CRecordset recordset(&database);
        CString temp, record,temp2;
        recordset.Open(CRecordset::forwardOnly, sqlString, CRecordset::readOnly);
        while (!recordset.IsEOF()){//is null
            record = _T("");
            register int len = recordset.GetODBCFieldCount();
            for (register int i = 0; i < 2; i++)
            {
                recordset.GetFieldValue(i, temp);
                if (i == 0 & ID.CompareNoCase(temp) == -1)
                {
                    //MessageBox(L"Positive Macth");
                    flag = 1;
                }
                else
                {
                    //MessageBox(L"Nagative Macth");
                    flag = 0;
                }
                record += temp + _T("|");
            }
            m_InfoList.AddString(record);
            recordset.MoveNext();
        }
    MessageBox(_T("Query done."), 0, 0);
    // TODO: Add your control notification handler code here
}

1 个答案:

答案 0 :(得分:1)

OK, re-read and this didn't answer what you asked.

Post your code, we need to see how you are iterating the results. The answer below is about ordering and not a missing record.

You can do this with an order by on your query.

SELECT id, description, order FROM mytable ORDER BY order ASC

This will cause the query to be retrieved ordered by the order column, and you just need to make sure that the order in the database is largest for the record you want to appear last.