dll中链接数据库的问题.
在dll中定义了一个导出函数.ConnSqlServer(),在函数中调用TDM 中的ConnectDB()去链接数据库.编译没有问题.可是一连数据库,m_pConn-> Open() ;时,程序就像是死掉了.请各位朋友,指点一二.主要程序如下:
__declspec(dllexport) void __stdcall ConnSqlServer(HWND MainWindowHandle)
{
CoInitialize(NULL);
Application-> Handle = MainWindowHandle;
TDM *pDM = new TDM(Application);
if(!pDM-> ConnectDB())
{
delete pDM;
return;
}
ShowMessage( "链接成功 ");
CoUninitialize();
}
TDM 是 TDataModule 类定义与实现文件是DMUnit.h,DMUnit.cpp
在dmUnit.h中
定义了
private:
TADOConnection *m_pConn;
public:// User declarations
bool __fastcall ConnectDB();
TADOConnection* __fastcall GetConnection();
在DMUnit.cpp中
bool __fastcall TDM::ConnectDB()
{
try
{
m_pConn = new TADOConnection(NULL);
AnsiString sconnString;
sconnString.sprintf( "Provider=SQLOLEDB.1;Password=%s;Persist Security Info=True;User ID=%s;Initial Catalog=%s;Data Source=%s "
, "qazwsx ", "sa ", "mytest ", "127.0.0.1 ");
m_pConn-> LoginPrompt = false;
m_pConn-> ConnectionString = sconnString;
m_pConn-> Open() ; //执行到这行时阻寨住了.
}
catch(Exception &e)
{
return false;
}
catch(...)
{
return false;
}
return m_pConn-> Connected;
}
//---------------------------------------
TADOConnection* __fastcall TDM::GetConnection()
{
return m_pConn;
}
请各位大侠看看是哪里出了问题.兄弟我挺急的.
[解决办法]
第一个问题就出在TDM *pDM = new TDM(Application);
需要
CoInitialize(NULL);
try
{
m_pConn = new TADOConnection(NULL);
AnsiString sconnString;
sconnString.sprintf( "Provider=SQLOLEDB.1;Password=%s;Persist Security Info=True;User ID=%s;Initial Catalog=%s;Data Source=%s "
, "qazwsx ", "sa ", "mytest ", "127.0.0.1 ");
m_pConn-> LoginPrompt = false;
m_pConn-> ConnectionString = sconnString;
m_pConn-> Open() ; //执行到这行时阻寨住了.
}
catch(Exception &e)
{
return false;
}
catch(...)
{
return false;
}
return m_pConn-> Connected;
CoUninitialize();
而不是在调用dll的时候调用