日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

mysql+odbc+ado_MFC ado+mysql+odbc技术分享

發(fā)布時(shí)間:2024/9/27 数据库 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql+odbc+ado_MFC ado+mysql+odbc技术分享 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

第一步:建立數(shù)據(jù)庫(kù)

假設(shè)有一個(gè)sql文件

mysql>use dbname; ? ? ? ? ? ? ? ? ? //創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)名為dbname的數(shù)據(jù)庫(kù)(空數(shù)據(jù)庫(kù))

mysql>set names utf8; ? ? ? ? ? ? ?//編碼,mysql>source D:/dbname.sql; ?//導(dǎo)入一個(gè)數(shù)據(jù)庫(kù)源文件創(chuàng)建數(shù)據(jù)庫(kù)內(nèi)容

我做的工程數(shù)據(jù)庫(kù)名稱(chēng)是db_grain

第二步創(chuàng)建數(shù)據(jù)源

三、搭建數(shù)據(jù)源。

本項(xiàng)目中是用的是odbc(驅(qū)動(dòng))+mysql的數(shù)據(jù)源

1、安裝相應(yīng)的mysql-connector(分32,64位)。

我裝的是mysql-connector-odbc-5.1.5-win32.msi驅(qū)動(dòng)。

2、控制面板è管理工具(或{性能與…..})è數(shù)據(jù)源(odbc)è添加è你相應(yīng)的odbc驅(qū)動(dòng)。

(我的是5.1)è完成

3.完成后會(huì)跳出下面對(duì)話框。Name可以自己填,軟件項(xiàng)目中會(huì)用到下面你填的內(nèi)容

database在填寫(xiě)server,port,user,password后直接下拉得到,如果沒(méi)有則前面的填寫(xiě)錯(cuò)誤

4.

配置ado cpp文件

1、在項(xiàng)目中導(dǎo)入下面兩個(gè)文件到相應(yīng)包下面。

5.修改ado.cpp文件的OnInitADOConn()//不同驅(qū)的數(shù)據(jù)源只要改onInitADOConn()中的兩個(gè)值(bstr_t strConnect,m_pConnection->Open)就行了,其他不要改

void ADO::OnInitADOConn()

{

::CoInitialize(NULL);

try

{

m_pConnection.CreateInstance("ADODB.Connection"); //這里不用改

_bstr_t strConnect="DRIVER={MySQL ODBC 5.1 Driver};//這里改成你的驅(qū)動(dòng)名稱(chēng)(假如你的是access數(shù)據(jù)驅(qū)動(dòng)改成你的access驅(qū)動(dòng)如Microsoft Access Driver (*.mdb))

Server=localhost;

/*

Persist Security Info ----是否保存安全信息User ID-------------------用戶名PassWord------------------密碼Initial Catalog-----------數(shù)據(jù)庫(kù)的名稱(chēng)或者數(shù)據(jù)庫(kù)ip或者目錄Data Source---------------數(shù)據(jù)源

*/

PassWord=123456;

Persist Security Info=False;

User ID=root;

Data Source=autoresour";

m_pConnection->Open(strConnect,"","",adModeUnknown); //

/* Open (

const _variant_t & Source,

const _variant_t & ActiveConnection,

enum CursorTypeEnum CursorType,//””

enum LockTypeEnum LockType,//一般為””

long Options ); }*/ //一般是 adModeUnknown

catch(_com_error e)

{

AfxMessageBox(e.Description()); //

}

}

以我工程的數(shù)據(jù)源名稱(chēng)為"autoresour"為例

// ADO.cpp: implementation of the ADO class.

//

//

#include "stdafx.h"

#include "ADO.h"

#ifdef _DEBUG

#undef THIS_FILE

static char THIS_FILE[]=__FILE__;

#define new DEBUG_NEW

#endif

//

// Construction/Destruction

//

ADO::ADO()

{

}

ADO::~ADO()

{

}

void ADO::OnInitADOConn()

{

::CoInitialize(NULL);

try

{

m_pConnection.CreateInstance("ADODB.Connection"); //創(chuàng)建連接對(duì)象實(shí)例

_bstr_t strConnect="DRIVER={MySQL ODBC 5.2w Driver};Server=localhost;PassWord=123456;Persist Security Info=False;User ID=root;Data Source=autoresour";

m_pConnection->Open(strConnect,"","",adModeUnknown); //打開(kāi)數(shù)據(jù)庫(kù)

}

catch(_com_error e)

{

AfxMessageBox(e.Description()); //彈出錯(cuò)誤處理

}

}

_RecordsetPtr& ADO::OpenRecordset(CString sql)

{

ASSERT(!sql.IsEmpty()); //SQL語(yǔ)句不能為空

try

{

m_pRecordset.CreateInstance(__uuidof(Recordset)); //創(chuàng)建記錄集對(duì)象實(shí)例

m_pRecordset->Open(_bstr_t(sql), m_pConnection.GetInterfacePtr(),

adOpenDynamic, adLockOptimistic, adCmdText); //執(zhí)行SQL得到記錄集

}

catch(_com_error e) //捕獲可能的異常

{

AfxMessageBox(e.Description());

}

return m_pRecordset;

}

void ADO::CloseRecordset()

{

if(m_pRecordset->GetState() == adStateOpen) //判斷當(dāng)前的記錄集狀態(tài)

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

}

void ADO::CloseConn()

{

m_pConnection->Close(); //關(guān)閉數(shù)據(jù)庫(kù)連接

::CoUninitialize(); //釋放COM環(huán)境

}

CString ADO::getTimeToULong(){

SYSTEMTIME sm;

::GetLocalTime(&sm);

CTime tmSCan(sm);

CString szTime = tmSCan.Format("%Y-%m-%d %H:%M:%S");

return szTime;

}

UINT ADO::GetRecordCountt(_RecordsetPtr pRecordset)

{

int nCount = 0; //聲明保存記錄數(shù)的變量

try{

pRecordset->MoveFirst(); //將記錄集指針移動(dòng)到第一條記錄

}

catch(...) //捕捉可能出現(xiàn)的錯(cuò)誤

{

return 0; //產(chǎn)生錯(cuò)誤時(shí)返回0

}

if(pRecordset->adoEOF) //判斷記錄集中是否沒(méi)有記錄

return 0; //無(wú)記錄時(shí)返回0

while (!pRecordset->adoEOF) //當(dāng)記錄集指針沒(méi)有指向最后時(shí)

{

pRecordset->MoveNext(); //將記錄集指針移動(dòng)到下一條記錄

nCount = nCount + 1; //記錄個(gè)數(shù)的變量加1

}

pRecordset->MoveFirst(); //將記錄集指針移動(dòng)到第一條記錄

return nCount; //返回記錄數(shù)

}

CTime ADO::CStringToTime(CString string){

int first=string.Find('-');

int second=string.Find('-',first+1);

int year=atoi(string.Left(4));

int month=atoi(string.Mid(first+1,second-first));

int day=atoi(string.Mid(second+1,string.GetLength()-second-1));

CTime temp(year,month,day,0,0,0);

return temp;

}

CTime ADO::CStringToTimeComplete(CString cstring){//cstring 2013-02-05 01:02:03

int first=cstring.Find('-');

int second=cstring.Find('-',first+1);

int year=atoi(cstring.Left(4));

int month=atoi(cstring.Mid(first+1,second-first));

int day=atoi(cstring.Mid(second+1,10-second-1));

first = cstring.Find(':', second);

second = cstring.Find(':', first + 1);

int hour = atoi(cstring.Mid(first - 2,2));

int minute = atoi(cstring.Mid(second - 2,2));

int secd = atoi(cstring.Mid(second + 1,2));

CTime temp(year,month,day,hour,minute,secd);

return temp;

}

CString ADO::getYMD(CString string){

if(string.Find(" ") != -1){

string = string.Mid(0, string.Find(" "));

}

int first=string.Find('-');

int second=string.Find('-',first+1);

int year=atoi(string.Left(4));

int month=atoi(string.Mid(first+1,second-first));

int day=atoi(string.Mid(second+1,string.GetLength()-second-1));

CString trace;

trace.Format("%d%d%d", year, month, day);

return trace;

}

CString ADO::getYMDHMS(CString date, CString time){

int first=date.Find('-');

int second=date.Find('-',first+1);

int year=atoi(date.Left(4));

int month=atoi(date.Mid(first+1,second-first));

int day=atoi(date.Mid(second+1,date.GetLength()-second-1));

CString trace = "";

CString temp = "";

trace.Format("%d-", year);

temp.Format("%d-", month);

if(month/10 == 0){temp.Format("0%d-", month);}

trace += temp;

temp.Format("%d ", day);

if(day/10 == 0){temp.Format("0%d ", day);}

trace += temp;

temp = time;

if(temp.GetLength() == 7){temp ="0" + time;}

trace += temp;

//TRACE("TRACE:" + trace);

return trace;

}

/*

ADO m_Ado;

m_Ado.OnInitADOConn();//連接數(shù)據(jù)庫(kù)

CString SQL = "select * from tb_data"; //設(shè)置查詢(xún)字符串

m_Ado.m_pRecordset = m_Ado.OpenRecordset(SQL);//打開(kāi)記錄集

while(!m_Ado.m_pRecordset->adoEOF)

{

CString strSql = (LPCSTR)(_bstr_t)m_Ado.m_pRecordset->GetCollect("nowtime");

m_Ado.m_pRecordset->MoveNext();//將記錄集指針移動(dòng)到下一條記錄

}

m_Ado.CloseRecordset();

m_Ado.CloseConn();//斷開(kāi)數(shù)據(jù)庫(kù)連接

*/

修改你的工程的StdAfx.h文件中導(dǎo)入動(dòng)鏈接庫(kù)//在stdAfx.h中

// _AFX_NO_AFXCMN_SUPPORT

//加入#import ""C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace\

rename("EOF","adoEOF")rename("BOF","adoBOF")\//導(dǎo)入ADO動(dòng)態(tài)鏈接庫(kù)

//`AFX_INSERT_LOCATION`

//如果把msado15.dll放在工程目錄下就以下面我寫(xiě)的為準(zhǔn),放的位置要夾在//AFX_NO_AFXCMN_SUPPORT和//`AFX_INSERT_LOCATION`之間。// stdafx.h : include file for standard system include files,

// or project specific include files that are used frequently, but

// are changed infrequently

//

#if !defined(AFX_STDAFX_H__068DB9EC_AC8F_4663_850A_031896F0B1F2__INCLUDED_)

#define AFX_STDAFX_H__068DB9EC_AC8F_4663_850A_031896F0B1F2__INCLUDED_

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers

#include // MFC core and standard components

#include // MFC extensions

#include // MFC Automation classes

#include // MFC support for Internet Explorer 4 Common Controls

#ifndef _AFX_NO_AFXCMN_SUPPORT

#include // MFC support for Windows Common Controls

#endif // _AFX_NO_AFXCMN_SUPPORT

#include "SkinPPWTL.h"

#include "sizecbar.h"

#include "scbarg.h"

#include "CoolTabCtrl.h"

#import "msado15.dll" no_namespace\ //導(dǎo)入工程同目錄下的ADO動(dòng)態(tài)鏈接庫(kù)

rename("EOF","adoEOF")rename("BOF","adoBOF")

//`AFX_INSERT_LOCATION`

// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__068DB9EC_AC8F_4663_850A_031896F0B1F2__INCLUDED_)

使用ado類(lèi)的使用

假設(shè)對(duì)table為employees操作,ado數(shù)據(jù)操作對(duì)所有不同類(lèi)型的數(shù)據(jù)源是通用的

注明: 下面的三個(gè)變量類(lèi)型CString m_ID,m_Name,m_CultureADO m_Ado;

m_Ado.OnInitADOConn();

CString sql = "select * from employees";

m_Ado.m_pRecordset = m_Ado.OpenRecordset(sql);

try

{

/*

//刪除操作必須有一下兩條組成

m_Ado.m_pRecordset->Move((long)pos,vtMissing);// vtMissing固定字符

m_Ado.m_pRecordset->Delete(adAffectCurrent);

*/

//以下是修改操作

m_Ado.m_pRecordset->Move((long)pos,vtMissing);//vtMissing為const,不能改變的字符,這里就不用修改

m_Ado.m_pRecordset->PutCollect("編號(hào)",(_bstr_t)m_ID);

m_Ado.m_pRecordset->PutCollect("姓名",(_bstr_t)m_Name);

m_Ado.m_pRecordset->PutCollect("學(xué)歷",(_bstr_t)m_Culture);

/*

//以下是添加操作

m_Ado.m_pRecordset->AddNew(); //添加新行

m_Ado.m_pRecordset->PutCollect("編號(hào)",(_bstr_t)m_ID);//(_variant_t)(long);_variant_t var;var.intVal = 2;pRs->PutCollect("Layer", var);

m_Ado.m_pRecordset->PutCollect("姓名",(_bstr_t)m_Name);

m_Ado.m_pRecordset->PutCollect("學(xué)歷",(_bstr_t)m_Culture);

//以下是取值操作

CString tmp = (LPCSTR)(_bstr_t)m_Ado.m_pRecordset->GetCollect("outdtmp");

1.adAffectCurrent Deletes only the current record 僅刪除當(dāng)前記錄

2.adAffectGroup Deletes only records that satisfy the Filter setting (Filter must be set to a FilterGroupEnum value or an array of Bookmarks)

對(duì)滿足當(dāng)前 Filter 屬性設(shè)置的記錄取消掛起更新。使用該選項(xiàng)時(shí),必須將Filter屬性設(shè)置為合法的FilterGroupEnum常量之一或設(shè)置成一個(gè)書(shū)簽數(shù)組

更多參考msdn

*/ m_Ado.CloseRecordset();

m_Ado.CloseConn();

}

catch(...)

{

MessageBox("操作失敗");

return;

}

總結(jié)

以上是生活随笔為你收集整理的mysql+odbc+ado_MFC ado+mysql+odbc技术分享的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。