怎么在客户端对记录集进行排序
如何在客户端对记录集进行排序
感谢AlanChenBoy(Alan)先。
我用_RecordsetPtr在客户端打开了记录集,并添加了8个字段,然后Open,再AddNew了10条记录。现在我希望对记录集进行排序,用index或者sort都可以。做过这个的高人可否指点一下?
注:我用m_rs-> Sort= "Item,Location DESC ";
或者rs-> Sort= "Item ";
或者rs-> Sort= "[Item],[Location] DESC ";
但运行时都报错:排序无法被应用
搞不懂原因,搜索了网上,没有详细说明的。
已经注意的是:
m_rs-> CursorLocation=adUseClient;
------解决方案--------------------
直接在SQL语句中排序:
select * from your_table order by item
------解决方案--------------------
//////////////////////////////////////////////////////////
// //
// SortX Function //
// //
//////////////////////////////////////////////////////////
void SortX(void)
{
HRESULT hr = S_OK;
// Initialize pointers on define.
// These are in the ADODB:: namespace.
_ConnectionPtr pConnection = NULL;
_RecordsetPtr pRstAuthors = NULL;
// Define string variables.
_bstr_t strCnn( "Provider= 'sqloledb ';Data Source= 'MySqlServer '; "
"Initial Catalog= 'pubs ';Integrated Security= 'SSPI '; ");
try
{
TESTHR(pConnection.CreateInstance(__uuidof(Connection)));
TESTHR(pRstAuthors.CreateInstance(__uuidof(Recordset)));
pRstAuthors-> CursorLocation = adUseClient;
pConnection-> Open (strCnn, " ", " ", adConnectUnspecified);
pRstAuthors-> Open( "SELECT * FROM authors ",
_variant_t((IDispatch *) pConnection),
adOpenStatic, adLockReadOnly, adCmdText);
SortXprint( " Initial Order ", pRstAuthors);
//Clear the screen for the next display.
printf( "\nPress any key to continue... ");
getch();
system( "cls ");
pRstAuthors-> Sort = "au_lname ASC, au_fname ASC ";
SortXprint( "Last Name Ascending ", pRstAuthors);
//Clear the screen for the next display.
printf( "\nPress any key to continue... ");
getch();
system( "cls ");
pRstAuthors-> Sort = "au_lname DESC, au_fname ASC ";
SortXprint( "Last Name Descending ", pRstAuthors);
}
catch(_com_error &e)
{
PrintProviderError(pConnection);
PrintComError(e);
}
// Clean up objects before exit.
if (pRstAuthors)
if (pRstAuthors-> State == adStateOpen)
pRstAuthors-> Close();
if (pConnection)
if (pConnection-> State == adStateOpen)
pConnection-> Close();
}
------解决方案--------------------
pRstAuthors-> Sort = "au_lname ASC, au_fname ASC ";
用Sort属性就行了
------解决方案--------------------
写查询语句的时候后面加order by 。。。
------解决方案--------------------
自已做个排序,再重显示。
------解决方案--------------------
是为了显示而排序,那要显示的数据肯定不是很多,
多字段排序和单字段排序没有甚么区别,包括stl,微软的CList等类都直接可以排序,没有难度的
感谢AlanChenBoy(Alan)先。
我用_RecordsetPtr在客户端打开了记录集,并添加了8个字段,然后Open,再AddNew了10条记录。现在我希望对记录集进行排序,用index或者sort都可以。做过这个的高人可否指点一下?
注:我用m_rs-> Sort= "Item,Location DESC ";
或者rs-> Sort= "Item ";
或者rs-> Sort= "[Item],[Location] DESC ";
但运行时都报错:排序无法被应用
搞不懂原因,搜索了网上,没有详细说明的。
已经注意的是:
m_rs-> CursorLocation=adUseClient;
------解决方案--------------------
直接在SQL语句中排序:
select * from your_table order by item
------解决方案--------------------
//////////////////////////////////////////////////////////
// //
// SortX Function //
// //
//////////////////////////////////////////////////////////
void SortX(void)
{
HRESULT hr = S_OK;
// Initialize pointers on define.
// These are in the ADODB:: namespace.
_ConnectionPtr pConnection = NULL;
_RecordsetPtr pRstAuthors = NULL;
// Define string variables.
_bstr_t strCnn( "Provider= 'sqloledb ';Data Source= 'MySqlServer '; "
"Initial Catalog= 'pubs ';Integrated Security= 'SSPI '; ");
try
{
TESTHR(pConnection.CreateInstance(__uuidof(Connection)));
TESTHR(pRstAuthors.CreateInstance(__uuidof(Recordset)));
pRstAuthors-> CursorLocation = adUseClient;
pConnection-> Open (strCnn, " ", " ", adConnectUnspecified);
pRstAuthors-> Open( "SELECT * FROM authors ",
_variant_t((IDispatch *) pConnection),
adOpenStatic, adLockReadOnly, adCmdText);
SortXprint( " Initial Order ", pRstAuthors);
//Clear the screen for the next display.
printf( "\nPress any key to continue... ");
getch();
system( "cls ");
pRstAuthors-> Sort = "au_lname ASC, au_fname ASC ";
SortXprint( "Last Name Ascending ", pRstAuthors);
//Clear the screen for the next display.
printf( "\nPress any key to continue... ");
getch();
system( "cls ");
pRstAuthors-> Sort = "au_lname DESC, au_fname ASC ";
SortXprint( "Last Name Descending ", pRstAuthors);
}
catch(_com_error &e)
{
PrintProviderError(pConnection);
PrintComError(e);
}
// Clean up objects before exit.
if (pRstAuthors)
if (pRstAuthors-> State == adStateOpen)
pRstAuthors-> Close();
if (pConnection)
if (pConnection-> State == adStateOpen)
pConnection-> Close();
}
------解决方案--------------------
pRstAuthors-> Sort = "au_lname ASC, au_fname ASC ";
用Sort属性就行了
------解决方案--------------------
写查询语句的时候后面加order by 。。。
------解决方案--------------------
自已做个排序,再重显示。
------解决方案--------------------
是为了显示而排序,那要显示的数据肯定不是很多,
多字段排序和单字段排序没有甚么区别,包括stl,微软的CList等类都直接可以排序,没有难度的