C++連接SQL數(shù)據(jù)庫分步驟進(jìn)行
每一種編程語言在應(yīng)用于程序開發(fā)中時(shí)都會有許多方法應(yīng)用于數(shù)據(jù)庫的操作。由于網(wǎng)上很多關(guān)于C++連接SQL數(shù)據(jù)庫的一些應(yīng)用沒有詳細(xì)的說明和完整的解決方法,所以我個(gè)人總結(jié)了一下。 另外由于本人能力有限,所以所寫內(nèi)容可能存在缺陷。#t#
C++連接SQL數(shù)據(jù)庫***步 系統(tǒng)配置
1.設(shè)置SQLSERVER服務(wù)器為SQL登錄方式,并且系統(tǒng)安全性中的sa用戶要設(shè)置登錄功能為“啟用”,還有必須要有密碼。
2.需要在ODBC中進(jìn)行數(shù)據(jù)源配置,數(shù)據(jù)源選\”SQL SERVER”,登錄方式使用“使用輸入用戶登錄ID和密碼的SQL SERVER驗(yàn)證”,并填寫登錄名(sa)和密碼,注意一點(diǎn),密碼不能為空,這就意味著你的sa用戶必須得有密碼。否則無法通過系統(tǒng)本身的安全策略。測試通過就完成了配置。
C++連接SQL數(shù)據(jù)庫第二步 C++與SQL連接初始化
1.在你所建立的C++項(xiàng)目中的stdafx.h頭文件中引入ADO
具體代碼如下
- #import “c:\Program Files\Common Files\System\ado\msado15.dll”
no_namespace rename(”EOF”, “adoEOF”) rename(”BOF”, “adoBOF”)
2.定義_ConnectionPtr變量后調(diào)用Connection對象的Open方法建立與服務(wù)器的連接。
數(shù)據(jù)類型_ConnectionPtr實(shí)際上是由類模板_com_ptr_t得到的一個(gè)具體的實(shí)例類。_ConnectionPtr類封裝了Connection對象的Idispatch接口指針及其一些必要的操作。可以通過這個(gè)指針操縱Connection對象。
例如連接SQLServer數(shù)據(jù)庫,代碼如下:
- //連接到MS SQL Server
- //初始化指針
- _ConnectionPtr pMyConnect = NULL;
- HRESULT hr = pMyConnect.CreateInstance(__uuidof(Connection));
- if (FAILED(hr))
- return;
- //初始化鏈接參數(shù)
- _bstr_t strConnect = “Provider=SQLOLEDB;
- Server=hch;
- Database=mytest;
- uid=sa; pwd=sa;”; //Database指你系統(tǒng)中的數(shù)據(jù)庫
- //執(zhí)行連接
- try
- {
- // Open方法連接字串必須四BSTR或者_(dá)bstr_t類型
- pMyConnect->Open(strConnect, “”, “”, NULL);
- }
- catch(_com_error &e)
- {
- MessageBox(e.Description(), “警告”, MB_OK|MB_ICONINFORMATION);
- }//發(fā)生鏈接錯(cuò)誤
C++連接SQL數(shù)據(jù)庫第三步 簡單的數(shù)據(jù)連接
- //定義_RecordsetPtr變量,調(diào)用它Recordset對象的Open,即可打開一個(gè)數(shù)據(jù)集
- //初始化過程 以下是個(gè)實(shí)例
- _RecordsetPtr pRecordset;
- if (FAILED(pRecordset.CreateInstance(__uuidof(Recordset))))
- {
- return;
- }
- //執(zhí)行操作
- try
- {
- pRecordset->Open(_variant_t(”userinfo”),
_variant_t((IDispatch*)pMyConnect),- adOpenKeyset, adLockOptimistic, adCmdTable);
- }
- catch (_com_error &e)
- {
- MessageBox(”無法打開userinfo表\”, “系統(tǒng)提示”,
MB_OK|MB_ICONINFORMATION);- }
C++連接SQL數(shù)據(jù)庫第四步 執(zhí)行SQL語句
這里是關(guān)鍵,我認(rèn)為只要你懂點(diǎn)SQL語句那么一切都會方便許多比用上面的方法簡單,更有效率點(diǎn)。
首先
- m_pConnection.CreateInstance(_uuidof(Connection));
//初始化Connection指針- m_pRecordset.CreateInstance(__uuidof(Recordset));
//初始化Recordset指針- CString strSql=”select * from tb_goods”;//具體執(zhí)行的SQL語句
- m_pRecordset=m_pConnection->Execute(_bstr_t(strSql),
NULL, adCmdText);//將查詢數(shù)據(jù)導(dǎo)入m_pRecordset數(shù)據(jù)容器
至此 你的SQL語句已經(jīng)執(zhí)行完成了m_pRecordset內(nèi)的數(shù)據(jù)就是你執(zhí)行的結(jié)果。
取得記錄:
- while(!m_pRecordset->adoEOF)//遍歷并讀取name列的記錄并輸出
- {
- CString temp = (TCHAR *)(_bstr_t)m_pRecordset->GetFields()->GetItem
- (”name”)->Value;
- AfxMessageBox(temp);
- pRecordset->MoveNext();
- }
插入記錄
- //記得初始化指針再執(zhí)行以下操作
- CString strsql;
- strsql.Format(”insert into tb_goods(no,name, price)
values(’%d’,'%s’, %d)”,m_intNo,m_strName,m_intPrice);- m_pRecordset=m_pConnection->
Execute(_bstr_t(strsql),NULL,adCmdText);
修改記錄
- CString strsql;
- strsql.Format(”update tb_goods set name=’%s’ ,
price=%d where no=%d “,m_strName,m_intPrice,m_intNo);- m_pRecordset=m_pConnection->Execute(_bstr_t(strsql),NULL,adCmdText);
刪除記錄
- CString strsql;
- strsql.Format(”delete from tb_goodswhere no= ‘%d’ “,m_intNo);
- m_pRecordset=m_pConnection->Execute(_bstr_t(strsql),NULL,adCmdText);
以上是幾個(gè)常用的SQL語句和執(zhí)行方法。效率可能不是很高,不過很容易理解。如果你對SQL語句很熟悉那么可以更有效的執(zhí)行查詢直接獲得需要的記錄。C++連接SQL數(shù)據(jù)庫的相關(guān)方法就為大家介紹到這里。