大連實訓之基於MFC對資料庫的基本操作(功能實現)

2021-06-16 23:41:04 字數 3221 閱讀 8271

一、建立資料庫檔案與mfc的聯絡

1.建立乙個資料庫檔案,並建立乙個名為student1的表

2.開始->設定->控制面板->管理工具->資料來源->新增——

3.在類檢視中新建mfc類,基類選取為:cbaorecordset;確定,在彈出的對話方塊中選擇odbc,在對應下拉列表中選擇你新增的資料來源;

下方選擇dynaset(動態建立),然後選擇確定,在彈出的對話方塊中選擇你要使用的表,選擇ok

4.在所建的新類中新增標頭檔案:#include

具體操作截圖:

二、對資料庫內的內容進行編輯

1.format: 第乙個引數是格式化字串,就像printf的第乙個引數一樣,用%d表示int,%s表示char*,%u表示unsigned int,%hd表示short,%hu表示unsigned short,%hhd表示char,%hhu表示unsigned char,%f表示float等。後面的引數就是與格式化字串中每個字段對應的型別變數。

2.strfilter:用m_strfilter實現乙個查詢功能,表中的「欄位名」必須用括起來,比如表中有乙個欄位名為「姓名」,用strfilter表示「姓名="張三"」,須寫成 m_pset->m_strfilter="[姓名] like '張三'"; 而且單引號是必須要的(vc6.0)。

1.顯示資料庫中的內容

//accessbox 為資料庫類的名

//1.簡單的輸出一條資訊

cstring strtemp;

student.open();//資料庫開啟

strtemp.format("number : %s,name : %s,

english : %d,math : %d,

chinese : %d",student.m_number,

student.m_name,student.m_english,

student.m_math,student.m_chinese);

student.close();//資料庫關閉

//2. 拓展:全部顯示

accessbox student;

cclientdc dc(this);//dc指標

cstring strtemp;

int y=50;

student.open();

while(!(student.iseof()))

student.close();

}2.給資料庫新增新的記錄

accessbox student1;

student1.open();

student1.addnew();

student1.m_number="1006";

student1.m_name="jone";

student1.m_score=86;

student1.update();

student1.close();

3.修改資料庫中的內容

//1.簡單的將 number 是 1005 的記錄中的 name 改為 haijun

accessbox student;

student.m_strfilter="number='1005'";

student.open();

student.edit();

student.m_name="haijun";

student.update();

student.close();

//2.拓展,將 english = 90 的記錄中的 chinese 改為 85

accessbox student;

student.m_strfilter="english=90";

student.open();

while(!(student.iseof()))

student.edit();

student.m_chinese=85;

}student.update();

student.close();

4.刪除資料庫中的內容

//1.簡單的將資料庫中的內容全部刪除

accessbox student;

student.m_strfilter="number='1005'";

student.open();

student.delete();

student.m_name;

student.update();

student.close();

//2.拓展:將 chinese=85 的記錄刪除

accessbox student ;

student.m_strfilter="chinese=85";

student.open();

while(!(student.iseof()))

student.delete();

student.m_number;

student.m_name;

student.m_english;

student.m_math;

student.m_chinese;

} student.update();

student.close();

oracle資料庫實訓(1)

用的是11g版本 解鎖 alter user scott account unlock 設定密碼 alter user scott identified by tiger 授權 grant dba to scott用scott使用者登入,利用已有的表進行實驗 查詢語句 1.員工的實發工資 nvl x...

MFC的CRECORDSET對資料庫的操作

mfc資料庫操作系列 資料中間層 crecordset mfc資料庫介面分為兩種 odbc和ole db odbc而言提供開放的訪問方式,使用較為簡單,但是需要註冊資料庫,這導致在部署應用程式的時候需要重新布置資料庫,並且個人意見 資料庫操作不涉及大量的資料交換建議使用。crecordset為資料庫...

基於Python對資料庫的操作

conn pymysql.connect host 127.0.0.1 port 3306,user root password database wangyi cur conn.cursor pymysql.cursors.dictcursor 這裡的cur指的是游標。游標是對映在結果集中一行資料...