 |
|
 |
Hi there,
I'm a developer of a software company. I am a C++ coder and when i develop my project, i faced a big problem and i have tried my best to solve this problem, but i found no way. Any body can help me???
My project is a C++ ATL COM Service. In my module, it has a connection ADODB :: _ConnectionPtr, use to connect to internal access database. Ofcource, this database is put at the same place with the module. We create the connection once when my module initialize.
Then the QC has test some test cases involve to "Disable network". They open net work connection, and disable it. At the first time of this behavior, maybe it run correctly, and it have no problem.
But at the sencond time, my module have an exception when i call this line of code ::
CString szSQL = _T("SELECT * FROM PRINCIPAL");
Exception here ->>
hr = pRecordset->Open(_bstr_t(szSQL), vNull,ADODB::adOpenForwardOnly,
ADODB::adLockOptimistic,ADODB::adCmdText);
I don't know why and some case the CPU is up to nearly 97% when i try to use method Open() of Recordset object.
->>But when we unplug the network cable, it still runnning correctly.
And i guess that maybe, when we disabled net work, windows may disable some involve service...
Million thanks to your help
I'd like to here your idea.
Any contact- please contact with me at hnctam@gmail.com
HNCT
|
|
|
|
 |
|
 |
When I open a new recordset i always do it within try-and-catch section like this:
#define DB_CONNECTION_BROKEN 0x80004005
try
{
}
catch(_com_error & error)
{
if(error.Error() == DB_CONNECTION_BROKEN)
{
}
}
P.S. Please vote for my article here: QuickADO
|
|
|
|
 |
|
 |
Hello guys
here is the query i am executing.
pRecordset->Open(query,_variant_t((IDispatch*)pConnection,true),adOpenForwardOnly,adLockOptimistic,adCmdText);
variable query build dynamically based on the user's selection.
for an example
Intial query what i may have is
query="Select fname,lname,mname,emaild from consultants" and these are default
But user can choose more columns on the conditions applicable something like below query as an example
query = "Select fname,lname,mname,emaild,job_title from consultants,jobs where jobs.jbob_id=consultants.job_id "
Now my Question is how to know how many and what columns are selected by record set.
I hope you understand the question though it is phrased with bad english.
Thanks
Sudhakar
|
|
|
|
 |
|
 |
You need to use ADOX for this see http://www.codeproject.com/database/caaadoxclass.asp#xx955695xx
John
|
|
|
|
 |
|
 |
yeah, right...that's just advertising nonsense!!!
ADO on its own contains sufficient information about available columns;
long nCols = pRecordset->Fields->Count; for(long i = 0;i < nCols;i ++) {
ADODB::Field * pField = NULL; pRecordset->Fields->get_Item(_variant_t(i), &pField); _bstr_t strColName = pField->Name; long lDataType = pField->Type; pField->Release(); }
P.S. This example was taken from this article: QuickADO
|
|
|
|
 |
|
 |
Thanks for the info.
John
|
|
|
|
 |
|
 |
better late than never, right?
Here's an example of code taken from article QuickADO:
long nCols = pRecordset->Fields->Count; for(long i = 0;i < nCols;i ++) { ADODB::Field * pField = NULL; pRecordset->Fields->get_Item(_variant_t(i), &pField); _bstr_t strColName = pField->Name; long lDataType = pField->Type; pField->Release(); }
|
|
|
|
 |
|
 |
Could you tell me when to use connected one and disconnected one. What are the things that will be affacted.
|
|
|
|
 |
|
 |
For simplicity, you can just stick to disconnected one more scalable. However you should use connected one when the number of records fetched is small.
Regards,
Siau Tan Long
|
|
|
|
 |
|
 |
You did't tell me trade-off. My table contains more than 65000 records if I use connected one what will make difference from disconnected one pls provide some details.
|
|
|
|
 |
|
 |
I assume that you are working on a web application. If you use a connected one, chances are your database connection will stay on longer. Each database connection takes up system resource, so in order to make your application more scalable you should release the database connection as soon as your application finishes using it. By using a disconnected one, you release each connection as soon as you finished using it.
In fact, I think you should use some paging mechanism to reduce the number of records fetched.
Regards,
Siau Tan Long
|
|
|
|
 |
|
 |
In your source you use:
pRs->AddRef();
*returnvalue = pRs;
pRs->putref_ActiveConnection(NULL);
but it is more elegant like this:
pRs->putref_ActiveConnection(NULL);
*returnvalue = pRs.Detach();
if you want to write smaller code try to get rid of try...catch blocks and disble it in C++ tab under Settings.
And import type library with raw_interfaces_only
|
|
|
|
 |
|
|
 |
|
 |
Thats Assuming you are returning the recordsets parameter as in this case.
Otherwise you need to use QueryInterface
|
|
|
|
 |
|
 |
Just wondering why you called AddRef, rather then QueryInterface ?
e.g.
pRecordset->AddRef();
instead of
pRecordset->QueryInterface(__uuidof(_Recordset),(void **)returnvalue);
I think its better to use QueryInterface as it checks to see if the object supports that interface and also retrieves a pointer to the interface, while also calling AddRef.
|
|
|
|
 |
|
 |
I have a question about adUseClient.
When you say disconnected ADO Recordset,its because of this property(curser Location)?In this case you cant' make change to datasource,can you?
So I think if I use adUseServer (default CursorLocation),it will work as connected and I can make change to datasource.Am I correct or not?
<html>Mazy</html>
"So,so you think you can tell,
Heaven from Hell,
Blue skies from pain,...
How I wish,how I wish you were here." Wish You Were Here-Pink Floyd-1975
|
|
|
|
 |
|
 |
No, because the recordset had been disconnected from the source.
pRs->putref_ActiveConnection(NULL);
|
|
|
|
 |
|
 |
Siau Tan Long wrote:
No, because the recordset had been disconnected from the source.
Would mention its the answer to which part of my question?
Or explan more?
Thanks
<html>Mazy</html>
"So,so you think you can tell,
Heaven from Hell,
Blue skies from pain,...
How I wish,how I wish you were here." Wish You Were Here-Pink Floyd-1975
|
|
|
|
 |
|
 |
adUseServer means that cursor resides in the server.
adUseClient means that cursor resides in the client.
You have to use adUseClient for disconnected recordset.
You can modify the recordset but it will not be updated to the database. However, you can reconnect the recordset back after you have made the modification.
I hope this helps.
|
|
|
|
 |
|
 |
Siau Tan Long wrote:
I hope this helps.
Yes,Thank you.
<html>Mazy</html>
"So,so you think you can tell,
Heaven from Hell,
Blue skies from pain,...
How I wish,how I wish you were here." Wish You Were Here-Pink Floyd-1975
|
|
|
|
 |
|
 |
If you use adUseServer, the Recordset will never get disconected
But if you lately want to save changes to DB, you can still use this:
pRs->putref_ActiveConnection(pConn);
pRs->UpdateBatch(adAffectAll);
this will save all your changes to DB , but you have to open it with adLockBatchOptimistic flag.
|
|
|
|
 |
|
 |
Thanks for your reply.
spod letela wrote:
But if you lately want to save changes to DB, you can still use this:
What do you mean by lately?You mean when?
<html>Mazy</html>
"So,so you think you can tell,
Heaven from Hell,
Blue skies from pain,...
How I wish,how I wish you were here." Wish You Were Here-Pink Floyd-1975
|
|
|
|
 |
|
|
 |