ADO方式連線SQL server 2008 R2資料庫 MFC
阿新 • • 發佈:2020-11-19
ADO (ActiveX Data Objects,ActiveX資料物件)是Microsoft提出的應用程式介面 API 用以實現訪問關係或非關係資料庫中的資料。
ADO 是一項微軟的技術
ADO 指 ActiveX 資料物件(ActiveX Data Objects)
ADO 是一個微軟的 Active-X 元件
ADO 會隨微軟的 IIS 被自動安裝
ADO 是一個訪問資料庫中資料的程式設計介面
1 //放在.h檔案中例如 stdafx.h 2 //#import "C://Program Files//Common Files//System//ado//msado15.dll" no_namespace rename("EOF","adoEOF")rename("BOF","adoBOF") 3 4 //這樣寫是因為上面那句有編譯錯誤 5 #pragma warning(disable:4146) 6 #import "C:/Program Files/Common Files/System/ADO/msado15.dll" named_guids rename("EOF","adoEOF"), rename("BOF","adoBOF") 7 #pragma warning(default:4146) 8 using namespace ADODB; 9 10 //新增一個指向Connection物件的指標 11 _ConnectionPtr m_pConnection; 12 //新增一個指向Recordset物件的指標 13 _RecordsetPtr m_pRecordset;
1 // 初始化—連線資料庫 2 bool InitADOCOM() 3 { 4 // 初始化OLE/COM庫環境 5 ::CoInitialize(NULL); 6 try 7 { 8 // 建立Connection物件 9 m_pConnection.CreateInstance("ADODB.Connection"); 10 // 設定連線字串,必須是BSTR型或者_bstr_t型別 11 CString sTemp; 12 //資料庫IP 資料庫名字 登入ID 登入密碼 13 sTemp.Format(_T("Provider=SQLOLEDB.1; Server=%s;Database=%s; uid=%s;pwd=%s;"), m_strServer, m_strDatabase, m_strUserID, m_strPassword); 14 _bstr_t strConnect = sTemp; 15 m_pConnection->Open(strConnect, "", "", adModeUnknown); 16 return TRUE; 17 } 18 // 捕捉異常 19 catch(_com_error e) 20 { 21 // 顯示錯誤資訊 22 AfxMessageBox(e.Description()); 23 return FALSE; 24 } 25 }
1 //呼叫 2 BOOL bRet; 3 bRet = InitADOCOM(); 4 if(bRet) 5 { 6 MessageBox(_T("資料庫連線成功")); 7 } 8 else 9 { 10 MessageBox(_T("資料庫連線失敗")); 11 }
1 //執行查詢 2 _RecordsetPtr& GetRecordSet(_bstr_t bstrSQL) 3 { 4 try 5 { 6 //重新連線資料庫 7 if(m_pConnection->GetState() != adStateOpen) 8 { 9 OnInitADOCOM(); 10 } 11 //建立記錄集物件 12 m_pRecordset.CreateInstance(__uuidof(Recordset)); 13 //取得表中的記錄 14 m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic, adLockOptimistic, adCmdText); 15 } 16 // 捕捉異常 17 catch(_com_error e) 18 { 19 // 顯示錯誤資訊 20 AfxMessageBox(e.Description()); 21 } 22 // 返回記錄集 23 return m_pRecordset; 24 }
1 _bstr_t strSelect = "select * from testTable"; 2 GetRecordSet(strSelect); 3 CString qqqq, q123; 4 while (!m_pRecordset->adoEOF) 5 { 6 qqqq = (_bstr_t)(m_pRecordset->Fields->GetItem(_variant_t("qwer"))->Value);//欄位名 7 q123 = (_bstr_t)(m_pRecordset->Fields->GetItem(_variant_t("q123"))->Value);//欄位名 8 //MessageBox(qqqq); 9 //MessageBox(q123); 10 m_pRecordset->MoveNext(); 11 }
1 //查詢並取出資料 2 void select() 3 { 4 // TODO: 在此新增控制元件通知處理程式程式碼 5 try 6 { 7 m_pRecordset.CreateInstance("ADODB.Recordset"); 8 m_pRecordset->Open("select * from testTable", 9 _variant_t((IDispatch*)m_pConnection, true), adOpenStatic, adLockOptimistic, adCmdText); 10 } 11 catch (_com_error &e) 12 { 13 AfxMessageBox(e.Description()); 14 } 15 try 16 { 17 CString strCol1, strCol2; 18 while (!m_pRecordset->adoEOF) 19 { 20 //按照行來迴圈,取每個欄位的資料 21 strCol1 = (_bstr_t)(m_pRecordset->Fields->GetItem(_variant_t("qwer"))->Value); 22 strCol2 = (_bstr_t)(m_pRecordset->Fields->GetItem(_variant_t("q123"))->Value); 23 m_pRecordset->MoveNext(); 24 } 25 } 26 catch (_com_error &e) 27 { 28 AfxMessageBox(e.Description()); 29 } 30 }
1 // 執行SQL語句 2 BOOL ExecuteSQL(_bstr_t bstrSQL) 3 { 4 try 5 { 6 //重新連線資料庫 7 if(m_pConnection->GetState() != adStateOpen) 8 { 9 OnInitADOCOM(); 10 } 11 // Connection物件的Execute方法:(_bstr_t CommandText, 12 // VARIANT * RecordsAffected, long Options ) 13 // 其中CommandText是命令字串,通常是SQL命令。 14 // 引數RecordsAffected是操作完成後所影響的行數, 15 // 引數Options表示CommandText的型別:adCmdText-文字命令;adCmdTable-表名 16 // adCmdProc-儲存過程;adCmdUnknown-未知 17 m_pConnection->Execute(bstrSQL, NULL, adCmdText); 18 return TRUE; 19 } 20 catch(_com_error e) 21 { 22 AfxMessageBox(e.Description()); 23 return FALSE; 24 } 25 }
_bstr_t strSQL = "delete from testTable where qwer = 'fred'";//不區分大小寫 BOOL bRet = ExecuteSQL(strSQL); if(bRet) { MessageBox(_T("執行成功")); }else { MessageBox(_T("執行失敗")); }
1 //獲取某個表的所有欄位名 2 void GetAllColumnNames(CString strTableName) 3 { 4 CStringArray columnNamesArray; 5 FieldPtr pField; 6 CString strColName = _T(""); 7 CString strSql = _T(""); 8 _bstr_t vSQL = _T(""); 9 strSql.Format(_T("SELECT * FROM %s"), strTableName); 10 vSQL = strSql; 11 //執行這個查詢語句 12 _RecordsetPtr pRecordset = GetRecordSet(vSQL); 13 FieldsPtr pFields = pRecordset->GetFields(); 14 colNamesArray.RemoveAll(); 15 for (int i = 0;i < pFields->Count; i++) 16 { 17 pField = pFields->GetItem(long(i)); 18 strColName = (LPCTSTR)(_bstr_t)pField->Name; 19 columnNamesArray.Add(strColName); 20 } 21 }
1 //獲取資料庫中的所有表名 2 void GetAllTableNames() 3 { 4 CString strTableName; 5 CString strSql; 6 _bstr_t vSQL; 7 strSql = "select * from sysobjects"; 8 vSQL = strSql; 9 //執行這個查詢語句 10 _RecordsetPtr pRecordset = GetRecordSet(vSQL); 11 CStringArray TableNamesArray; 12 while(!pRecordset->adoEOF) 13 { 14 strTableName = (LPCTSTR)(_bstr_t)pRecordset->GetCollect("name"); 15 TableNamesArray.Add(strTableName); 16 pRecordset->MoveNext(); 17 } 18 }
1 // 關閉記錄集和連線 2 void ExitConnect() 3 { 4 if (m_pRecordset != NULL) 5 { 6 m_pRecordset->Close(); 7 m_pRecordset->Release(); 8 m_pRecordset = NULL; 9 } 10 if (m_pConnection->GetState() == adStateOpen) 11 { 12 m_pConnection->Close(); 13 } 14 if (m_pConnection != NULL) 15 { 16 m_pConnection.Release(); 17 m_pConnection = NULL; 18 } 19 // 釋放環境 20 ::CoUninitialize(); 21 }
//不要多次關閉 ExitConnect();