如何在VC中建立与ADO的连接 我是vc新手,不知道怎么样连接数据库,请你知道我,好么? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个是连接 SqlServer的BOOL CreateConnect(void){ CString m_sPassword=_T(""); CString m_sUser=_T("anonymous"); CString m_sServer=_T("ug00"); //CString strConn="uid=anonymous;pwd=;driver={SQL Server};server=ug00;database=STDLIBRARY;dsn=''"; CString strConn; //strConn.Format("uid=%s;pwd=%s;driver={SQL Server};server=%s;database='';dsn='SPL'",m_sPassword,m_sUser,m_sServer); //strConn.Format("uid=%s;pwd=%s;driver={SQL Server};server=%s;database=STDLIBRARY;dsn=''",m_sPassword,m_sUser,m_sServer); //建立连接串 strConn.Format("driver={SQL Server};server=%s;database=STDLIBRARY;dsn=''",m_sServer); //CString strConn="Provider=SQLOLEDB;Persist Security Info=False;User ID="+g_Global.m_strUserName+";Initial Catalog=GLSTD_LIB;Data Source="+g_Global.m_strServerName+";Password="+g_Global.m_strPassword+";";//ZHANGSG+ HRESULT hr; try{ /*----------------问题出在这里--------------*/ hr=gl_pConnection.CreateInstance(__uuidof(Connection)); if(SUCCEEDED(hr)) { gl_pConnection->ConnectionTimeout=10;//连接时间 hr=gl_pConnection->Open(_bstr_t((LPCTSTR)strConn), _bstr_t(L"anonymous"), _bstr_t(L""), adModeUnknown); if(SUCCEEDED(hr)) { //g_Global.bConnected=TRUE; return TRUE; } } } catch(_com_error &e)//捕捉异常情况 {#ifdef _DEBUG _bstr_t bstrSource(e.Source()); _bstr_t bstrDescription(e.Description()); CString csError; csError.Format(":\n\t编号:%08x\n\t编号含义:%s\n\t源代码:%s\n\t描述:%s\n", e.Error(),e.ErrorMessage(),(LPCTSTR)bstrSource,(LPCTSTR)bstrDescription); MessageBox(NULL,(LPCTSTR)csError,"[Arx->BuidDbConnection]引进的对象出现异常",MB_OK|MB_ICONEXCLAMATION);#endif //g_Global.bConnected=FALSE; return FALSE; } catch(...) {#ifdef _DEBUG AfxMessageBox("不可捕捉的异常。",MB_OK|MB_ICONEXCLAMATION);#endif //g_Global.bConnected=FALSE; return FALSE; } return FALSE; } 你可以参考深入前出MFC和VC技术内幕两书中的数据库的编程实例。 看看这两篇文章,很好http://www.vckbase.com/document/viewdoc.asp?id=307http://www.vckbase.com/document/viewdoc.asp?id=215 先要#import ado15.dll建立_Connection初始化COM对象呵呵,只能这么提示了,你用Variant作为搜索关键字到MSDN里找,能找到相关内容的。用Ado作关键字找的东西太多了 急求教基本TCP一对多通信问题 一个关于剪切板的很奇怪的问题 P2P之UDP穿透NAT(重发) cformview与控件问题 为什么ListCtrl控件的TopLeft总是(0,0)? 开发API有什么用呀! 远程抓取屏幕怎么做? 不好意思的问一下 如何在已经建立好了的工程中增加对数据库的支持? 在vb控件中如何控制容器或父窗口的中的控件属性 ●为什么我的ClassView窗口中,总是有些类显示不出来?● 按钮怎么不显示?
BOOL CreateConnect(void)
{ CString m_sPassword=_T("");
CString m_sUser=_T("anonymous");
CString m_sServer=_T("ug00");
//CString strConn="uid=anonymous;pwd=;driver={SQL Server};server=ug00;database=STDLIBRARY;dsn=''";
CString strConn;
//strConn.Format("uid=%s;pwd=%s;driver={SQL Server};server=%s;database='';dsn='SPL'",m_sPassword,m_sUser,m_sServer);
//strConn.Format("uid=%s;pwd=%s;driver={SQL Server};server=%s;database=STDLIBRARY;dsn=''",m_sPassword,m_sUser,m_sServer);
//建立连接串
strConn.Format("driver={SQL Server};server=%s;database=STDLIBRARY;dsn=''",m_sServer);
//CString strConn="Provider=SQLOLEDB;Persist Security Info=False;User ID="+g_Global.m_strUserName+";Initial Catalog=GLSTD_LIB;Data Source="+g_Global.m_strServerName+";Password="+g_Global.m_strPassword+";";//ZHANGSG+
HRESULT hr;
try{ /*----------------问题出在这里--------------*/
hr=gl_pConnection.CreateInstance(__uuidof(Connection));
if(SUCCEEDED(hr))
{
gl_pConnection->ConnectionTimeout=10;//连接时间
hr=gl_pConnection->Open(_bstr_t((LPCTSTR)strConn),
_bstr_t(L"anonymous"),
_bstr_t(L""),
adModeUnknown);
if(SUCCEEDED(hr))
{
//g_Global.bConnected=TRUE;
return TRUE;
}
}
}
catch(_com_error &e)//捕捉异常情况
{
#ifdef _DEBUG
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
CString csError;
csError.Format(":\n\t编号:%08x\n\t编号含义:%s\n\t源代码:%s\n\t描述:%s\n",
e.Error(),e.ErrorMessage(),(LPCTSTR)bstrSource,(LPCTSTR)bstrDescription);
MessageBox(NULL,(LPCTSTR)csError,"[Arx->BuidDbConnection]引进的对象出现异常",MB_OK|MB_ICONEXCLAMATION);
#endif
//g_Global.bConnected=FALSE;
return FALSE;
}
catch(...)
{
#ifdef _DEBUG
AfxMessageBox("不可捕捉的异常。",MB_OK|MB_ICONEXCLAMATION);
#endif
//g_Global.bConnected=FALSE;
return FALSE;
}
return FALSE;
}
http://www.vckbase.com/document/viewdoc.asp?id=307
http://www.vckbase.com/document/viewdoc.asp?id=215
建立_Connection
初始化COM对象呵呵,只能这么提示了,你用Variant作为搜索关键字到MSDN里找,能找到相关内容的。用Ado作关键字找的东西太多了