首页
/
每日頭條
/
科技
/
vc怎麼設置數據庫
vc怎麼設置數據庫
更新时间:2026-02-16 16:56:41

vc怎麼設置數據庫?目錄1 新建數據庫,今天小編就來說說關于vc怎麼設置數據庫?下面更多詳細答案一起來看看吧!

vc怎麼設置數據庫(VCADO數據庫)1

vc怎麼設置數據庫

目錄

1 新建數據庫

2 添加和編輯對話框資源

3 初始化OLE/COM庫

4 創建Connection對象、打開數據源,建立同數據源的連接

5 執行SQL命令、使用結果集

6 終止連接

1 新建數據庫

新建一簡單Access數據庫student.mdb,内有一studentinfo表。

2 添加和編輯對話框資源

創建一個基于對話框的MFC工程,工程名為Ch15Demo1。

單選按鈕“添加記錄”的屬性需要勾選“組”才可以關聯變量,如下:

對話框中的控件變量如下:

“添加記錄”按鈕默認為選中狀态,代碼如下:

CCh15Demo1Dlg::CCh15Demo1Dlg(CWnd* pParent /*=NULL*/)

: CDialog(CCh15Demo1Dlg::IDD, pParent)

{

//{{AFX_DATA_INIT(CCh15Demo1Dlg)

m_StuNo = _T("");

m_StuName = _T("");

m_StuAddress = _T("");

m_StuTel = _T("");

m_Status = _T("");

m_radio = 0;//添加記錄單選按鈕選中

//}}AFX_DATA_INIT

m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}

初始化其它按鈕狀态:

BOOL CCh15Demo1Dlg::OnInitDialog()

{

CDialog::OnInitDialog();

...

GetDlgItem(IDC_REMOVE)->EnableWindow(false);//禁用删除按鈕

GetDlgItem(IDC_CHECKIN)->EnableWindow(false);//禁用提交按鈕

//禁用導航按鈕控件

GetDlgItem(IDC_FIRST)->EnableWindow(false);//禁用第一條按鈕

GetDlgItem(IDC_NEXT)->EnableWindow(false);//禁用下一條按鈕

GetDlgItem(IDC_PREV)->EnableWindow(false);//禁用上一條按鈕

GetDlgItem(IDC_LAST)->EnableWindow(false);//禁用最後一條按鈕

return TRUE;

}

3 初始化OLE/COM庫

ADO是一組動态鍊接庫,因此在使用之前還必須導入ADO并且初始化。

// stdafx.h : include file for standard system include files,

#import "C:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")

// Ch15Demo1.cpp : implementation file

BOOL CCh15Demo1App::InitInstance()

{

AfxEnableControlContainer();

//初始化OLE DLLs

if (!AfxOleInit())

{

AfxMessageBox("初始化OLE DLL失敗!");

return FALSE;

}

...

}

4 創建Connection對象,打開數據源,建立同數據源的連接

創建一個Connection對象,定義用于連接的字符串信息,包括數據源名稱、用戶ID、口令、連接超時、默認數據庫以及光标的位置。一個Connection對象代表了同數據源的一次會話。

在頭文件CH15Demo1Dlg.h中定義一處指向Connection對象的指針:

// Ch15Demo1Dlg.h : header file

class CCh15Demo1Dlg : public CDialog

{

public:

_ConnectionPtr m_pConnection;//連接對象

_RecordsetPtr m_pRecordset; //記錄集對象

...

}

在文件Ch15Demo1Dlg.cpp的OnInitDialog()中通過Connection對象連接創建的數據庫Student.mdb:

// Ch15Demo1Dlg.cpp : implementation file

BOOL CCh15Demo1Dlg::OnInitDialog()

{

CDialog::OnInitDialog();

...

HRESULT hr;

try

{

hr = m_pConnection.CreateInstance("ADODB.Connection");//創建Connection對象

if(SUCCEEDED(hr))

{

hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Student.mdb","","",adModeUnknown);///連接數據庫

//hr = m_pConnection->Open("studb","","",adModeUnknown);

}

}

catch(_com_error e)///捕捉異常

{

CString errormessage;

errormessage.Format("連接數據庫失敗!\r\n錯誤信息:%s",e.ErrorMessage());

AfxMessageBox(errormessage);///顯示錯誤信息

}

_variant_t RecordsAffected;

return TRUE;

}

5 執行SQL命令、使用結果集。

5.1 自定義一個顯示記錄的函數

// Ch15Demo1Dlg.h : header file

class CCh15Demo1Dlg : public CDialog

{

protected:

void ShowRecord();//顯示記錄

}

// Ch15Demo1Dlg.cpp : implementation file

void CCh15Demo1Dlg::ShowRecord()

{

m_StuNo=(char*)_bstr_t(m_pRecordset->GetCollect("stuNo"));

m_StuName=(char*)_bstr_t(m_pRecordset->GetCollect("stuname"));

m_StuAddress=(char*)_bstr_t(m_pRecordset->GetCollect("stuaddress"));

m_StuTel=(char*)_bstr_t(m_pRecordset->GetCollect("stuphone"));

UpdateData(false);

}

5.2 各控件消息響應函數

先在頭文件中定義公共指針和變量

// Ch15Demo1Dlg.h : header file

class CCh15Demo1Dlg : public CDialog

{

public:

_ConnectionPtr m_pConnection;//連接對象

_RecordsetPtr m_pRecordset; //記錄集對象

int count;//記錄數

int curNo;//當前操作記錄

...

}

// Ch15Demo1Dlg.cpp : implementation file

void CCh15Demo1Dlg::OnRadio1()

{

// TODO: Add your control notification handler code here

m_radio=0;

GetDlgItem(IDC_ADD)->EnableWindow(true);//激活添加按鈕

GetDlgItem(IDC_REMOVE)->EnableWindow(false);//禁用删除按鈕

GetDlgItem(IDC_CHECKIN)->EnableWindow(false);//禁用提交按鈕

//禁用導航按鈕控件

GetDlgItem(IDC_FIRST)->EnableWindow(false);//禁用第一條按鈕

GetDlgItem(IDC_NEXT)->EnableWindow(false);//禁用下一條按鈕

GetDlgItem(IDC_PREV)->EnableWindow(false);//禁用上一條按鈕

GetDlgItem(IDC_LAST)->EnableWindow(false);//禁用最後一條按鈕

m_StuNo=m_StuName=m_StuAddress=m_StuTel="";

UpdateData(false);

}

void CCh15Demo1Dlg::OnRadio2()

{

// TODO: Add your control notification handler code here

m_radio=1;

GetDlgItem(IDC_ADD)->EnableWindow(false);//禁用添加按鈕

GetDlgItem(IDC_REMOVE)->EnableWindow(true);//激活删除按鈕

GetDlgItem(IDC_CHECKIN)->EnableWindow(false);//禁用提交按鈕

//激活導航按鈕控件

GetDlgItem(IDC_FIRST)->EnableWindow(true);//激活第一條按鈕

GetDlgItem(IDC_NEXT)->EnableWindow(true);//激活下一條按鈕

GetDlgItem(IDC_PREV)->EnableWindow(true);//激活上一條按鈕

GetDlgItem(IDC_LAST)->EnableWindow(true);//激活最後一條按鈕

_variant_t RecordsAffected;

m_pRecordset =m_pConnection->Execute("SELECT COUNT(*) FROM studentinfo",&RecordsAffected,adCmdText);

//&RecordsAffected是執行命令後的返回值,表明符合命令的的行數,可選參數

//adCmdTex指明執行命令的類型,可選參數

_variant_t vIndex = (long)0;

_variant_t vCount = m_pRecordset->GetCollect(vIndex); //取得第一個字段的值放入vCount變量

count=vCount.lVal;//獲取記錄集的

m_pRecordset->Close();///關閉記錄集

if(count==0)//記錄為0

{

GetDlgItem(IDC_REMOVE)->EnableWindow(false);//禁用删除按鈕

//禁用導航按鈕控件

GetDlgItem(IDC_FIRST)->EnableWindow(false);//禁用第一條按鈕

GetDlgItem(IDC_NEXT)->EnableWindow(false);//禁用下一條按鈕

GetDlgItem(IDC_PREV)->EnableWindow(false);//禁用上一條按鈕

GetDlgItem(IDC_LAST)->EnableWindow(false);//禁用最後一條按鈕

m_Status="表中沒有數據!";

UpdateData(false);

return;

}

m_pRecordset.CreateInstance(__uuidof(Recordset));//創建記錄集對象

CString strSQL="SELECT * FROM studentinfo";

try

{

//從數據庫中打開表

m_pRecordset->Open(_bstr_t(strSQL), m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);

}

catch (_com_error e)

{

CString strError;

strError.Format("警告:打開數據表時發生異常。錯誤信息:%s",e.ErrorMessage());

AfxMessageBox(strError);

return;

}

m_pRecordset->MoveFirst();//移到第一條記錄

curNo=1;

m_Status.Format("共有%d條記錄,當前第%d條",count,curNo);

UpdateData(false);

ShowRecord();//顯示記錄

}

void CCh15Demo1Dlg::OnRadio3()

{

// TODO: Add your control notification handler code here

m_radio=2;

GetDlgItem(IDC_ADD)->EnableWindow(false);//禁用添加按鈕

GetDlgItem(IDC_REMOVE)->EnableWindow(false);//禁用删除按鈕

GetDlgItem(IDC_CHECKIN)->EnableWindow(true);//激活提交按鈕

//激活導航按鈕控件

GetDlgItem(IDC_FIRST)->EnableWindow(true);//激活第一條按鈕

GetDlgItem(IDC_NEXT)->EnableWindow(true);//激活下一條按鈕

GetDlgItem(IDC_PREV)->EnableWindow(true);//激活上一條按鈕

GetDlgItem(IDC_LAST)->EnableWindow(true);//激活最後一條按鈕

_variant_t RecordsAffected;

m_pRecordset =m_pConnection->Execute("SELECT COUNT(*) FROM studentinfo",&RecordsAffected,adCmdText);

_variant_t vIndex = (long)0;

_variant_t vCount = m_pRecordset->GetCollect(vIndex); //取得第一個字段的值放入vCount變量

count=vCount.lVal;

m_pRecordset->Close();///關閉記錄集

if(count==0)//記錄為0

{

GetDlgItem(IDC_CHECKIN)->EnableWindow(false);//禁用提交按鈕

//禁用導航按鈕控件

GetDlgItem(IDC_FIRST)->EnableWindow(false);//禁用第一條按鈕

GetDlgItem(IDC_NEXT)->EnableWindow(false);//禁用下一條按鈕

GetDlgItem(IDC_PREV)->EnableWindow(false);//禁用上一條按鈕

GetDlgItem(IDC_LAST)->EnableWindow(false);//禁用最後一條按鈕

return;

}

m_pRecordset.CreateInstance(__uuidof(Recordset));

CString strSQL="SELECT * FROM studentinfo";

try

{

//從數據庫中打開表

m_pRecordset->Open(_bstr_t(strSQL), m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);

}

catch (_com_error e)

{

CString strError;

strError.Format("警告:打開數據表時發生異常。 錯誤信息: %s",e.ErrorMessage());

AfxMessageBox(strError);

return;

}

m_pRecordset->MoveFirst();//移到第一條記錄

curNo=1;

m_Status.Format("共有%d條記錄,當前第%d條",count,curNo);

UpdateData(false);

ShowRecord();//顯示記錄

}

void CCh15Demo1Dlg::OnAdd()

{

// TODO: Add your control notification handler code here

_variant_t RecordsAffected;

UpdateData();

if(m_StuNo.IsEmpty())

{

AfxMessageBox("學号不能為空!");

return;

}

CString strSQL;

strSQL.Format("INSERT INTO studentinfo(stuNo,stuname,stuaddress,stuphone) VALUES ('%s', '%s','%s','%s')",m_StuNo,m_StuName,m_StuAddress,m_StuTel);

m_pConnection->Execute((_bstr_t)strSQL,&RecordsAffected,adCmdText);

m_Status="成功添加了一行記錄!";

m_StuNo=m_StuName=m_StuAddress=m_StuTel="";

UpdateData(false);

}

//因為COM必須設計成跨平台,所以它需要一種更普遍的方式來處理字符串以及其他數據。在ADO技術實現對數據庫的操作時,數據庫中保存的都是COM類型的變量,其特有的數據類型為VARIANT和BSTR類型

//_variant_t類封裝和管理Variant數據類型,在使用ADO對象模型操作數據庫時,如果對象的方法或者屬性接受某個值,則聲明該值的形式為_variant_t,而非一般CString,二者可以強制轉換

void CCh15Demo1Dlg::OnCheckin()

{

// TODO: Add your control notification handler code here

UpdateData();

m_pRecordset->PutCollect("stuNo",_variant_t(m_StuNo));

m_pRecordset->PutCollect("stuname",_variant_t(m_StuName));

m_pRecordset->PutCollect("stuaddress",_variant_t(m_StuAddress));

m_pRecordset->PutCollect("stuphone",_variant_t(m_StuTel));

m_pRecordset->Update();//提交記錄

m_Status="成功修改了一條記錄!";

UpdateData(false);

}

void CCh15Demo1Dlg::OnRemove()

{

// TODO: Add your control notification handler code here

m_pRecordset->Delete(adAffectCurrent);

m_Status="成功删除了一行記錄!";

count=count-1;

UpdateData(false);

OnFirst();

}

//當得到記錄集時,需要從各條記錄中讀出數據,必須在記錄集中移動光标,使要訪問的記錄成為當前記錄。ADO的記錄集對象中提供了幾種在記錄集中移動記錄指針的方法(函數),使用這些方法可以方便地得到要訪問的記錄。

void CCh15Demo1Dlg::OnFirst()

{

// TODO: Add your control notification handler code here

if(count==0)//記錄為0

{

m_StuNo=m_StuName=m_StuAddress=m_StuTel="";

UpdateData(false);

GetDlgItem(IDC_REMOVE)->EnableWindow(false);//禁用删除按鈕

//禁用導航按鈕控件

GetDlgItem(IDC_FIRST)->EnableWindow(false);//禁用第一條按鈕

GetDlgItem(IDC_NEXT)->EnableWindow(false);//禁用下一條按鈕

GetDlgItem(IDC_PREV)->EnableWindow(false);//禁用上一條按鈕

GetDlgItem(IDC_LAST)->EnableWindow(false);//禁用最後一條按鈕

return;

}

m_pRecordset->MoveFirst();//移到第一條記錄

curNo=1;

m_Status.Format("共有%d條記錄,當前第%d條",count,curNo);

UpdateData(false);

ShowRecord();//顯示記錄

}

void CCh15Demo1Dlg::OnLast()

{

// TODO: Add your control notification handler code here

m_pRecordset->MoveLast();//移到最後一條記錄

curNo=count;

m_Status.Format("共有%d條記錄,當前第%d條",count,curNo);

UpdateData(false);

ShowRecord();//顯示記錄

}

void CCh15Demo1Dlg::OnNext()

{

// TODO: Add your control notification handler code here

if(curNo<count)

{

m_pRecordset->MoveNext();//移到下一條記錄

curNo ;

if(m_pRecordset->adoEOF)

{

m_pRecordset->MoveLast();

}

m_Status.Format("共有%d條記錄,當前第%d條",count,curNo);

UpdateData(false);

ShowRecord();//顯示記錄

}

}

void CCh15Demo1Dlg::OnPrev()

{

// TODO: Add your control notification handler code here

if(curNo>1)

{

m_pRecordset->MovePrevious();//移到上一條記錄

curNo--;

if(m_pRecordset->BOF)

{

m_pRecordset->MoveFirst();

}

m_Status.Format("共有%d條記錄,當前第%d條",count,curNo);

UpdateData(false);

ShowRecord();//顯示記錄

}

}

6 關閉記錄集和連接

使用ClassWizard,重載對話框類WM_DESTROY消息響應函數DestroyWindow(),并添加如下代碼:

BOOL CCh15Demo1Dlg::DestroyWindow()

{

// TODO: Add your specialized code here and/or call the base class

if(m_pRecordset!=NULL)

{

m_pRecordset->Close();//關閉記錄集對象

m_pRecordset=NULL;

}

if(m_pConnection->State)

{

m_pConnection->Close();//關閉連接對象

m_pConnection=NULL;

}

return CDialog::DestroyWindow();

}

-End-

Comments
Welcome to tft每日頭條 comments! Please keep conversations courteous and on-topic. To fosterproductive and respectful conversations, you may see comments from our Community Managers.
Sign up to post
Sort by
Show More Comments
推荐阅读
紅旗hs5怎麼挂檔
紅旗hs5怎麼挂檔
1、先踩住刹車,按着車鍵,車子打着後。接下來就要讓車走起來。2、接着還是要踩住刹車,接着右手大拇指要按住下圖紅色箭頭所指的那個按鍵。3、前進:按住紅色箭頭所指的按鍵,把檔位往下拉到D擋。4、後退:按住紅色箭頭所指的按鍵,把檔位往下拉到R擋。5、停車:先把檔位往下拉到N擋,在按P字駐車鍵。6、手動檔位駕駛模式:把檔位往下拉到M擋。
2026-02-16
steam怎麼修複遊戲
steam怎麼修複遊戲
演示機型:華為MateBookX系統版本:win10APP版本:steam2.10.91.911、在遊戲的名稱上點擊右鍵,在彈出的菜單中點擊屬性。2、在彈出的菜單中,選擇本地文件—驗證遊戲文件的完整性。3、然後就會彈出驗證文件的窗口,等待其驗證完成。遊戲文件越大,驗證過程所需的時間就會越長,請耐心等待。4、如果遊戲的文件真的是有點問題,那麼Steam就會自動啟動下載缺失的文件,等待下載
2026-02-16
汽車座套選購方法是什麼
汽車座套選購方法是什麼
1、顔色:座套顔色與愛車主色調是否搭配。例如車内飾顔色是米色最好選擇灰色或者米色等顔色,這樣才能更好的彰顯出愛車高貴與美觀一緻性,也充分展示了車主個人品味。2、品牌:座套與愛車座椅相吻合,目前汽車座套生産廠家根據車型與結構進行座套打模和制造,專車專用,這樣套上去的汽車座套才會舒适,沒有松松垮垮,因此...
2026-02-16
索引和目錄的區别
索引和目錄的區别
索引和目錄的區别如下:1、索引是将文獻中具有檢索意義的事項按照一定方式有序編排起來,以供檢索。2、目錄是“目錄”是目和錄的總稱。3、目錄是索引的一種,但是索引不是目錄。4、索引側重于讓你找到你要找的文章,目錄側重于顯示整篇文章的結構。拓展資料:在關系數據庫中,索引是一種單獨的、物理的對數據庫表中一列...
2026-02-16
qq大于2g的文件怎麼傳
qq大于2g的文件怎麼傳
以QQV9.4、win10、華為MateBookX為例。若是QQ會員,使用QQ聊天對話框選擇文件即可發送;若不是QQ會員,可以使用郵件發送。打開QQ将鼠标移到上方的頭像,點擊一旁懸浮窗的郵件圖标。進入界面後,點擊寫信,點擊超大附件,點擊上傳新文件或直接将文件拖至該位置。等待掃描上傳完成,點擊确定,添...
2026-02-16
Copyright 2023-2026 - www.tftnews.com All Rights Reserved