xiaobin_hlj80

https://blog.csdn.net/xiaobin_HLJ80 http://blog.chinaunix.net/uid/31552151.html

导航

ODBC2 - MFC

本文所用工程的设置:
2026-09-22 18_00_53-MFC Application

2026-09-22 18_34_12-MFC Application

Rec类:

2026-09-25 08_18_46-Add Class

Data Source (ODBC)

public:
	CRec1Set(CDatabase* pDatabase = NULL);

In database terms, a data source is a specific set of data, the information required to access that data, and the location of the data source,
which can be described using a data-source name. To work with class CDatabase,
the data source must be one that you have configured through Open Database Connectivity (ODBC) Administrator.
Examples of data sources include a remote database running on Microsoft SQL Server across a network or a Microsoft Access file in a local directory.
From your application, you can access any data source for which you have an ODBC driver.

Field

  • Declare
public:
	CStringA	m_stor_id;
	CStringA	m_ord_num;
	CTime		m_ord_date;
	int			m_qty;
	CStringA	m_payterms;
	CStringA	m_title_id;
  • Initial
CRec1Set::CRec1Set1Set(CDatabase* pdb)
	: CRecordset(pdb)
{
	m_stor_id = "";
	m_ord_num = "";
	m_ord_date;
	m_qty = 0;
	m_payterms = "";
	m_title_id = "";
	m_nFields = 6;
	m_nDefaultType = dynaset;
}

handling

invoke overloaded method:

  • GetDefaultConnect()
  • GetDefaultSQL()
  • DoFieldExchange()

Connection String

CString CRec1Set::GetDefaultConnect()
{
	return _T("DSN=sql2000;UID=sa;PWD=pass2word1;DATABASE=pubs");
}

Table

CString CRec1Set::GetDefaultSQL()
{
	return _T("[dbo].[sales]");
}

Record Field Exchange

void CRec1Set::DoFieldExchange(CFieldExchange* pFX)
{
	pFX->SetFieldType(CFieldExchange::outputColumn);
// Macros such as RFX_Text() and RFX_Int() are dependent on the
// type of the member variable, not the type of the field in the database.
// ODBC will try to automatically convert the column value to the requested type
	RFX_Text(pFX, _T("[stor_id]"), m_stor_id);
	RFX_Text(pFX, _T("[ord_num]"), m_ord_num);
	RFX_Date(pFX, _T("[ord_date]"), m_ord_date);
	RFX_Int(pFX, _T("[qty]"), m_qty);
	RFX_Text(pFX, _T("[payterms]"), m_payterms);
	RFX_Text(pFX, _T("[title_id]"), m_title_id);
}

Ref

posted on 2026-09-23 09:07  xiaobin80  阅读(8)  评论(0)    收藏  举报