VC连接SQL问题!

wcqgm 2006-05-19 10:53:18
我用vc连接数据库。我在网上看到连接的方法。我试了不知道怎么连不上。请那位高手给出连接SQL的语句。谢谢啊!
...全文
261 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
51365133 2006-05-20
  • 打赏
  • 举报
回复
#import "D:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","rsEOF")

CoInitialize(NULL);
_ConnectionPtr pConn(__uuidof(Connection));
_RecordsetPtr pRst(__uuidof(Recordset));
_CommandPtr pCmd(__uuidof(Command));

pConn->ConnectionString="Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=pubs";
pConn->Open("","","",adConnectUnspecified);

//pRst=pConn->Execute("select * from authors",NULL,adCmdText);
//pRst->Open("select * from authors",_variant_t((IDispatch*)pConn),
// adOpenDynamic,adLockOptimistic,adCmdText);
pCmd->put_ActiveConnection(_variant_t((IDispatch*)pConn));
pCmd->CommandText="select * from authors";
pRst=pCmd->Execute(NULL,NULL,adCmdText);
while(!pRst->rsEOF)
{
((CListBox*)GetDlgItem(IDC_LIST1))->AddString(
(_bstr_t)pRst->GetCollect("au_lname"));
pRst->MoveNext();
}

pRst->Close();
pConn->Close();
pCmd.Release();
pRst.Release();
pConn.Release();
CoUninitialize();
xiangrujian 2006-05-20
  • 打赏
  • 举报
回复
帮顶!
syy64 2006-05-19
  • 打赏
  • 举报
回复
BOOL CDlgDBConnect::ConnectAccess()
{
if(m_strDatabase.IsEmpty())
{
::MessageBox(NULL,"Êý¾Ý¿â²»ÄÜΪ¿Õ£¡","Ìáʾ",MB_OK);
return false;
}
theApp.m_pConnection.CreateInstance(__uuidof(Connection));
try
{
theApp.m_strDatabase=m_strDatabase;
m_strDatabase.Replace("\\","\\\\");
_bstr_t strConn;
if(m_strPassword.IsEmpty())
{

strConn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +m_strDatabase;
theApp.m_pConnection->CursorLocation = adUseClient;
theApp.m_pConnection->Open(strConn,
"",
"",
adModeUnknown);
}
else
{
strConn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +m_strDatabase+";Persist Security Info=False;Jet OLEDB:DataBase Password="+m_strPassword;
theApp.m_pConnection->Open(strConn,
"",
"",
adModeUnknown);
}
}
catch(_com_error e)
{
::MessageBox(NULL,e.Description(),"Ìáʾ", MB_OK);
return FALSE;
}

::MessageBox(NULL,"Êý¾Ý¿âÁ¬½Ó³É¹¦£¡","Ìáʾ", MB_OK);
return TRUE;


}
wcqgm 2006-05-19
  • 打赏
  • 举报
回复
用的ado。
一条晚起的虫 2006-05-19
  • 打赏
  • 举报
回复
ADO 还是 ODBC?

4,018

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧