{
// TODO: Add your control notification handler code here
CString str1,str2;
CString str3 = "1";
int n = ::GetPrivateProfileString("test","test1","error",str1.GetBuffer(250),250,".\\test.ini");
str2.Format(str1,str3);
//AfxMessageBox(str2);
CoInitialize(NULL);
_ConnectionPtr sqlSp;
HRESULT hr=sqlSp.CreateInstance(_uuidof(Connection));
if(FAILED(hr))
{
AfxMessageBox("指针实例化失败 ");
return ;
}
else
{
try
{
_bstr_t strConnect="Driver={sql server};server=127.0.0.1,1433;uid=sa;pwd=1234567;database=DataCollection;";
sqlSp->Open(strConnect,"","",adModeUnknown);
}
catch(_com_error &e)
{
AfxMessageBox("打开数据库失败 !");
}
}
_RecordsetPtr m_pRecordset; //记录集对象指针,用来执行SQL语句并记录查询结果
if(FAILED(m_pRecordset.CreateInstance( _uuidof( Recordset ))))
{
AfxMessageBox("指针实例化失败 ");
return ;
}
m_pRecordset = sqlSp->Execute(_bstr_t(str2),NULL,adCmdText);
while(!m_pRecordset->adoEOF)
{
string UserName=(char*)(_bstr_t)(m_pRecordset->Fields->GetItem(_variant_t("UserName"))->Value);
string Password=(char*)(_bstr_t)(m_pRecordset->Fields->GetItem(_variant_t("Password"))->Value);
CString strname,strpass;
strname = UserName.c_str();
strpass = Password.c_str();
AfxMessageBox(strname +" " +strpass);
m_pRecordset->MoveNext();
}
}
文件中的格式:
[test]
test1 = select * from t_User where UserType = '%s'
或者
void CMfclofintestDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CString str1,str2;
int n = ::GetPrivateProfileString("test","test1","error",str1.GetBuffer(250),250,".\\test.ini");
n = ::GetPrivateProfileString("test","test2","error",str2.GetBuffer(250),250,".\\test.ini");
//str2.Format(str1,str3);
//AfxMessageBox(str2);
CoInitialize(NULL);
_ConnectionPtr sqlSp;
HRESULT hr=sqlSp.CreateInstance(_uuidof(Connection));
if(FAILED(hr))
{
AfxMessageBox("指针实例化失败 ");
return ;
}
else
{
try
{
_bstr_t strConnect="Driver={sql server};server=127.0.0.1,1433;uid=sa;pwd=1234567;database=DataCollection;";
sqlSp->Open(strConnect,"","",adModeUnknown);
}
catch(_com_error &e)
{
AfxMessageBox("打开数据库失败 !");
}
}
_RecordsetPtr m_pRecordset; //记录集对象指针,用来执行SQL语句并记录查询结果
if(FAILED(m_pRecordset.CreateInstance( _uuidof( Recordset ))))
{
AfxMessageBox("指针实例化失败 ");
return ;
}
m_pRecordset = sqlSp->Execute(_bstr_t(str2),NULL,adCmdText);
while(!m_pRecordset->adoEOF)
{
string UserName=(char*)(_bstr_t)(m_pRecordset->Fields->GetItem(_variant_t("UserName"))->Value);
string Password=(char*)(_bstr_t)(m_pRecordset->Fields->GetItem(_variant_t("Password"))->Value);
CString strname,strpass;
strname = UserName.c_str();
strpass = Password.c_str();
AfxMessageBox(strname +" " +strpass);
m_pRecordset->MoveNext();
}
}
文件格式:
[test]
test1 = select * from t_User where UserName = 'admin'
test2 = select * from t_User where UserType = '1'