_ConnectionPtr pMyConnect;
_RecordsetPtr m_pRecordset;
HRESULT hr=pMyConnect.CreateInstance(__uuidof(Connection));
if(FAILED(hr)){ return;}_bstr_t strConnect="Provider=SQLOLEDB; Server=TLL\TONGJIE;"
"Database=DB; uid=sa; pwd=123;";
//connecting to the database server now:
try{pMyConnect->Open(strConnect,"","",adModeUnknown);}
catch (_com_error &e)//运行时在这里捕获到了异常
{
::MessageBox(NULL,e.Description(),"警告",MB_OK | MB_ICONWARNING);
}工程名为database
上一段放在databaseView.cpp的void CDatabaseView::OnInitialUpdate()方法里
StdAfx.h中已经添加
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "adoEOF")
且在database.cpp的BOOL CDatabaseApp::InitInstance()方法中添加初始化OLE/COM的::CoInitialize(NULL)方法。编译可以通过,但是总连接不上数据库。
不知道什么原因,数据库方面是没有问题的,用JAVA可以连接上。
以前用的一直是JAVA,现在需要用到VC,一点头绪都没有。
好郁闷啊,恳请大家指点!
多谢
_RecordsetPtr m_pRecordset;
HRESULT hr=pMyConnect.CreateInstance(__uuidof(Connection));
if(FAILED(hr)){ return;}_bstr_t strConnect="Provider=SQLOLEDB; Server=TLL\TONGJIE;"
"Database=DB; uid=sa; pwd=123;";
//connecting to the database server now:
try{pMyConnect->Open(strConnect,"","",adModeUnknown);}
catch (_com_error &e)//运行时在这里捕获到了异常
{
::MessageBox(NULL,e.Description(),"警告",MB_OK | MB_ICONWARNING);
}工程名为database
上一段放在databaseView.cpp的void CDatabaseView::OnInitialUpdate()方法里
StdAfx.h中已经添加
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "adoEOF")
且在database.cpp的BOOL CDatabaseApp::InitInstance()方法中添加初始化OLE/COM的::CoInitialize(NULL)方法。编译可以通过,但是总连接不上数据库。
不知道什么原因,数据库方面是没有问题的,用JAVA可以连接上。
以前用的一直是JAVA,现在需要用到VC,一点头绪都没有。
好郁闷啊,恳请大家指点!
多谢
_bstr_t ConnStr="ODBC;Provider=SQLOLEDB;Driver=SQLServer};Server=127.0.0.1;Uid=sa;Pwd=123456;Database=ExchZT"if(FAILED(::CoInitialize(NULL)))
return FALSE;
m_pConnection.CreateInstance(_uuidof(Connection));
m_pRecordset.CreateInstance(_uuidof(Recordset));try
{
m_pConnection->CursorLocation= adUseClient;
m_pConnection->Open(ConnStr,"","",0); //连接叫作ADOTest的ODBC数据源
}
catch(_com_error e) //异常处理
{
AfxMessageBox(e.ErrorMessage()+e.Description());
return;
}
//连接数据源
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
//使用ODBC连接
hr = m_pConnection->Open("DSN=REMO","","",0);
//AfxMessageBox("数据源连接成功。");
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}在ODBC中设置数据源,看看可不可以
再说
编译通过,但仍连接不上
未指定的错误[Microsoft][ODBC 驱动程序 管理器] 未发现数据源名称并且未指定默认驱动程序。但我已经在系统DSN上添加过数据源了。
未通过编译
Linking...
LINK : fatal error LNK1168: cannot open Debug/database2.exe for writing
Error executing link.exe.database2.exe - 1 error(s), 0 warning(s)link error不知道是怎么回事,请赐教
多谢
未通过编译
Linking...
LINK : fatal error LNK1168: cannot open Debug/database2.exe for writing
Error executing link.exe.database2.exe - 1 error(s), 0 warning(s)link error不知道是怎么回事,请赐教
多谢就是说你的database2已经运行了,怎么写?
------------------
duyhui(杜)的观点
既然用了ado就不要用odbc了,也就不需要dsn...
下面的ClientData是SQLSERVR数据中的数据库名
m_pConnection.CreateInstance(__uuidof(Connection)); CString strConnection;
strConnection.Format("Provider=SQLOLEDB.1;%s;%s","192.168.0.4","ClientData");
CString strUserID,strPassword;
strUserID = "sa";
strPassword = ""; HRESULT hr;
if (m_pConnection->GetState() != adStateClosed)
{
m_pConnection->Close();
} try
{
m_pConnection->CommandTimeout = 3;
hr = m_pConnection->Open(_bstr_t(strConnection), _bstr_t(strUserID), _bstr_t(strPassword), NULL);
if ( hr != S_OK )
{
AfxMessageBox("打开数据库错误!");
return ;
}
}
catch(...)
{
AfxMessageBox("打开数据库错误!");
return ;
}
用的是duyhui(杜)的代码
我在ODBC里将数据源删除了,但出现的错误仍然是
未指定的错误[Microsoft][ODBC 驱动程序 管理器] 未发现数据源名称并且未指定默认驱动程序。就这个数据库连接都搅了我几天了,整个人都快崩溃了
m_pConnection->Open("DSN=DB;uid=sa;pwd=123;","","",0); 且在ODBC中添加了数据源
连接成功,并且可以正常的添加查询记录。
只是不知道为什么是这样?
多谢各位!
为什么ADO连接都有这么多种方式呢?