UpdateData();
CoInitialize(NULL);
_ConnectionPtr pConn(__uuidof(Connection));
_RecordsetPtr pRst(__uuidof(Recordset));
pConn->ConnectionString ="Provider=SQLOLEDB.1;Password=12345sa12345;Persist Security Info=True;User ID=sa;Initial Catalog=DBLAB;Data Source=STAR-PC;";
pConn->Open ("","","",adConnectUnspecified);
//cstring.format
CString str = "select * from dbo.userinfo where userName = '";
str += m_name;
str += "' and userPwd = '";
str += m_pw;
str += "'";
//_bstr_t pipei = (_bstr_t)str;
//pRst=pConn->Execute(pipei,NULL,adCmdText);
pRst=pConn->Execute((_bstr_t)str,NULL,adCmdText);
if(!pRst->rsEOF)//pRst->rsEOF的值判定是否到达结尾,到达则为真,未到达则为假
{
CDialog::OnOK();
CIndex dlg;
dlg.m_whose = "当前登录用户为:" + m_name;
dlg.DoModal();
}
else
MessageBox("登录失败!");
pRst->Close();
pConn->Close();
pRst.Release();
pConn.Release();
CoUninitialize();
涉及取SQL的内容,如下:
CoInitialize(NULL);
_ConnectionPtr pConn(__uuidof(Connection));
_RecordsetPtr pRst(__uuidof(Recordset));
pConn->ConnectionString ="Provider=SQLOLEDB.1;Password=12345sa12345;Persist Security Info=True;User ID=sa;Initial Catalog=TestData1;Data Source=localhost;";
pConn->Open ("","","",adConnectUnspecified);
pRst=pConn->Execute("select * from dbo.Products ",NULL,adCmdText);
while(!pRst->rsEOF)
{
_bstr_t lpcstrlxx = (_bstr_t)pRst->GetCollect ("ProductName");//pRst->GetCollect强制为_bstr_t
((CListBox*)GetDlgItem(IDC_LIST1))->AddString(lpcstrlxx);
//((CListBox*)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect ("ProductName"));
_bstr_t strb = (_bstr_t)pRst->GetCollect ("ProductName");
CString strlxx = (LPCTSTR)strb;
MessageBox(strlxx);
pRst->MoveNext ();
}
CString stre1 = "e2";
CString strInsert1 = "insert into dbo.Products (ProductID,ProductName,Price) values ('727','";
strInsert1 += stre1;
strInsert1 += "','2303')";
_bstr_t Insert1 = (_bstr_t)strInsert1;//ProductID是主键,不能重复,会奔溃!
pConn->Execute(Insert1,NULL,adCmdText);
MessageBox("yes!");
pRst->Close ();
pConn->Close ();
pRst.Release ();
pConn.Release ();
CoUninitialize();