</pre><pre name="code" class="cpp">#include <string>
#include <iostream>
//c:\Program Files\Common Files\System\ADO\msado15.dll
#import "c:\Program Files\Common Files\System\ADO\msado15.dll" no_namespace rename("EOF", "EndOfFile")
using namespace std;
int main()
{
::CoInitialize(NULL);
_ConnectionPtr m_pConnection=NULL;
_RecordsetPtr m_pRecordset=NULL;
m_pConnection.CreateInstance(__uuidof(Connection));
m_pRecordset.CreateInstance(__uuidof(Recordset));
_bstr_t strConnect = "Provider=SQLOLEDB.1;Password=123;Persist Security Info=True;\
User ID=sa;Initial Catalog=LBME;Data Source=127.0.0.1,1433";
//_bstr_t strConnect = "Provider=SQLOLEDB.1;Server=127.0.0.1,1433;Database=LBME;uid=sa; pwd=123;";
//_bstr_t strConnect ="Provider=SQLOLEDB.1; User ID=sa; Password=123; Data Source=127.0.0.1,1433; Initial Catalog=LBME";
//_bstr_t bstrSQL("select * from TheUser where UserID=12;");
_bstr_t bstrSQL("ALTER TABLE TheUser ADD Id int");
try
{
m_pConnection->Open(strConnect, "","", adModeUnknown);
//m_pConnection->Open("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=LBME","","",adModeUnknown);
m_pRecordset->Open(bstrSQL, m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
_variant_t vstr, vuint;
while (!m_pRecordset->EndOfFile)
{
vstr = m_pRecordset->GetCollect("UserID");
//vuint = m_pRecordset->GetCollect("uint");
//unsigned int str = (_bstr_t)vstr.bstrVal;
unsigned int uint = vstr.ulVal;
cout<<uint;
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
}
catch (_com_error e)
{
cout<<e.Description()<<endl;
return 0;
}
if(m_pConnection->State)
{
m_pConnection->Close();
}
::CoUninitialize();
return 0;
}
vs2010连接sqlserver2008
最新推荐文章于 2021-10-26 05:05:11 发布