-
May 20th, 2008, 08:35 AM
#1
Connect database when start with Windows
I use ADO to connect to database. I connect successfully. But when I run my application with Windows there's an error :
Debug error :
This application has requested the Runtime to terminate it in an unusual way
Some one plz explain for me & give me a solution
Here is a part of my code:
iSize = 0;
DateTimeNote Temp;
bool bFlag = true;
Open("Provider=Microsoft.Jet.OLEDB.4.0; \
Data Source=DateNote.mdb");
CString sql;
variant_t vNull;
variant_t TheValue;
vNull.vt = VT_ERROR;
vNull.scode = DISP_E_PARAMNOTFOUND;
CString StrResult;
CString StrSql;
CString StrDateTime;
CString StrDateTimeTemp;
StrDateTime.Format(_T("%i/%i/%i"),DateTime.GetMonth(),DateTime.GetDay(),DateTime.GetYear());
StrSql.Format(_T("SELECT * FROM DATETIMENOTE WHERE NGAY = '%s'"), StrDateTime);
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pRecordset->PutRefActiveConnection(m_pConnection);
m_pRecordset->Open(_variant_t(StrSql), vNull, adOpenForwardOnly, adLockOptimistic, adCmdText);
while (!m_pRecordset->GetEndOfFile())
{
TheValue = m_pRecordset->GetCollect("GIO");
if(TheValue.vt != VT_NULL)
{
StrResult = (char *)_bstr_t(TheValue);
StrDateTimeTemp = StrDateTime + _T(" ") + StrResult;
Temp.DateTime.ParseDateTime(StrDateTimeTemp, LOCALE_NOUSEROVERRIDE, LANG_USER_DEFAULT);
}
TheValue = m_pRecordset->GetCollect("NOTE");
if(TheValue.vt != VT_NULL)
{
StrResult = (char *)_bstr_t(TheValue);
Temp.Note = StrResult;
}
pTemp[iSize] = Temp;
iSize ++;
m_pRecordset->MoveNext();
}
-
May 20th, 2008, 08:41 AM
#2
Re: Connect database when start with Windows
Most likely you are using a variable that is not initialized properly or NULL, or an exception was thrown from your code.
Code like m_pRecordset.CreateInstance(__uuidof(Recordset)); should be checked if it executed successfully and if it is possible to throw exceptions, it should be inside a try/catch block.
The best way to see what happens is to debug your program line by line and see where it crashes.
Har Har
-
May 20th, 2008, 09:41 AM
#3
Re: Connect database when start with Windows
Be careful about your usage of strings. You have "", _T(""), and _bstr_t in your code.
Make sure that you are using the correct string type for what you need.
-
May 20th, 2008, 09:55 AM
#4
Re: Connect database when start with Windows
Let me explain : My code runs properly. When I debug as well as when I run the application file. It just doesn't work well when running with Windows. I don't know why b
-
May 20th, 2008, 10:29 AM
#5
Re: Connect database when start with Windows
Originally Posted by capint
Let me explain : My code runs properly. When I debug as well as when I run the application file. It just doesn't work well when running with Windows. I don't know why b
What do you mean, "doesn't work well when running with Windows"?
Do you mean during Windows startup?
-
May 20th, 2008, 10:31 AM
#6
Re: Connect database when start with Windows
I'm not sure what you mean by "when runs with Windows".
Perhaps you mean when you start the application from the Windows Explorer? If so, then your problem may be the default directory.
When you run your application in the IDE (debugger) the default directory (where it's looking for your DateNote.mdb file is the directory containing the .sln and .vcproj files.
However when you run it from Windows Explorer, the default directory is the one containing your .exe (Debug or Release probably). The .mdb file would need to be in that directory.
Hope that helps.
Be sure to rate those who help!
-------------------------------------------------------------
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
May 20th, 2008, 10:31 AM
#7
Re: Connect database when start with Windows
Yes. I put my application in starup and it doesn't work. When I remove connecting database. It runs well with Windows. Of course, I put the file Access at the same directory
Last edited by capint; May 20th, 2008 at 10:39 AM.
-
May 20th, 2008, 10:54 AM
#8
Re: Connect database when start with Windows
I suggest you add some error handling to your application and log the errors to a log file.
-
May 20th, 2008, 10:56 AM
#9
Re: Connect database when start with Windows
Can you show me how to do that ? I only know debug when VC is running
-
May 20th, 2008, 10:57 AM
#10
Re: Connect database when start with Windows
The start up folder of your application when run under startup may be different than what you think and as previously suggested, your app may not be able to find the .mdb file.
I suggest you add some error handling to your application and log the errors to a log file. Or you could catch the exception and temporarily display a MessageBox with the error.
-
May 20th, 2008, 11:03 AM
#11
Re: Connect database when start with Windows
If you're right (my app may not be able to find the .mdb file). So, why I can run my application properly when it doesn't run with Windows. I debuged carefully and it load & saved database well
And if you are right ? How can I handle it ? I put the file mdb at the same directory with the application file, is that right ?
Last edited by capint; May 20th, 2008 at 11:25 AM.
-
May 20th, 2008, 12:42 PM
#12
Re: Connect database when start with Windows
Originally Posted by capint
If you're right (my app may not be able to find the .mdb file). So, why I can run my application properly when it doesn't run with Windows. I debuged carefully and it load & saved database well
And if you are right ? How can I handle it ? I put the file mdb at the same directory with the application file, is that right ?
Please quit referring to this as 'run with windows'. You have a Windows application, right? So it's always running with Windows - the term is confusing. To avoid confusion say something like, 'running during startup' or 'when my app is launched from startup.'.
That being said, have you tried to use the logging approach or to post a messagebox that I suggested? That will tell you if indeed, the error is due to the .mdb file not getting found. That way you know for sure what the problem is. If it is the problem, there are approaches you can take so your application finds the .mdb file.
-
May 20th, 2008, 12:49 PM
#13
Re: Connect database when start with Windows
I'm sorry so much
I've done like you said. When my app runs during startup, it can not load file mdb. Could you show me any solution ?
I tried to get the whole path of the file mdb by the function GetCurrentDirectory + filename.
But in the function m_pConnection->Open(CmdStr, "", "", -1); CmdStr is a char *. I found it's difficult to cast from CString to char *, or from TCHAR * to char *. So, I still can't carry it out. So plz help me
Last edited by capint; May 20th, 2008 at 01:39 PM.
-
May 20th, 2008, 05:22 PM
#14
Re: Connect database when start with Windows
This should get you started.
Stdafx.h
Code:
#include <iostream>
#include <tchar.h>
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString constructors will be explicit
#include <atlbase.h>
#include <atlstr.h>
#include <atlWin.h>
const int INVALID_INDEX = -1;
Cpp file
Code:
#include "stdafx.h"
#import "C:\\Program Files\\Common Files\\System\ado\\msado15.dll" rename( "EOF", "adoEOF" )
typedef ADODB::_ConnectionPtr IAdoConnectionPtr;
CString GetAppFolder( )
{
CString sAppFolder;
int nPos = INVALID_INDEX;
if( !::GetModuleFileName( NULL, sAppFolder.GetBuffer( MAX_PATH ), MAX_PATH ) )
{
throw _com_error( HRESULT_FROM_WIN32( ::GetLastError( ) ) );
}
sAppFolder.ReleaseBuffer( );
// Extract the folder from the full path
if( INVALID_INDEX != ( nPos = sAppFolder.ReverseFind( _T('\\') ) ) )
{
sAppFolder.Truncate( nPos );
}
return sAppFolder;
}
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr = S_OK;
::CoInitialize( NULL );
try
{
// Retrieve the appfolder and form the connection string
CString sConnection;
sConnection.Format( _T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s%\\Nwind.mdb"), GetAppFolder( ) );
IAdoConnectionPtr spIConnection = NULL;
if( FAILED( hr = spIConnection.CreateInstance( __uuidof( ADODB::Connection ) ) ) )
{
throw _com_error( hr );
}
spIConnection->ConnectionString = _bstr_t( sConnection );
_bstr_t bstrEmpty;
if( FAILED( hr = spIConnection->Open( bstrEmpty, bstrEmpty, bstrEmpty, INVALID_INDEX ) ) )
{
throw _com_error( hr );
}
}
catch( _com_error e )
{
::MessageBox( NULL, e.ErrorMessage( ), _T("Access Test"), MB_OK | MB_ICONERROR );
}
::CoUninitialize( );
return 0;
}
Note: this code compiles and runs in ANSI or UNICODE
Last edited by Arjay; May 20th, 2008 at 06:45 PM.
-
May 21st, 2008, 06:35 AM
#15
Re: Connect database when start with Windows
Arjay, thank you so much.
I really couldn't do any thing without your help
I learned from your code that we can cast CString to _bstr_t and with my previous solution I still can solve my problem.
I still don't understand why the app can't load mdb files while they're put at the same directory ...
Thnks again Arjay !
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|