📄 madopacket.cpp
字号:
}
if (var.vt == VT_BSTR)
{
buf = (char *)(_bstr_t)var;
}
else
{
buf.Empty();
return buf;
}
return buf;
}
double mswADO::GetValue(char *field, double &buf)
{
_variant_t var;
try
{
var = m_pRecordset->GetCollect(_variant_t(field));
}
catch(_com_error &e)
{
GetError(e);
return 0;
}
if (var.vt == VT_R8)
{
buf = var.dblVal;
}
else
{
buf = 0;
return 0;
}
return buf;
}
float mswADO::GetValue(char *field, float &buf)
{
_variant_t var;
try
{
var = m_pRecordset->GetCollect(_variant_t(field));
}
catch(_com_error &e)
{
GetError(e);
return 0;
}
if (var.vt == VT_R4)
{
buf = var.fltVal;
}
else
{
buf = 0;
return 0;
}
return buf;
}
int mswADO::GetValue(char *field, int &buf)
{
_variant_t var;
m_strLastError.Empty();
try
{
var = m_pRecordset->GetCollect(_variant_t(field));
}
catch(_com_error &e)
{
GetError(e);
return buf;
}
if (var.vt == VT_I4)
{
buf = var.intVal;
}
else
{
buf = 0;
return 0;
}
return buf;
}
bool mswADO::SetValue(char *field, CString &buf)
{
_variant_t var;
var = _variant_t(buf);
try
{
m_pRecordset->PutCollect(_variant_t(field), var);
}
catch(_com_error &e)
{
GetError(e);
return false;
}
return true;
}
bool mswADO::SetValue(char *field, double buf)
{
_variant_t var;
var = _variant_t(buf);
try
{
m_pRecordset->PutCollect(_variant_t(field), var);
}
catch(_com_error &e)
{
GetError(e);
return false;
}
return true;
}
bool mswADO::SetValue(char *field, float buf)
{
_variant_t var;
var = _variant_t(buf);
try
{
m_pRecordset->PutCollect(_variant_t(field), var);
}
catch(_com_error &e)
{
GetError(e);
return false;
}
return true;
}
bool mswADO::SetValue(char *field, int buf)
{
_variant_t var;
//var = _variant_t(buf);
var.vt = VT_I4;
var.intVal = buf;
try
{
m_pRecordset->PutCollect(_variant_t(field), var);
}
catch(_com_error &e)
{
GetError(e);
return false;
}
return true;
}
void mswADO::SetServer(char *ser)
{
m_strServer = ser;
}
void mswADO::SetDatabase(char *db)
{
m_strDatabase = db;
}
void mswADO::SetUser(char *user)
{
m_strUser = user;
}
void mswADO::SetPW(char *pw)
{
m_strPW = pw;
}
void mswADO::SetTable(char *table)
{
m_strTable = table;
}
void mswADO::SetField(char *field)
{
m_strField = field;
}
bool mswADO::Connect(char *ser, char *db)
{
m_strServer = ser;
m_strDatabase = db;
CString str;
str.Format("Provider=SQLOLEDB.1;Integrated Security=SSPI;\
Persist Security Info=False;Initial Catalog=%s;Data Source=%s", m_strDatabase, m_strServer);
m_pConnection->ConnectionString = _bstr_t(str);//_bstr_t(str.GetBuffer(str.GetLength()));
m_strLastError.Empty();
try
{
m_pConnection->Open(m_pConnection->ConnectionString, "", "", adConnectUnspecified);
}
catch(_com_error &e)
{
GetError(e);
return false;
}
return true;
}
bool mswADO::OpenTable(char *table)
{
m_strTable = table;
CString str;
str.Format("SELECT * FROM %s", m_strTable.GetBuffer(m_strTable.GetLength()));
m_strLastError.Empty();
try
{
m_pRecordset->Open(_bstr_t(str), m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
}
catch(_com_error &e)
{
GetError(e);
m_lCount = 0;
m_lFieldCount = 0;
return false; }
m_lCount = GetCount();
m_lFieldCount = GetFieldCount();
return true;
}
bool mswADO::OpenTable(CString &table)
{
m_strTable = table;
CString str;
str.Format("SELECT * FROM %s", m_strTable.GetBuffer(m_strTable.GetLength()));
m_strLastError.Empty();
try
{
m_pRecordset->Open(_bstr_t(str), m_pConnection.GetInterfacePtr(), adOpenStatic, adLockOptimistic, adCmdText);
}
catch(_com_error &e)
{
GetError(e);
m_lCount = 0;
m_lFieldCount = 0;
return false;
}
m_lCount = GetCount();
m_lFieldCount = GetFieldCount();
return true;
}
bool mswADO::Connect(CString &ser, CString &db)
{
m_strServer = ser;
m_strDatabase = db;
CString str;
str.Format("Provider=SQLOLEDB.1;Integrated Security=SSPI;\
Persist Security Info=False;Initial Catalog=%s;Data Source=%s", m_strDatabase, m_strServer);
m_pConnection->ConnectionString = _bstr_t(str);//_bstr_t(str.GetBuffer(str.GetLength()));
m_strLastError.Empty();
try
{
m_pConnection->Open(m_pConnection->ConnectionString, "", "", adConnectUnspecified);
}
catch(_com_error &e)
{
GetError(e);
return false;
}
return true;
}
bool mswADO::IsEmpty()
{
if ((m_pRecordset->adoBOF) && (m_pRecordset->adoEOF))
{
return true;
}
else
{
return false;
}
}
long mswADO::GetCount()
{
long count = 0;
try
{
m_pRecordset->MoveFirst();
}
catch(...)
{
return 0;
}
if (m_pRecordset->adoEOF)
{
return 0;
}
while (!m_pRecordset->adoEOF)
{
m_pRecordset->MoveNext();
count = count + 1;
}
m_pRecordset->MoveFirst();
return count;
}
bool mswADO::Move(long num, int start)
{
return true;
}
long mswADO::GetFieldCount()
{
long count;
try
{
count = m_pRecordset->GetFields()->GetCount();
}
catch(_com_error &e)
{
GetError(e);
return 0;
}
return count;
}
bool mswADO::Move(long num)
{
try
{
m_pRecordset->MoveFirst();
m_pRecordset->Move(num);
}
catch(_com_error &e)
{
GetError(e);
return false;
}
return true;
}
void mswADO::CloseTable()
{
m_pRecordset->Close();
}
void mswADO::RefreshRec()
{
try
{
m_pRecordset->Requery(adCmdUnknown);
}
catch(_com_error &e)
{
GetError(e);
return;
}
}
bool mswADO::OpenTable(CString &cmd, enum CursorTypeEnum CursorType, enum LockTypeEnum LockType)
{
try
{
m_pRecordset->Open(_bstr_t(cmd), m_pConnection.GetInterfacePtr(), CursorType, LockType, adCmdText);
}
catch(_com_error &e)
{
GetError(e);
m_lCount = 0;
m_lFieldCount = 0;
return false;
}
m_lCount = GetCount();
m_lFieldCount = GetFieldCount();
return true;
}
void mswADO::GetError(_com_error &e)
{
CString str;
ErrorsPtr pErrors = m_pConnection->GetErrors();
if (pErrors->GetCount() == 0)
{
str = (char *)(_bstr_t)e.ErrorMessage();
::AfxMessageBox(str);
}
else
{
for (int i = 0; i < pErrors->GetCount(); i++)
{
_bstr_t desc = pErrors->GetItem((long)i)->GetDescription();
str = (char *)desc;
::AfxMessageBox(str);
}
}
}
bool mswADO::Next()
{
try
{
m_pRecordset->MoveNext();
}
catch(_com_error &e)
{
GetError(e);
return false;
}
return true;
}
bool mswADO::Previous()
{
try
{
m_pRecordset->MovePrevious();
}
catch(_com_error &e)
{
GetError(e);
return false;
}
return true;
}
bool mswADO::First()
{
try
{
m_pRecordset->MoveFirst();
}
catch(_com_error &e)
{
GetError(e);
return false;
}
return true;
}
bool mswADO::Last()
{
try
{
m_pRecordset->MoveLast();
}
catch(_com_error &e)
{
GetError(e);
return false;
}
return true;
}
bool mswADO::IsEOF()
{
if (m_pRecordset->adoEOF == 0)
{
return false;
}
else
{
return true;
}
}
bool mswADO::IsBOF()
{
if (m_pRecordset->adoBOF == 0)
{
return false;
}
else
{
return true;
}
}
_variant_t mswADO::GetValue(long index)
{
_variant_t var;
try
{
var = m_pRecordset->GetCollect(index);
}
catch(_com_error &e)
{
GetError(e);
// return var;
}
return var;
}
CString mswADO::GetFieldName(long index)
{
CString name;
_variant_t var;
if (index >= m_lFieldCount)
{
return "";
}
try
{
var = m_pRecordset->GetFields()->GetItem(index)->GetName();
}
catch(_com_error &e)
{
GetError(e);
return "";
}
if (var.vt == VT_EMPTY)
{
return "";
}
name = (char *)(_bstr_t)var;
name.TrimLeft();
name.TrimRight();
return name;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -