📄 datalink1.h
字号:
// 版本 1.1
// 修改日期 2006-2-15
//
// 版本 1.2
// 修改日期 2006-2-22
// Recordset.Open(); Recordset.Close();
#pragma once
// 使用重定义
#define CRecordset COleDBRecordset
#ifndef _countof
// determine number of elements in an array (not bytes)
#define _countof(array) (sizeof(array)/sizeof(array[0]))
#endif
// initial datalink
void AFXAPI AfxSetDataLink(LPCTSTR lpszConnectionString);
void AFXAPI AfxGetDataLink(CString& strConnectionString);
/////////////////////////////////////////////////////////////////////////////
// _AFX_DATALINK_STATE
class _AFX_DATALINK_STATE : public CNoTrackObject
{
public:
_AFX_DATALINK_STATE();
LPCTSTR m_pszConnectionString;
IErrorInfo* m_pErrorInfo;
virtual ~_AFX_DATALINK_STATE();
};
EXTERN_PROCESS_LOCAL(_AFX_DATALINK_STATE, _afxDataLinkState)
/*********************************************************************************************************
连接字符串的基本格式包括一系列由分号分隔的关键字/值对。等号 (=) 连接各个关键字及其值。若要包括含有分号、单
引号字符或双引号字符的值,则该值必须用双引号括起来。如果该值同时包含分号和双引号字符,则该值可以用单引号括
起来。如果该值以双引号字符开始,则还可以使用单引号。相反地,如果该值以单引号开始,则可以使用双引号。如果该
值同时包含单引号和双引号字符,则用于将值括起来的引号字符每次出现时,都必须成对出现。
若要在字符串值中包括前导或尾随空格,则该值必须用单引号或双引号括起来。即使将整数、布尔值或枚举值用引号括起
来,其周围的任何前导或尾随空格也将被忽略。然而,保留字符串关键字或值内的空格。使用 .NET Framework 1.1 版时
,在连接字符串中可以使用单引号或双引号而不用使用分隔符(例如,Data Source= my'Server 或 Data Source= my"Se
rver),但引号字符不可以为值的第一个或最后一个字符。
若要在关键字或值中包括等号 (=),则它之前必须还有另一个等号。例如,在假设的连接字符串中,
"key==word=value"
key="dd""ddd"""
key=dd"ddd"
关键字是“key=word”并且值是“value”。
如果“keyword= value”对中的一个特定关键字多次出现在连接字符串中,则将所列出的最后一个用于值集。
关键字不区分大小写。
**********************************************************************************************************/
class CConnectionString : public CString
{
public:
CConnectionString();
CConnectionString(const CString& stringSrc);
CConnectionString(LPCTSTR lpsz);
CConnectionString(LPCTSTR lpch, int nLength);
CConnectionString(UINT nProviderType, LPCTSTR datasrc, LPCTSTR catalog = NULL,
LPCTSTR userid = NULL, LPCTSTR password = NULL);
CConnectionString(LPCTSTR provider, LPCTSTR datasrc, LPCTSTR catalog = NULL,
LPCTSTR userid = NULL, LPCTSTR password = NULL);
virtual ~CConnectionString();
public:
void SetKeyValue(LPCTSTR keyName, LPCTSTR value);
CString GetKeyValue(LPCTSTR keyName);
CString GetKeyName(int nIndex);
IDispatch* OpenConnection();
BOOL TestConnect();
};
class CSqlString : public CString
{
public:
CSqlString();
CSqlString(const CString& stringSrc);
CSqlString(LPCTSTR lpsz);
CSqlString(LPCTSTR lpch, int nLength);
virtual ~CSqlString();
public:
static CString Execute(IDispatch* pConnection, LPCTSTR lpszCmdText, UINT* pRecordsAffected = NULL);
static CString Execute(LPCTSTR lpszFormat, ...);
static CString ExecuteV(LPCTSTR lpszFormat, va_list args);
CString Execute();
int GetRecordsAffected() const;
CString GetConnectionString() const;
void SetConnectionString(LPCTSTR value);
// 转换成“正常”形式
void Normalize();
// select_list
CString GetSelectList();
void SetSelectList(LPCTSTR lpszSelectList);
// column_alias, column_name and expression
int GetColumnCount();
CString GetColumnAlias(int nIndex);
CString GetColumnName(int nIndex);
CString GetColumnName(LPCTSTR lpszAlias);
int GetColumnIndex(LPCTSTR lpszAlias);
void SetColumnAlias(int nIndex, LPCTSTR lpszAlias);
void SetColumnName(int nIndex, LPCTSTR lpszName);
void SetColumnName(LPCTSTR lpszAlias, LPCTSTR lpszName);
int InsertColumn(int nIndex, LPCTSTR lpszName);
void DeleteColumn(int nIndex);
// table_source
CString GetTableSource();
void SetTableSource(LPCTSTR lpszTableSource);
// table_alias, table_name and joined_table
int GetTableCount();
CString GetTableAlias(int nIndex);
CString GetTableName(int nIndex);
CString GetTableName(LPCTSTR lpszAlias);
int GetTableIndex(LPCTSTR lpszAlias);
void SetTableAlias(int nIndex, LPCTSTR lpszAlias);
void SetTableName(int nIndex, LPCTSTR lpszName);
void SetTableName(LPCTSTR lpszAlias, LPCTSTR lpszName);
int InsertTable(int nIndex, LPCTSTR lpszName);
void DeleteTable(int nIndex);
// joined_table
CString GetJoinedTable();
void SetJoinedTable(LPCTSTR lpszJoinedTable);
int GetJoinedTableCount();
CString GetJoinedTableAlias(int nIndex);
CString GetJoinedTableName(int nIndex);
CString GetJoinedTableName(LPCTSTR lpszAlias);
int GetJoinedTableIndex(LPCTSTR lpszAlias);
void SetJoinedTableAlias(int nIndex, LPCTSTR lpszAlias);
void SetJoinedTableName(int nIndex, LPCTSTR lpszName);
void SetJoinedTableName(LPCTSTR lpszAlias, LPCTSTR lpszName);
int InsertJoinedTable(int nIndex, LPCTSTR lpszName, LPCTSTR lpszJoinType,
LPCTSTR lpszJoinCondition);
void DeleteJoinedTable(int nIndex);
// join_type and search_condition
CString GetJoinType(int nIndex);
CString GetJoinType(LPCTSTR lpszTableAlias);
CString GetJoinCondition(int nIndex);
CString GetJoinCondition(LPCTSTR lpszTableAlias);
void SetJoinType(int nIndex, LPCTSTR lpszType);
void SetJoinType(LPCTSTR lpszTableAlias, LPCTSTR lpszType);
void SetJoinCondition(int nIndex, LPCTSTR lpszCondition);
void SetJoinCondition(LPCTSTR lpszTableAlias, LPCTSTR lpszCondition);
// search_condition, group_by_expression and sort_expression
CString GetSearchCondition(BOOL bCompute = FALSE);
CString GetGroupByExpression();
CString GetSortExpression();
void SetSearchCondition(LPCTSTR lpszCondition, BOOL bCompute = FALSE);
void SetGroupByExpression(LPCTSTR lpszExpression);
void SetSortExpression(LPCTSTR lpszExpression);
// command parameters
void SetParameter(LPCTSTR lpszName, LPCTSTR lpszValue);
protected:
CString m_strConnectionString;
UINT m_nRecordsAffected;
//CMapStringToString m_mapParameters;
};
#define SQL_FETCH_NEXT 1
#define SQL_FETCH_FIRST 2
/* Other codes used for FetchOrientation in SQLFetchScroll() */
#define SQL_FETCH_LAST 3
#define SQL_FETCH_PRIOR 4
#define SQL_FETCH_ABSOLUTE 5
#define SQL_FETCH_RELATIVE 6
/////////////////////////////////////////////////////////////////////////////
// Info helper definitions
#define AFX_CURRENT_RECORD_UNDEFINED (-2)
#define AFX_CURRENT_RECORD_BOF (-1)
// For returning status for a recordset
struct COleDBRecordsetStatus
{
long m_lCurrentRecord; // -2=Unknown,-1=BOF,0=1st record. . .
BOOL m_bRecordCountFinal;// Have we counted all records?
};
#pragma warning( push )
#pragma warning( disable: 4121 )
//////////////////////////////////////////////////////////////////////////////
// COleDBRecordset helpers
typedef int RETCODE;
#define SQL_NO_DATA_FOUND 100
void AFXAPI _AfxSetCurrentRecord(long* plCurrentRecord, long nRows, RETCODE nRetCode);
void AFXAPI _AfxSetRecordCount(long* plRecordCount, long lCurrentRecord,
BOOL bEOFSeen, RETCODE nRetCode);
// COleDBRecordset
//
class COleDBRecordset : public CObject
{
DECLARE_DYNAMIC(COleDBRecordset)
// Constructor
public:
/* explicit */ COleDBRecordset(IDispatch* pConnection = NULL);
public:
virtual ~COleDBRecordset();
// Attributes
public:
BOOL CanAppend() const; // Can AddNew be called?
BOOL CanRestart() const; // Can Requery be called to restart a query?
BOOL CanScroll() const; // Can MovePrev and MoveFirst be called?
BOOL CanTransact() const; // Are Transactions supported?
BOOL CanUpdate() const; // Can Edit/AddNew/Delete be called?
BOOL CanBookmark() const; // Can Get/SetBookmark be called?
const CString& GetSQL() const; // SQL executed for this recordset
BOOL IsOpen() const; // Recordset successfully opened?
BOOL IsBOF() const; // Beginning Of File
BOOL IsEOF() const; // End Of File
BOOL IsDeleted() const; // On a deleted record
//BOOL IsFieldDirty(void *pv); // has field been updated?
BOOL IsFieldNull(int nIndex); // is field NULL valued?
BOOL IsFieldNull(LPCTSTR lpszName);
BOOL IsFieldNullable(int nIndex); // can field be set to a NULL value
BOOL IsFieldNullable(LPCTSTR lpszName);
long GetRecordCount() const; // Records seen so far or -1 if unknown
void GetStatus(COleDBRecordsetStatus& rStatus) const;
public:
IDispatch* m_pConnection;
IDispatch* m_pRecordset;
BOOL m_bAutoRelease;
protected:
BOOL m_bBOF;
BOOL m_bEOF;
BOOL m_bUpdatable; // Is recordset updatable?
BOOL m_bAppendable;
CString m_strSQL;
//CString m_strUpdateSQL; // SQL statement for updates
BOOL m_bScrollable; // supports MovePrev
BOOL m_bDeleted;
UINT m_nFields; // number of RFX fields
UINT m_nParams; // number of RFX params
DWORD m_dwOptions; // archive dwOptions on Open
BOOL m_bEOFSeen;
long m_lRecordCount;
long m_lCurrentRecord;
// Operations
public:
virtual BOOL Open(LPCTSTR lpszSQL = NULL, DWORD dwOptions = 0);
virtual BOOL OpenSchema(int nSchema = 20);
void Cancel();
virtual void Close();
//BOOL SetFilter(LPCTSTR lpszFilter);
//BOOL SetSort(LPCTSTR lpszCriteria);
// cursor operations
void MoveNext();
void MovePrev();
void MoveFirst();
void MoveLast();
virtual void Move(long nRows, WORD wFetchType = SQL_FETCH_RELATIVE);
void SetAbsolutePosition(long nRows);
void GetBookmark(COleVariant& varBookmark);
void SetBookmark(const COleVariant& varBookmark);
// edit buffer operations
virtual void AddNew(); // add new record at the end
virtual void Edit(); // start editing
virtual BOOL Update(); // update it
virtual void Delete(); // delete the current record
void DeleteAll(); // delete all the record
void CancelUpdate(); // cancel pending Edit/AddNew
CString GetFullString();
virtual COleVariant GetFieldValue(int nIndex);
virtual COleVariant GetFieldValue(LPCTSTR lpszName);
CString GetString(int nIndex);
CString GetString(LPCTSTR lpszName);
COleDateTime GetDateTime(int nIndex);
COleDateTime GetDateTime(LPCTSTR lpszName);
short GetInt16(int nIndex);
short GetInt16(LPCTSTR lpsName);
int GetInt32(int nIndex);
int GetInt32(LPCTSTR lpszName);
long GetInt64(int nIndex);
long GetInt64(LPCTSTR lpszName);
float GetFloat(int nIndex);
float GetFloat(LPCTSTR lpszName);
double GetDouble(int nIndex);
double GetDouble(LPCTSTR lpszName);
BOOL GetBinary(int nIndex, LPVOID* ppData, UINT* pBytes);
BOOL GetBinary(LPCTSTR lpszName, LPVOID* ppData, UINT* pBytes);
BOOL GetChunk(int nIndex, LPVOID* ppData, UINT* pBytes);
BOOL GetChunk(LPCTSTR lpszName, LPVOID* ppData, UINT* pBytes);
int GetFieldCount();
BOOL HasField(LPCTSTR lpszName);
CString GetFieldName(int nIndex);
UINT GetFieldType(int nIndex);
UINT GetFieldType(LPCTSTR lpszName);
int GetFieldLength(int nIndex);
int GetFieldLength(LPCTSTR lpszName);
IDispatch* GetFields();
virtual void SetFieldValue(LPCTSTR lpszName, COleVariant& varValue);
virtual void SetFieldValue(int nIndex, COleVariant& varValue);
void SetFieldValue(LPCTSTR lpszName, LPCTSTR lpszValue);
void SetFieldValue(int nIndex, LPCTSTR lpszValue);
void SetFieldValue(LPCTSTR lpszName, const COleDateTime& date);
void SetFieldValue(int nIndex, const COleDateTime& date);
void SetFieldValue(LPCTSTR lpszName, short nValue);
void SetFieldValue(int nIndex, short nValue);
void SetFieldValue(LPCTSTR lpszName, int nValue);
void SetFieldValue(int nIndex, int nValue);
void SetFieldValue(LPCTSTR lpszName, long lValue);
void SetFieldValue(int nIndex, long lValue);
void SetFieldValue(LPCTSTR lpszName, float fltValue);
void SetFieldValue(int nIndex, float fltValue);
void SetFieldValue(LPCTSTR lpszName, double dblValue);
void SetFieldValue(int nIndex, double dblValue);
void SetFieldValue(LPCTSTR lpszName, LPVOID pData, UINT nBytes);
void SetFieldValue(int nIndex, LPVOID pData, UINT nBytes);
void SetFieldValueNull(int nIndex);
void SetFieldValueNull(LPCTSTR lpszName);
public:
void SetFieldDirty(int nIndex, BOOL bDirty = TRUE);
void SetFieldDirty(LPCTSTR lpszName, BOOL bDirty = TRUE);
// Recordset operations
virtual BOOL Requery(); // Re-execute query based on new params
// find operations
BOOL Find(LPCTSTR lpszFind, BOOL bSearchBackward = FALSE);
BOOL Save(LPCTSTR strFileName, UINT nPersistFormat);
//BOOL Save(IXMLDOMDocument** document);
//BOOL Load(LPCTSTR strFileName);
};
inline const CString& COleDBRecordset::GetSQL() const
{ ASSERT(IsOpen()); return m_strSQL; }
inline BOOL COleDBRecordset::IsBOF() const
{ /*ASSERT(IsOpen());*/ return m_bBOF; }
inline BOOL COleDBRecordset::IsEOF() const
{ /*ASSERT(IsOpen());*/ return m_bEOF; }
inline BOOL COleDBRecordset::IsDeleted() const
{ ASSERT(IsOpen()); return m_bDeleted; }
inline BOOL COleDBRecordset::CanUpdate() const
{ ASSERT(IsOpen()); return m_bUpdatable; }
inline BOOL COleDBRecordset::CanScroll() const
{ ASSERT(IsOpen()); return m_bScrollable; }
inline BOOL COleDBRecordset::CanAppend() const
{ ASSERT(IsOpen()); return m_bAppendable; }
inline long COleDBRecordset::GetRecordCount() const
{ ASSERT(IsOpen()); return m_lRecordCount; }
inline void COleDBRecordset::GetStatus(COleDBRecordsetStatus& rStatus) const
{ ASSERT(IsOpen());
rStatus.m_lCurrentRecord = m_lCurrentRecord;
rStatus.m_bRecordCountFinal = m_bEOFSeen; }
inline void COleDBRecordset::MoveNext()
{ ASSERT(IsOpen()); Move(1, SQL_FETCH_NEXT); }
inline void COleDBRecordset::MovePrev()
{ ASSERT(IsOpen()); Move(-1, SQL_FETCH_PRIOR); }
inline void COleDBRecordset::MoveFirst()
{ ASSERT(IsOpen()); Move(1, SQL_FETCH_FIRST); }
inline void COleDBRecordset::MoveLast()
{ ASSERT(IsOpen()); Move(-1, SQL_FETCH_LAST); }
inline void COleDBRecordset::SetAbsolutePosition(long nRows)
{ ASSERT(IsOpen()); Move(nRows, SQL_FETCH_ABSOLUTE); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -