⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dhdbtables.cpp

📁 ADO查看数据库工具的原码
💻 CPP
字号:
// DHDBTables.cpp: implementation of the CDHDBTables class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "..\DHDBView.h"
#include "DHDBTables.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

#include <atlbase.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDHDBTables::CDHDBTables()
{
	HRESULT hr = NULL;
 	hr = ::CoInitialize(NULL);
 	if(FAILED(hr))
 	{
 		MessageBox(0,"初始化COM环境失败!", 0,0);
 	}
	m_Rst = NULL;
	m_Con = NULL;
}

CDHDBTables::~CDHDBTables()
{
	if(IsOpen()) Close();
	::CoUninitialize();
}

BOOL CDHDBTables::IsOpen()
{
	if(m_Rst != NULL)
	{
		return (m_Rst->GetState() == adStateOpen);
	}
	if(m_Con != NULL)
	{
		return (m_Con->GetState() == adStateOpen);
	}
	return FALSE;
}

BOOL CDHDBTables::Close()
{
	if(m_Rst != NULL)
	{
		if(m_Rst->GetState() == adStateOpen)
		{
			m_Rst->Close();
			m_Rst = NULL;
		}
	}
	if(m_Con != NULL)
	{
		if(m_Con->GetState() == adStateOpen)
		{
			m_Con->Close();
			m_Con = NULL;
		}
	}
	return TRUE;
}

CStringArray* CDHDBTables::GetDBTables(CString strCon)
{
	Close();
	CStringArray *pStrAry = new CStringArray();
	strCon.TrimLeft();
	strCon.TrimRight();
	if(strCon.IsEmpty())
	{
		return pStrAry;
	}
	HRESULT hr = NULL;
	try
	{
	//	hr = ::CoCreateInstance(__uuidof(Connection),0,CLSCTX_LOCAL_SERVER,__uuidof(Connection),m_Con);
		hr = m_Con.CreateInstance(__uuidof(Connection));
		if (FAILED(hr))
		{
			TRACE0(_T("创建数据库连接对象失败!"));
			return pStrAry;
 		}
		hr = m_Con->Open(_bstr_t(strCon),"","", adConnectUnspecified);
		if (FAILED(hr))
		{
			TRACE0(_T("打开数据库连接对象失败!"));
			return pStrAry;
 		}
		if(m_Rst = m_Con->OpenSchema(adSchemaTables))
 		{
			if(m_Rst == NULL)
			{
 				TRACE0(_T("打开连接失败!"));
				return pStrAry;
			}
			while(!m_Rst->adoEOF)
			{
				CString strType = m_Rst->GetCollect(_variant_t("TABLE_TYPE")).bstrVal;
				if(strType.Compare("TABLE") == 0)
					pStrAry->Add(m_Rst->GetCollect(_variant_t("TABLE_NAME")).bstrVal);
				m_Rst->MoveNext();
			}
			return pStrAry;
 		}
	}
	catch(_com_error& e)
	{
		CString strMes;
		strMes.Format("打开数据源失败:%s\n", (char*)e.Description());
		::AfxMessageBox(strMes);
		return pStrAry;
	}
	return pStrAry;
}

CStringArray* CDHDBTables::GetTableColumns(CString strCon, CString strTableName)
{
	strTableName.TrimLeft();
	strTableName.TrimRight();
	Close();
	CStringArray *pStrAry = new CStringArray();
	if(strTableName.IsEmpty())
		return pStrAry;
	if(strTableName.Find(" ") > 0)
		return pStrAry;
	HRESULT hr = NULL;
	try
	{
		hr = m_Con.CreateInstance(__uuidof(Connection));
		if (FAILED(hr))
		{
			TRACE0(_T("创建数据库连接对象失败!"));
			return pStrAry;
 		}
		hr = m_Con->Open(_bstr_t(strCon),"","", adConnectUnspecified);
		if (FAILED(hr))
		{
			TRACE0(_T("打开数据库连接对象失败!"));
			return pStrAry;
 		}
		CString strCmd;
		strCmd.Format("SELECT TOP 1 * FROM %s", strTableName);
		m_Rst =	m_Con->Execute(_bstr_t(strCmd), NULL, adCmdText);
		if(m_Rst == NULL)
		{
 			TRACE0(_T("打开连接失败!"));
			return pStrAry;
		}
		long nItemCount = m_Rst->GetFields()->GetCount();
		for(long i =0;i<nItemCount;i++)
		{
			CString strColName = (char*)(m_Rst->GetFields()->GetItem(i)->GetName());
			CString strType;
			long nsize = m_Rst->GetFields()->GetItem(i)->GetDefinedSize();
			int type = m_Rst->GetFields()->GetItem(i)->GetType();
			switch (type)
			{
			case	adEmpty:
				strType = "NULL";
				break;
			case	adTinyInt: 
				strType = "TinyInt";//"1-byte signed integer";
				break;
			case	adSmallInt:
				strType = "SmallInt";//"2-byte signed integer";
				break;
			case	adInteger:
				strType = "Integer";//"4-byte signed integer";
				break;
			case	adBigInt: 
				strType = "BigInt";//"8-byte signed integer";
				break;
			case	adUnsignedTinyInt:
				strType = "UnsignedTinyInt";//"1-byte unsigned integer";
				break;
			case	adUnsignedSmallInt:
				strType = "UnsignedSmallInt";//"2-byte unsigned integer";
				break;	
			case	adUnsignedInt:
				strType = "UnsignedInt";//"4-byte unsigned integer";
				break;
			case    adUnsignedBigInt: 
				strType = "UnsignedBigInt";//"8-byte unsigned integer";
				break;
			case    adSingle: 
				strType = "Single";//"single-precision floating point value";
				break;
			case    adDouble: 
				strType = "Double";//"double-precision floating point value";
				break;
			case    adCurrency:
				strType = "Currency";//"currency value";
				break;
			case    adDecimal:
				strType = "Decimal";//"exact numeric value with a fixed precision and scale";
				break;
			case    adNumeric:
				strType = "Numeric";//"exact numeric value with a fixed precision and scale";
				break;
			case    adBoolean:
				strType = "Boolean";//"Boolean value";
				break;
			case    adError:
				strType = "Error";//"32-bit error code";
				break;
			case    adUserDefined:
				strType = "UserDefined";//"user-defined variable";
				break;
			case    adVariant:
				strType = "Variant";//"Automation Variant";
				break;
			case    adIDispatch:
				strType = "IDispatch";//"pointer to an IDispatch interface on an OLE object";
				break;
			case    adIUnknown:
				strType = "IUnknown";//"pointer to an IUnknown interface on an OLE object";
				break;
			case    adGUID:
				strType = "GUID";//"globally unique identifier (GUID)";
				break;
			case    adDate:
				strType = "Date";//"Date value";
				break;
			case    adDBDate:
				strType = "DBDate";//"date value (yyyymmdd)";
				break;
			case    adDBTime:
				strType = "DBTime";//"time value (hhmmss)";
				break;
			case    adDBTimeStamp:
				strType = "DBTimeStamp";//"date-time stamp";
				break;
			case    adBSTR:
				strType = "BSTR";//"null-terminated character string (Unicode) ";
				break;
			case    adChar:
				strType = "Char";//"String value";
				break;
			case    adVarChar:
				strType = "VarChar";//"String value";
				break;
			case    adLongVarChar:
				strType = "LongVarChar";//"long String value";
				break;
			case    adWChar:
				strType = "WChar";//"null-terminated Unicode character string";
				break;
			case    adVarWChar:
				strType = "VarWChar";//"null-terminated Unicode character string";
				break;
			case    adLongVarWChar:
				strType = "LongVarWChar";//"long null-terminated string value";
				break;
			case    adBinary:
				strType = "Binary";//"binary value";
				break;
			case    adVarBinary:
				strType = "VarBinary";//"binary value";
				break;
			case    adLongVarBinary:
				strType = "LongVarBinary"; //"long binary value";
				break;
			case    adChapter:
				strType = "Chapter";
				break;
			case    adFileTime:
				strType = "FileTime";
				break;
			case    adPropVariant:
				strType = "PropVariant";
				break;
			case    adVarNumeric:
				strType = "VarNumeric";
				break;
			case    adArray:
				strType = "Array"; //"a safe-array of that type";
				break;
			default:
				strType = "未知数据类型";
				break;
			}
			CString strItem;
			strItem.Format("%s | %s  | %d", strColName, strType, nsize);
			pStrAry->Add(strItem);
		}
		
		
		return pStrAry;
	}
	catch(_com_error& e)
	{
		TRACE1("%s\n", e.Description());
	}
	return pStrAry;

	/*m_Rst = m_Con->OpenSchema(adSchemaColumns)
			if(m_Rst == NULL)
			{
 				TRACE0(_T("打开连接失败!"));
				return *pStrAry;
			}
	
			while(!m_Rst->adoEOF)
			{
				CString strName = m_Rst->GetCollect(_variant_t("TABLE_NAME")).bstrVal;
				
				if(strName.Compare(strTableName) == 0)
				{
					int type = m_Rst->GetCollect(_variant_t("DATA_TYPE")).iVal;
					CString strType;
					switch (type)
					{
					case	adEmpty:
						strType = "NULL";
						break;
					case	adTinyInt: 
						strType = "TinyInt";//"1-byte signed integer";
						break;
					case	adSmallInt:
						strType = "SmallInt";//"2-byte signed integer";
						break;
					case	adInteger:
						strType = "Integer";//"4-byte signed integer";
						break;
					case	adBigInt: 
						strType = "BigInt";//"8-byte signed integer";
						break;
					case	adUnsignedTinyInt:
						strType = "UnsignedTinyInt";//"1-byte unsigned integer";
						break;
					case	adUnsignedSmallInt:
						strType = "UnsignedSmallInt";//"2-byte unsigned integer";
						break;	
					case	adUnsignedInt:
						strType = "UnsignedInt";//"4-byte unsigned integer";
						break;
					case    adUnsignedBigInt: 
						strType = "UnsignedBigInt";//"8-byte unsigned integer";
						break;
					case    adSingle: 
						strType = "Single";//"single-precision floating point value";
						break;
					case    adDouble: 
						strType = "Double";//"double-precision floating point value";
						break;
					case    adCurrency:
						strType = "Currency";//"currency value";
						break;
					case    adDecimal:
						strType = "Decimal";//"exact numeric value with a fixed precision and scale";
						break;
					case    adNumeric:
						strType = "Numeric";//"exact numeric value with a fixed precision and scale";
						break;
					case    adBoolean:
						strType = "Boolean";//"Boolean value";
						break;
					case    adError:
						strType = "Error";//"32-bit error code";
						break;
					case    adUserDefined:
						strType = "UserDefined";//"user-defined variable";
						break;
					case    adVariant:
						strType = "Variant";//"Automation Variant";
						break;
					case    adIDispatch:
						strType = "IDispatch";//"pointer to an IDispatch interface on an OLE object";
						break;
					case    adIUnknown:
						strType = "IUnknown";//"pointer to an IUnknown interface on an OLE object";
						break;
					case    adGUID:
						strType = "GUID";//"globally unique identifier (GUID)";
						break;
					case    adDate:
						strType = "Date";//"Date value";
						break;
					case    adDBDate:
						strType = "DBDate";//"date value (yyyymmdd)";
						break;
					case    adDBTime:
						strType = "DBTime";//"time value (hhmmss)";
						break;
					case    adDBTimeStamp:
						strType = "DBTimeStamp";//"date-time stamp";
						break;
					case    adBSTR:
						strType = "BSTR";//"null-terminated character string (Unicode) ";
						break;
					case    adChar:
						strType = "Char";//"String value";
						break;
					case    adVarChar:
						strType = "VarChar";//"String value";
						break;
					case    adLongVarChar:
						strType = "LongVarChar";//"long String value";
						break;
					case    adWChar:
						strType = "WChar";//"null-terminated Unicode character string";
						break;
					case    adVarWChar:
						strType = "VarWChar";//"null-terminated Unicode character string";
						break;
					case    adLongVarWChar:
						strType = "LongVarWChar";//"long null-terminated string value";
						break;
					case    adBinary:
						strType = "Binary";//"binary value";
						break;
					case    adVarBinary:
						strType = "VarBinary";//"binary value";
						break;
					case    adLongVarBinary:
						strType = "LongVarBinary"; //"long binary value";
						break;
					case    adChapter:
						strType = "Chapter";
						break;
					case    adFileTime:
						strType = "FileTime";
						break;
					case    adPropVariant:
						strType = "PropVariant";
						break;
					case    adVarNumeric:
						strType = "VarNumeric";
						break;
					case    adArray:
						strType = "Array"; //"a safe-array of that type";
						break;
					default:
						strType = "未知数据类型";
						break;
					}

					CString strColName = m_Rst->GetCollect(_variant_t("COLUMN_NAME")).bstrVal;
					CString strItem;
					strItem.Format("%s | %s", strColName, strType);
					pStrAry->Add(strItem);
				}
				m_Rst->MoveNext();
			}*/
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -