首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

大奇怪啊vc中使用ADO技术连接数据库,Connection象的Open函数丢失连接字串,该怎么处理

2012-01-14 
大奇怪啊!!vc中使用ADO技术连接数据库,Connection象的Open函数丢失连接字串在使用ADO访问sql数据库,出现了

大奇怪啊!!vc中使用ADO技术连接数据库,Connection象的Open函数丢失连接字串
在使用ADO访问sql数据库,出现了奇怪的事情.
我用桌面建立.udl文件法获取连接字串,付给了_ConnectionPtr   m_pcon-> connectionstring;但是在调试过程中,发现真正传给
inline   HRESULT   Connection15::Open   (   _bstr_t   ConnectionString,   _bstr_t   UserID,   _bstr_t   Password,   long   Options   )   时,连接字串少了一部分啊!
具体程序如下:
BOOL   CMYADO2App::InitInstance()
{

CoInitialize(NULL);
/*m_pcon.CreateInstance(__uuidof(Connection));
m_pcom.CreateInstance(__uuidof(Command));
m_prcd.CreateInstance(__uuidof(Recordset));*/

m_pcon.CreateInstance( "ADODB.Connection ");
m_prcd.CreateInstance( "ADODB.Recordset ");

m_pcon-> ConnectionString= "Provider=SQLOLEDB.1;\
  Integrated   Security=SSPI;Persist   Security   Info=False;\
  User   ID=LOCALWINDOWSNT\\ONE;Initial   Catalog=Northwind;\
Data   Source=LOCALWINDOWSNT\\ONE ";
CString   str=(LPCSTR)m_pcon-> ConnectionString;

m_pcon-> Open((_bstr_t) "Provider=SQLOLEDB.1;\
Integrated   Security=SSPI;Persist   Security   Info=False;\
User   ID=LOCALWINDOWSNT\\ONE;Initial   Catalog=Northwind;\
Data   Source=LOCALWINDOWSNT\\ONE ",
"LOCALWINDOWSNT\\ONE ", "   ",\
adConnectUnspecified);
                   
AfxEnableControlContainer();

//   Standard   initialization
//   If   you   are   not   using   these   features   and   wish   to   reduce   the   size
//     of   your   final   executable,   you   should   remove   from   the   following
//     the   specific   initialization   routines   you   do   not   need.

#ifdef   _AFXDLL
Enable3dControls();//   Call   this   when   using   MFC   in   a   shared   DLL
#else
Enable3dControlsStatic();//   Call   this   when   linking   to   MFC   statically
#endif

CMYADO2Dlg   dlg;
m_pMainWnd   =   &dlg;
int   nResponse   =   dlg.DoModal();
if   (nResponse   ==   IDOK)
{
//   TODO:   Place   code   here   to   handle   when   the   dialog   is
//     dismissed   with   OK
}
else   if   (nResponse   ==   IDCANCEL)
{
//   TODO:   Place   code   here   to   handle   when   the   dialog   is
//     dismissed   with   Cancel
}

//   Since   the   dialog   has   been   closed,   return   FALSE   so   that   we   exit   the
//     application,   rather   than   start   the   application 's   message   pump.
return   FALSE;
}
调试到
inline   HRESULT   Connection15::Open   (   _bstr_t   ConnectionString,   _bstr_t   UserID,   _bstr_t   Password,   long   Options   )   时发现ConnectionString值是
{ "Provider=SQLOLEDB.1;Integrated   Security=SSPI;Persist   Security   Info=False;User   ID=LOCALWINDOWSNT\ONE;Initial   Catalog=Northwin "   (1)}
与原来我给的值相比,Northwind变成了Northwin,Data   Source=LOCALWINDOWSNT\\ONE项也丢失了!
结果当然不对了!!


急!!!!急!!!!!!!!!!!究竟是怎么回事??难道编译器有问题????????????


[解决办法]
这个我帮你顶顶,我也不知道具体的原因
[解决办法]
我以前遇到过insert语句少一部分的情况
也不知道怎么回事
[解决办法]
就是传入服务器名,用户名,密码吧?
试下用其他方法传下,我是这样定义的:
void CADOConn::OnInitDBConnect()
{
CString strUser;
CString strServer;
CString strPwd;
CString strConnection;
strUser = theApp.m_UserName;
strPwd = theApp.m_Password;
strServer = theApp.m_ServerName;

strConnection = "Provider=SQLOLEDB;Server= ";
strConnection+= strServer + ";Database=troublecheck;uid= ";
strConnection+= strUser + ";pwd= ";
strConnection+= strPwd + "; ";

::CoInitialize(NULL);
try
{
m_pConnection.CreateInstance(__uuidof(Connection));
_bstr_t strConnect = (_bstr_t)strConnection;
m_pConnection-> Open(strConnect, " ", " ",adModeUnknown);
//数据库连接成功
theApp.bConnectDB = TRUE;

}

catch(_com_error e)
{
//数据库连接失败
theApp.bConnectDB = FALSE;
//AfxMessageBox( "failed ");
//AfxMessageBox(e.Description()+ "系统退出! ");

}
}

_RecordsetPtr& CADOConn::GetRecordSet(_bstr_t bstrSQL)
{
try
{

if (m_pConnection == NULL)
{
OnInitDBConnect();
}

m_pRecordset.CreateInstance(_uuidof(Recordset));
m_pRecordset-> Open(bstrSQL, (_variant_t)m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
}
catch (_com_error e)
{
AfxMessageBox(e.Description()+ "系统退出! ");
exit(0);
}
return m_pRecordset;
_variant_t var;
}
[解决办法]

[解决办法]
你的udl配的对不对呀 ?

可以通过udl测试一下数据库连接情况!3L的方法是可以的
但是如果你的udl配置的没有问题的话,就把他放到自己的工程目录下
然后调用下面的函数
参数说明: udlName 为配置的udl全名如:SourceName.udl

ConnectToDb(char * udlName )
{
char szUdlName[100];

sprintf(szUdlName,"File Name=%s",udlName);
//初始化ADO
while( 1 )
{
try
{
pDBConn.CreateInstance( __uuidof( Connection ) );
pRDSet.CreateInstance( __uuidof( Recordset ) );
pRDSetA.CreateInstance( __uuidof( Recordset ) );
m_commandptr.CreateInstance (__uuidof(Command));
pDBConn->ConnectionString = szUdlName;
pDBConn->Open( "", "", "", NULL );

m_commandptr->ActiveConnection =pDBConn ;

return TRUE;
}
catch( _com_error &e )
{
LogFiles.Write( " Connect DB Failed. Error:%s\n", e.ErrorMessage( ) );
pDBConn = NULL;
Sleep( 1000 );
continue;
}
break;
}
return TRUE;
}
这个函数是自己曾用数据库连接的函数,应该没有问题

热点排行