VC 連線SQL資料庫,並獲取相應查詢內容

2021-06-18 20:33:39 字數 1803 閱讀 3685

前幾天用vc++,獲取sql中的內容並儲存,由於不是很熟悉,走了很多彎路,現在總結哈,希望能給需要的人一些幫助。

直接貼**。

#include "string"

#include "map"

//連線sql引用的

#import "c:\program files\common files\system\ado\msado15.dll"no_namespace rename("eof","adoeof")

using namespace std;

//你自己定義的函式

void ***dlg::yourfun()

;server=資料庫電腦ip位址(比如本機127.0.0.1),1433;database=資料庫名稱;

//name = 資料庫名字

//pwd = 你的資料庫密碼

//filepath1 = f:/

//filepath2 = .txt

cstring database;

getprivateprofilestring("info", "database", "", database.getbuffer(max_path), max_path, filepath);

//將獲取的資料庫鏈結字段,轉換成指定格式

_bstr_t strconnect = database;

//資料庫連線

try

} catch(_com_error e)

tryelse

欄位2result = m_precordset->getcollect("欄位2");

cstring 欄位2results;

欄位2results=(lpcstr)_bstr_t(欄位2result);

pfile->seektoend();//先定位到檔案尾部

pfile->writestring(欄位2results);//寫入檔案

pfile->writestring("\t");//每個字段內容用talbe鍵分開

欄位3result = m_precordset->getcollect("欄位3");

cstring 欄位3results;

欄位3results=(lpcstr)_bstr_t(欄位3result);

pfile->seektoend();//先定位到檔案尾部

pfile->writestring(欄位3results);

pfile->writestring("\t");

欄位4result = m_precordset->getcollect("欄位4");

cstring 欄位4results;

欄位4results=(lpcstr)_bstr_t(欄位4result);

pfile->seektoend();//先定位到檔案尾部

pfile->writestring(欄位4results);

pfile->writestring("\r\n");//每條記錄一行,記錄完換行

m_precordset->movenext(); //記錄完一條記錄,指向下一條記錄

} //迴圈關閉開啟的檔案指標

map>::const_iterator map_it = filename.begin();

while(map_it!=filename.end())

filename.clear();

//messagebox(s);

} catch(_com_error e)

}

才疏學淺,如有不對,望海涵,哈哈。

VC 連線sql資料庫

include iostream.h 在vc 中使用ado開發資料庫之前,需要匯入ado類資料庫 import c program files common files system ado msado15.dll no namespace rename eof endoffile int main...

VC 與SQL資料庫連線

以下是正確的連線方法 類adoconn負責連線sql資料庫 vc sql server資料庫應用系統開發與例項提供!import c program files common files system ado msado15.dll no namespace rename eof adoeof re...

VC 資料庫連線

1.建立基於對話方塊的mfc應用程式 放置乙個list box和乙個按鈕 2.在stdafx.h中加入 import c program files common files system ado msado15.dll no namespace rename eof rseof 3.在按鈕觸發事件...