新聞中心
每一種編程語言在應(yīng)用于程序開發(fā)中時(shí)都會有許多方法應(yīng)用于數(shù)據(jù)庫的操作。由于網(wǎng)上很多關(guān)于C++連接SQL數(shù)據(jù)庫的一些應(yīng)用沒有詳細(xì)的說明和完整的解決方法,所以我個人總結(jié)了一下。 另外由于本人能力有限,所以所寫內(nèi)容可能存在缺陷。#t#

皮山網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),皮山網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為皮山1000多家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)營銷網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的皮山做網(wǎng)站的公司定做!
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得到的一個具體的實(shí)例類。_ConnectionPtr類封裝了Connection對象的Idispatch接口指針及其一些必要的操作??梢酝ㄟ^這個指針操縱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ā)生鏈接錯誤
C++連接SQL數(shù)據(jù)庫第三步 簡單的數(shù)據(jù)連接
- //定義_RecordsetPtr變量,調(diào)用它Recordset對象的Open,即可打開一個數(shù)據(jù)集
- //初始化過程 以下是個實(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);
以上是幾個常用的SQL語句和執(zhí)行方法。效率可能不是很高,不過很容易理解。如果你對SQL語句很熟悉那么可以更有效的執(zhí)行查詢直接獲得需要的記錄。C++連接SQL數(shù)據(jù)庫的相關(guān)方法就為大家介紹到這里。
當(dāng)前文章:C++連接SQL數(shù)據(jù)庫分步驟進(jìn)行
當(dāng)前網(wǎng)址:http://m.fisionsoft.com.cn/article/dpohihd.html


咨詢
建站咨詢
