C++操作 SQL數據庫 實例 代碼步驟


C++連接SQL數據庫第一步 系統配置

1.設置SQLSERVER服務器為SQL登錄方式,並且系統安全性中的sa用戶要設置登錄功能為“啟用”,還有必須要有密碼。

2.需要在ODBC中進行數據源配置,數據源選\”SQL SERVER”,登錄方式使用“使用輸入用戶登錄ID和密碼的SQL SERVER驗證”,並填寫登錄名(sa)和密碼,注意一點,密碼不能為空,這就意味着你的sa用戶必須得有密碼。否則無法通過系統本身的安全策略。測試通過就完成了配置。

C++連接SQL數據庫第二步 C++與SQL連接初始化

1.在你所建立的C++項目中的stdafx.h頭文件中引入ADO

具體代碼如下

  1. #import “c:\Program Files\Common Files\System\ado\msado15.dll”  no_namespace rename(”EOF”, “adoEOF”) rename(”BOF”, “adoBOF”) 

2.定義_ConnectionPtr變量后調用Connection對象的Open方法建立與服務器的連接。

數據類型_ConnectionPtr實際上是由類模板_com_ptr_t得到的一個具體的實例類。_ConnectionPtr類封裝了Connection對象的Idispatch接口指針及其一些必要的操作。可以通過這個指針操縱Connection對象。

例如連接SQLServer數據庫,代碼如下:

  1. //連接到MS SQL Server  
  2. //初始化指針  
  3. _ConnectionPtr pMyConnect = NULL;  
  4. HRESULT hr = pMyConnect.CreateInstance(__uuidof(Connection));  
  5. if (FAILED(hr))  
  6. return;  
  7. //初始化鏈接參數  
  8. _bstr_t strConnect = “Provider=SQLOLEDB
  9. Server=hch
  10. Database=mytest
  11. uid=sapwd=sa;”; //Database指你系統中的數據庫  
  12. //執行連接  
  13. try  
  14. {  
  15. // Open方法連接字串必須四BSTR或者_bstr_t類型  
  16. pMyConnect->Open(strConnect, “”, “”, NULL);  
  17. }  
  18. catch(_com_error &e)  
  19. {  
  20. MessageBox(e.Description(), “警告”, MB_OK|MB_ICONINFORMATION);  
  21. }//發生鏈接錯誤 

 

C++連接SQL數據庫第三步 簡單的數據連接

  1. //定義_RecordsetPtr變量,調用它Recordset對象的Open,即可打開一個數據集  
  2. //初始化過程 以下是個實例  
  3. _RecordsetPtr pRecordset;  
  4. if (FAILED(pRecordset.CreateInstance(__uuidof(Recordset))))  
  5. {  
  6. return;  
  7. }  
  8. //執行操作  
  9. try  
  10. {  
  11. pRecordset->Open(_variant_t(”userinfo”),  _variant_t((IDispatch*)pMyConnect),  
  12. adOpenKeyset, adLockOptimistic, adCmdTable);  
  13. }  
  14. catch (_com_error &e)  
  15. {  
  16. MessageBox(”無法打開userinfo表\”, “系統提示”,  MB_OK|MB_ICONINFORMATION);  

 

C++連接SQL數據庫第四步 執行SQL語句

這里是關鍵,我認為只要你懂點SQL語句那么一切都會方便許多比用上面的方法簡單,更有效率點。

首先

  1. m_pConnection.CreateInstance(_uuidof(Connection));  //初始化Connection指針  
  2. m_pRecordset.CreateInstance(__uuidof(Recordset)); //初始化Recordset指針  
  3. CString strSql=”select * from tb_goods”;//具體執行的SQL語句  
  4. m_pRecordset=m_pConnection->Execute(_bstr_t(strSql),  NULL, adCmdText);//將查詢數據導入m_pRecordset數據容器 

至此 你的SQL語句已經執行完成了m_pRecordset內的數據就是你執行的結果。

取得記錄:

  1. while(!m_pRecordset->adoEOF)//遍歷並讀取name列的記錄並輸出  
  2. {  
  3. CString temp = (TCHAR *)(_bstr_t)m_pRecordset->GetFields()->GetItem  
  4. (”name”)->Value;  
  5. AfxMessageBox(temp);  
  6. pRecordset->MoveNext();  

 

插入記錄

  1. //記得初始化指針再執行以下操作  
  2. CString strsql;  
  3. strsql.Format(”insert into tb_goods(no,name, price)  values(’%d’,'%s’, %d)”,m_intNo,m_strName,m_intPrice);  
  4. m_pRecordset=m_pConnection-> Execute(_bstr_t(strsql),NULL,adCmdText); 

 

修改記錄

  1. CString strsql;  
  2. strsql.Format(”update tb_goods set name=’%s’ ,  price=%d where no=%d “,m_strName,m_intPrice,m_intNo);   
  3. m_pRecordset=m_pConnection->Execute(_bstr_t(strsql),NULL,adCmdText); 

 

刪除記錄

  1. CString strsql;  
  2. strsql.Format(”delete from tb_goodswhere no= ‘%d’ “,m_intNo);  
  3. m_pRecordset=m_pConnection->Execute(_bstr_t(strsql),NULL,adCmdText); 

 

以上是幾個常用的SQL語句和執行方法。效率可能不是很高,不過很容易理解。如果你對SQL語句很熟悉那么可以更有效的執行查詢直接獲得需要的記錄。C++連接SQL數據庫的相關方法就為大家介紹到這里。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM