MFC連線SQLServer資料庫及運算元據庫資訊

2021-07-05 21:31:27 字數 1572 閱讀 7157

個人部落格位址 www.tmbcode.com

mfc連線sqlserver資料庫及運算元據庫資訊

一、測試環境

作業系統:windowsxp

資料庫:sqlserver2005

ide:vc++6.0

二、初始化ado

在.h檔案中新增:

_connectionptrm_pconnection;

_recordsetptrm_prst;

在.cpp中新增函式:

coinitialize(null); 

hresulthrpc,hrpr;

hrpc=m_pconnection.createinstance(__uuidof(connection));

hrpr=m_prst.createinstance(__uuidof(recordset));

if(succeeded(hrpc)&& succeeded(hrpr))

returntrue;

returnfalse;

三、連線資料庫的函式

cstringtemp;

_variant_tvar;

cstringsql;

sql.format("driver=;server=test;database=test;uid=sa;pwd=123456");

trycatch(_com_errore)

returnfalse;

returntrue;

四、建立連線(在initinstance()中新增)

//初始化ado

returnfalse;

//連線生產資料庫

if(!this->connec***bdatabase())

afxmessagebox("連線生產資料庫失敗",mb_ok,0);

returnfalse;

五、運算元據庫資訊

bool ***::getorderlist()//查詢和插入

cstringtemp;

_variant_tvar;

cstringsql;

trysql.format("select*  from table where name='test' andenable = 1");

temp=varianttocstring(var);

this->m_ctrclass.addstring(temp);

this->age=var.intval();

catch(_com_errore)

returnfalse;

sql.format("insertinto student (name,adress) values ('%s','%s')", \

this->name,this->adress);

trycatch(_com_errore)

messagebox("錄入student表失敗","提示",mb_iconhand);

returnfalse;

returntrue;

update和delete以此類推,這裡就不做詳細介紹了。

MFC通過ADO連線SQL SERVER資料庫

以乙個mfc的dialog應用程式為例。1.首先是引入msado15.dll。在stdafx.h中加入 import c program files common files system ado msado15.dll no namespace rename eof adoeof 2.初始化com...

MFC使用ADO連線SQL Server資料庫

1.首先,要用 import語句來引用支援ado的元件型別庫 tlb 其中型別庫可以作為可執行程式 dll exe等 的一部分被定位在其自身程式中的附屬資源裡,如 被定位在msado15.dll的附屬資源中,只需要直接用 import引用它既可。可以直接在stdafx.h檔案中加入下面語句來實現 i...

Linux C語言連線 sqlserver資料庫

記錄一下linux下使用c語言連線sqlserver的方法。連線前需要安裝freetds。參考 這個是從參考鏈結中的提取 分析一下,寫上注釋。include include include include include includeint main void else 連線資料庫 if dbus...