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

📄 biodb.cpp

📁 通过MS的ADO对数据库进行操作.包括连接,读数据,修改数据,删除数据等
💻 CPP
字号:
// BioDB.cpp: implementation of the CBioDB class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ConnectDB.h"
#include "BioDB.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CBioDB::CBioDB()
{
	
}

CBioDB::~CBioDB()
{
	
}

//////////////////////////////////////////////////////////////////////
// Csysadmin Class
//////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Csysadmin::Csysadmin()
{
	 
	HRESULT hr;
	FILE *file=NULL;
	char n_connectchar[200];
	long wbfSize;
	CString strTemp;
	char pModuleName[256];
	memset(n_connectchar,0,200);
	memset(pModuleName,0,256);

	//初始化全局变量
	m_saID="";
	m_saName="";
	m_saPSW="";
	m_saType="";
	//结束
	
	
	::CoInitialize(NULL);
	hr=m_Connect.CreateInstance(__uuidof(Connection));//初始化 Connection
	
	
	::GetModuleFileName(NULL,pModuleName,256);		//返回模块完整路径
	strTemp=pModuleName;
	
	int pos=strTemp.ReverseFind('\\');
	g_strAppPath=strTemp.Left(pos);//获得初始化文件路径
	g_strAppPath += "\\DBconfig.ini";
	
	
	//获得文件内容	
	file = fopen(g_strAppPath,"r");
	if(file == NULL)
	{
		//打开文件错误
		AfxMessageBox("OPEN File Error!");
		exit(1);
	}
	//获得文件字符串长度
	if(fseek(file,0,SEEK_END ))
	{
		AfxMessageBox("MOVE POINT Error!");
		exit(0);
	}
	wbfSize = ftell(file);
	//end 获取长度
	fseek(file,0,SEEK_SET );//恢复指针,指向文件头
	wbfSize=fread(n_connectchar,sizeof(char),wbfSize,file);
	m_ConnectString = (CString)n_connectchar;
	fclose(file);
	
	//end 获取文件内容	
	
	if(SUCCEEDED(hr))
	{
		try{
			m_Connect->Open((_bstr_t)m_ConnectString,"","",NULL);
		}
		catch(_com_error &e)
		{
			_bstr_t bstrSource(e.Source());
			_bstr_t bstrDescription(e.Description());
			CString c_errortemp;
			c_errortemp=(LPCSTR)bstrSource;
			c_errortemp += (LPCSTR)bstrDescription;
			AfxMessageBox(c_errortemp);
			exit(1);
		}
	}
	else
	{
		::CoUninitialize();
		exit(1);
	}
	
	hr = m_Record.CreateInstance(__uuidof(Recordset));//初始化Recordset
	if(FAILED(hr))
	{
		::CoUninitialize();
		exit(1);
	}
	
	
}

Csysadmin::~Csysadmin()
{
	
	m_Connect->Close();
	::CoUninitialize();
}

CString Csysadmin::getSaID()
{
	return m_saID;
}

CString Csysadmin::getSaName()
{
	return m_saName;
}

CString Csysadmin::getSaType()
{
	return m_saType;
}

CString Csysadmin::getSaPSW()
{
	return m_saPSW;
}

void Csysadmin::setSaID(CString newvalue)
{
	m_saID = newvalue;
}

void Csysadmin::setSaName(CString newvalue)
{
	m_saName = newvalue;
}

void Csysadmin::setSaType(CString newvalue)
{
	m_saType = newvalue;
}

void Csysadmin::setSaPSW(CString newvalue)
{
	m_saPSW = newvalue;
}

void Csysadmin::DeleteAll()
{//完成
	try
	{
		//		m_Record->CursorLocation = adUseClient;
		m_Record->Open("delete sysadmin ",(_bstr_t)m_ConnectString,adOpenStatic,adLockOptimistic,adCmdText);
	}//删除数据库SYSADMIN中所有的数据,因为是adOpenStatic 所以操作完成之后直接关闭m_Record,不需要调用m_Record->close()
	catch(_com_error &e)
	{
		_bstr_t bstrSource(e.Source());//出错源
		_bstr_t bstrDescription(e.Description());//错误描述
		CString c_errortemp;
		c_errortemp=(LPCSTR)bstrSource;
		c_errortemp += (LPCSTR)bstrDescription;
		AfxMessageBox(c_errortemp);
	}
	
}

void Csysadmin::SInfosysAdmin(CString saID)//SELECT INFO FORM SYSADMIN by ID
{//完成!//可以用TrimRight取消空格

	CString CommandString;
	CommandString = "select * from sysAdmin where saID = "+saID;
	try
	{
		m_Record->Open((_variant_t)CommandString,(_bstr_t)m_ConnectString,adOpenDynamic,adLockOptimistic,adCmdText);
		if(VARIANT_FALSE==m_Record->adoEOF)
		{
			m_saID=(char*)(_bstr_t)(m_Record->Fields->GetItem(_variant_t("saID"))->Value);
			m_saID.TrimRight();
			m_saName=(char*)(_bstr_t)(m_Record->Fields->GetItem(_variant_t("saName"))->Value);
			m_saName.TrimRight();
			m_saPSW=(char*)(_bstr_t)(m_Record->Fields->GetItem(_variant_t("saPSW"))->Value);
			m_saPSW.TrimRight();
			m_saType=(char*)(_bstr_t)(m_Record->Fields->GetItem(_variant_t("saType"))->Value);
			m_saType.TrimRight();
		}
		m_Record->Close();
/*		TEMP = m_Record->GetCollect("saID").Detach();//上句是动态打开数据库,所以m_Record 未关闭!
		
		if(VARIANT_FALSE == m_Record->adoEOF)
		{
			
			ASSERT(TEMP.vt == VT_BSTR);
			CString strData(TEMP.bstrVal);
			CString tempstring = strData;
			tempstring.TrimRight();
			int i = tempstring.GetLength();
			m_Record->Close();
			
		}
		else
		{
			m_Record->Close();			
		}
*/
		
	}
	catch(_com_error &e)
	{
		_bstr_t bstrSource(e.Source());
		_bstr_t bstrDescription(e.Description());
		CString c_errortemp;
		c_errortemp=(LPCSTR)bstrSource;
		c_errortemp += (LPCSTR)bstrDescription;
		AfxMessageBox(c_errortemp);
	}
	
}

bool Csysadmin::InsertSysAdmin()//INSERT A ROW 
{//完成
	try{
		m_Record->Open((_variant_t)"sysAdmin",(_bstr_t)m_ConnectString,adOpenKeyset,adLockOptimistic, adCmdTable);
		if(!m_Record->Supports(adAddNew)) return 0;
		
		m_Record->AddNew(); 
		m_Record->Fields->GetItem
			(_variant_t("saID"))->Value=_bstr_t(m_saID);
		m_Record->Fields->GetItem
			(_variant_t("saName"))->Value=_bstr_t(m_saName);
		m_Record->Fields->GetItem
			(_variant_t("saPSW"))->Value=_bstr_t(m_saPSW);
		m_Record->Fields->GetItem
			(_variant_t("saType"))->Value=_bstr_t(m_saType);
		m_Record->Update();
		m_Record->Close();
		return 1;
	}//try
	catch (_com_error &e)
	{
		_bstr_t bstrSource(e.Source());
		_bstr_t bstrDescription(e.Description());
		CString c_errortemp;
		c_errortemp=(LPCSTR)bstrSource;
		c_errortemp += (LPCSTR)bstrDescription;
		AfxMessageBox(c_errortemp);
		return 0;
	}

	return 0;

}

void Csysadmin::LoadsysAdminInfo(VARIANT *returnValue)//SLECT ALL INFO FORM SYSADMIN 这个方法与SELECT BY NAME 一样
{//未完成!
	
	CString CommandString;
	long *lUbound;
	CommandString = "select * from sysAdmin";
	VARIANT vartemp;

	try
	{
		m_Record->Open((_variant_t)CommandString,(_bstr_t)m_ConnectString,adOpenDynamic,adLockOptimistic,adCmdText);
		//读大量数据
		*returnValue = m_Record->GetRows(adGetRowsRest);//获得所有行
		SafeArrayGetUBound(returnValue->parray,2,lUbound);//在lUbound返回列数. 第二个参数2表示列 1表示行

/*		while(m_Record->adoEOF==VARIANT_FALSE) 
		{
			returnValue
			
			CString sName=(char*)(_bstr_t)(m_Record->Fields->GetItem
				(_variant_t("saID"))->Value);
			CString cAge=(char*)(_bstr_t)(m_Record->Fields->GetItem
				(_variant_t("saName"))->Value);//取到的数据放到哪? 由什么传递出去?

			m_Record->MoveNext(); 
		}
*/	
			
	}
	catch(_com_error &e)
	{
		_bstr_t bstrSource(e.Source());
		_bstr_t bstrDescription(e.Description());
		CString c_errortemp;
		c_errortemp=(LPCSTR)bstrSource;
		c_errortemp += (LPCSTR)bstrDescription;
		AfxMessageBox(c_errortemp);
	}




}

BOOL Csysadmin::UDataUInfoSA(CString saID)//UPDATA Info in sysAdmin by ID (in the first:get the info from database!)
{//完成
	try
	{
		m_Record->Open((_variant_t)"sysAdmin",(_bstr_t)m_ConnectString,adOpenKeyset,adLockOptimistic, adCmdTable);
		if(!m_Record->Supports(adAddNew)) return FALSE;
		
		m_Record->AddNew(); 
		m_Record->Fields->GetItem
			(_variant_t("saID"))->Value=_bstr_t(m_saID);
		m_Record->Fields->GetItem
			(_variant_t("saName"))->Value=_bstr_t(m_saName);
		m_Record->Fields->GetItem
			(_variant_t("saPSW"))->Value=_bstr_t(m_saPSW);
		m_Record->Fields->GetItem
			(_variant_t("saType"))->Value=_bstr_t(m_saType);
		m_Record->Update();
		m_Record->Close();
		return TRUE;
	}//try
	catch (_com_error &e)
	{
		_bstr_t bstrSource(e.Source());
		_bstr_t bstrDescription(e.Description());
		CString c_errortemp;
		c_errortemp=(LPCSTR)bstrSource;
		c_errortemp += (LPCSTR)bstrDescription;
		AfxMessageBox(c_errortemp);
		return FALSE;
	}

}

void Csysadmin::DelUInfoByID(CString ID)//del user info by id
{//完成
	CString commandstring;
	commandstring = "delete sysadmin where saID =  "+ID;
	try
	{
		//		m_Record->CursorLocation = adUseClient;
		m_Record->Open((_variant_t)commandstring,(_bstr_t)m_ConnectString,adOpenStatic,adLockOptimistic,adCmdText);
	}//删除数据库SYSADMIN中所有的数据,因为是adOpenStatic 所以操作完成之后直接关闭m_Record,不需要调用m_Record->close()
	catch(_com_error &e)
	{
		_bstr_t bstrSource(e.Source());//出错源
		_bstr_t bstrDescription(e.Description());//错误描述
		CString c_errortemp;
		c_errortemp=(LPCSTR)bstrSource;
		c_errortemp += (LPCSTR)bstrDescription;
		AfxMessageBox(c_errortemp);
	}

}

⌨️ 快捷键说明

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