马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
测试C++ ADO 访问修改SQL数据的时候 发现表内数据为空的时候会报指针的错误.下面上图片和上代码 #include <icrsint.h>
#include<iostream>
#include<iomanip>
#include <string>
#import "c:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF", "adoEOF") rename("BOF", "adoBOF")
class cSQL
{
public:
cSQL()
{
CoInitialize(NULL);
HRESULT hr = sqlSp.CreateInstance(__uuidof(Connection));
if (FAILED(hr))
{
std::cout << "初始化指针失败!" << std::endl;
return;
}
else
{
if (FAILED(m_pRecordset.CreateInstance(_uuidof(Recordset))))
{
std::cout << "记录集对象指针实例化失败!" << std::endl;
return;
}
}
}
~cSQL()
{
m_pRecordset->Close();
sqlSp->Close();
CoUninitialize();
}
void INIT(char password[])
{
sprintf_s(bueffer, 256, "Driver={sql server};server=127.0.0.1,1433;uid=sa;pwd=%s;database=wx2008db;", password);
_bstr_t strConnect = bueffer;
try
{
sqlSp->Open(strConnect, "", "", NULL);
}
catch (_com_error &e)
{
std::cout << e.Description() << std::endl;
}
try
{
m_pRecordset->Open("select * from tStat", (IDispatch*)sqlSp, adOpenDynamic, adLockOptimistic, adCmdText);
}
catch (_com_error &e)
{
std::cout << e.Description() << std::endl;
}
try
{
m_pRecordset->MoveFirst();
while (!m_pRecordset->adoEOF) ////遍历并读取sPassword列的记录并输出 直到结尾
{
insertTime = (char *)(_bstr_t)m_pRecordset->GetCollect("sCardID");//方法一;
//m_pRecordset->Fields->GetItem(_variant_t("sCardID"))->Value);
std::cout << "\t" << insertTime << std::endl;
m_pRecordset->MoveNext();
}
}
catch (_com_error &e)
{
std::cout << e.Description()<< std::endl;
}
}
protected:
_ConnectionPtr sqlSp=NULL;
_RecordsetPtr m_pRecordset;
char bueffer[256], serverip[256], password[256];
std::string insertTime;
};
void main()
{
cSQL mysql;
mysql.INIT("Sicent110");
system("pause");
}
谁能看下要怎么改呢 |