#include
#include
#include
#include
#include
#include
#include
#include
#include
#pragma comment(lib, "comsuppw.lib")
#import "C:\Program Files (x86)\Common Files\system\ado\msado15.dll" no_namespace rename ("EOF", "adoEOF")
#pragma comment(lib,"odbc32.lib")
using namespace std;
struct userMsg
{
void putstr()
{
cout << userName << endl;
cout << userPwd << endl;
}
string userName;
string userPwd;
};
int main()
{
userMsg umg;
::CoInitialize(NULL);
_ConnectionPtr pconnect;
HRESULT hr = pconnect.CreateInstance(_uuidof(Connection)); //创建链接句柄
_RecordsetPtr pRecordset;
if (FAILED(pRecordset.CreateInstance(_uuidof(Recordset)))) //初始化结果集指针
{
printf("初始化结果集指针失败\n");
}
pconnect->CursorLocation = adUseClient; //加上这句才能获取到结果集;
_bstr_t strConnect = "Driver={sql server};server=127.0.0.1;uid=sa;pwd=123456;database=moon;";
pconnect->Open(strConnect, "", "", adModeUnknown); //链接字符串链接 链接数据库
//pRecordset->Open("insert into UserMsg(userID, userPWD) values('tern', '12345678');" , (IDispatch*)pconnect, adOpenDynamic, adLockOptimistic, adCmdText);
Fields *pFields = NULL;
_variant_t vAffect;
string strSql = "select * from UserMsg";
pRecordset = pconnect->Execute(strSql.c_str(), &vAffect, adCmdText); // 执行sql语句
pRecordset->get_Fields(&pFields);
long len = 0;
pRecordset->get_RecordCount(&len); //获取结果集中的个数
pRecordset->get_Fields(&pFields);
long w = pFields->Count;
while (!pRecordset->adoEOF)
{
for (long i=0; i < w;i++)
{
umg.userName = _com_util::ConvertBSTRToString(pRecordset->GetCollect(_variant_t(i)).bstrVal);
umg.putstr();
}
pRecordset->MoveNext();
}
cout << len << endl;
cin >> len;
pconnect->Close();
return 0;
}