c语言可以连接access数据库??
1个回答
2013-10-19
展开全部
#import "C:\program files\common files\system\ado\msado15.dll " no_namespace rename( "EOF ", "adoEOF ")
//#import "c:\program files\common files\system\ado\msado15.dll " rename ( "EOF ", "adoEOF ") no_namespace
#define CREATEiNSTANCE(sp,riid) { HRESULT _hr =sp .CreateInstance( __uuidof( riid ) ); \
if (FAILED(_hr)) _com_issue_error(_hr); }
#define RsITEM(rs,x) ((rs-> Fields-> Item[_variant_t(x)]-> Value))
class CHmbAdoDB
{
public:
BOOL m_bOpen;
_ConnectionPtr m_DbConn;
CString m_ErrMsg;
CString m_SQLState;
long mSqlCode;
public:
void Cancel();
BOOL IsOpen();
void RollbackTrans();
void CommitTrans();
long BeginTrans();
void Close();
BOOL Open(LPCSTR lpszConnect);
BOOL Execute(LPCTSTR lpSQL);
void ProviderError();
CHmbAdoDB()
{
}
virtual ~CHmbAdoDB()
{
}
};
class CHmbAdoDBRs : public CObject
{
public:
_RecordsetPtr m_Rs;
public:
CHmbAdoDBRs();
virtual ~CHmbAdoDBRs();
BOOL Open( CHmbAdoDB *pDB, LPCSTR szSource,long option=-1);
void Close();
void GetRs(_variant_t x,_bstr_t &ret);
void GetRs(_variant_t x,CString &ret);
BOOL IsEOF();
BOOL IsOpen();
BOOL MoveNext();
BOOL MovePrev();
BOOL MoveFirst();
BOOL MoveLast();
};
#endif // !defined(AFX_RfidAdoDB_H__7C539954_1C62_47B6_AB6E_1A5BB73A67E5__INCLUDED_) truct InitOle
{
InitOle() { ::CoInitialize(NULL); }
~InitOle() { ::CoUninitialize(); }
} _init_InitOle_;
long mSqlCode;
BOOL CHmbAdoDB::Open(LPCSTR lpszConnect)
{
try
{
CREATEiNSTANCE(m_DbConn,Connection);
m_DbConn-> Open(lpszConnect, " ", " ", adModeUnknown );
}
catch( _com_error &e)
{
return FALSE;
}
catch(...)
{
return FALSE;
}
return TRUE;
}
void CHmbAdoDB::Close()
{
if( IsOpen() )
{
m_DbConn-> Close();
}
}
long CHmbAdoDB::BeginTrans()
{
return m_DbConn-> BeginTrans();
}
void CHmbAdoDB::CommitTrans()
{
m_DbConn-> CommitTrans();
}
void CHmbAdoDB::RollbackTrans()
{
m_DbConn-> RollbackTrans();
}
void CHmbAdoDB::Cancel()
{
m_DbConn-> Cancel();
}
BOOL CHmbAdoDB::IsOpen()
{
return ( (m_DbConn-> GetState() != 0) ? TRUE : FALSE );
}
BOOL CHmbAdoDB::Execute(LPCTSTR lpSQL)
{
if (!IsOpen())
{
AfxMessageBox( "Network error, Database do not open. ");
return FALSE;
}
CString strSQL(lpSQL);
strSQL.TrimLeft();
strSQL.TrimRight();
try
{
m_DbConn-> Execute(_bstr_t(strSQL), NULL, adExecuteNoRecords);
}
catch( _com_error &e)
{
return FALSE;
}
catch(...)
{
return FALSE;
}
return TRUE;
}
void CHmbAdoDB::ProviderError()
{
// Print Provider Errors from Connection object.
// pErr is a record object in the Connection 's Error collection.
ErrorPtr pErr = NULL;
if( (m_DbConn-> Errors-> Count) > 0)
{
long nCount = m_DbConn-> Errors-> Count;
// Collection ranges from 0 to nCount -1.
for(long i = 0; i < nCount; i++)
{
pErr = m_DbConn-> Errors-> GetItem(i);
mSqlCode = pErr-> NativeError;
m_ErrMsg.Format( "SQLCODE : %ld\t%s ",mSqlCode, LPCTSTR(pErr-> Description));
m_SQLState.Format( "%s ",(char*)pErr-> SQLState);
printf( "%s // %s\n ",m_SQLState,m_ErrMsg);
}
}
m_DbConn-> Errors-> Clear();
}
CHmbAdoDBRs::CHmbAdoDBRs( )
{
}
CHmbAdoDBRs::~CHmbAdoDBRs()
{
}
BOOL CHmbAdoDBRs::Open( CHmbAdoDB *pDB, LPCSTR szSource,long option )
{
CString strSQL;
strSQL = CString(szSource);
strSQL.TrimLeft();
strSQL.TrimRight();
_variant_t vRecsAffected(0L);
// CREATEiNSTANCE( m_Rs,Recordset) ;
// m_Rs-> PutRefActiveConnection( pDB-> m_DbConn );
try
{
m_Rs = pDB-> m_DbConn-> Execute(_bstr_t(strSQL),
&vRecsAffected,
adOptionUnspecified);
m_Rs-> AddRef();
// m_Rs-> Open(szSource, vtMissing, adOpenKeyset, adLockBatchOptimistic, adOptionUnspecified);
// m_Rs-> Open(szSource, vtMissing, adOpenKeyset, adLockReadOnly, option);
// m_Rs-> Open(szSource,pDB-> m_DbConn.GetInterfacePtr(),adOpenDynamic,adLockOptimistic, adCmdText);
}
catch( _com_error &e)
{
//ErrorMessage();
m_Rs-> Close();
m_Rs-> Release();
return FALSE;
}
catch(...)
{
m_Rs-> Close();
m_Rs-> Release();
return FALSE;
}
return TRUE;
}
void CHmbAdoDBRs::Close()
{
m_Rs-> Close();
m_Rs-> Release();
}
void CHmbAdoDBRs::GetRs(_variant_t x,_bstr_t &ret)
{
ret = (m_Rs-> Fields-> Item[x]-> Value);
}
void CHmbAdoDBRs::GetRs(_variant_t x,CString &ret)
{
_bstr_t retVal;
try{
retVal = (m_Rs-> Fields-> Item[x]-> Value);
}
catch(...){
ret = " ";
return;
}
ret = CString((char*) retVal);
}
BOOL CHmbAdoDBRs::IsEOF()
{
return m_Rs-> adoEOF;
}
BOOL CHmbAdoDBRs::IsOpen()
{
return ( (m_Rs-> GetState() != 0) ? TRUE : FALSE );
}
BOOL CHmbAdoDBRs::MoveNext() { return (FAILED(m_Rs-> MoveNext()) ? FALSE : TRUE ); }
BOOL CHmbAdoDBRs::MovePrev() { return (FAILED(m_Rs-> MovePrevious()) ? FALSE : TRUE ); }
BOOL CHmbAdoDBRs::MoveFirst() { return (FAILED(m_Rs-> MoveFirst()) ? FALSE : TRUE ) ; }
BOOL CHmbAdoDBRs::MoveLast() { return (FAILED(m_Rs-> MoveLast()) ? FALSE : TRUE ) ; }
以上为一个ADO的封装类.
c语言如何连接access数据库
现在一般是用ADO的方法.
//#import "c:\program files\common files\system\ado\msado15.dll " rename ( "EOF ", "adoEOF ") no_namespace
#define CREATEiNSTANCE(sp,riid) { HRESULT _hr =sp .CreateInstance( __uuidof( riid ) ); \
if (FAILED(_hr)) _com_issue_error(_hr); }
#define RsITEM(rs,x) ((rs-> Fields-> Item[_variant_t(x)]-> Value))
class CHmbAdoDB
{
public:
BOOL m_bOpen;
_ConnectionPtr m_DbConn;
CString m_ErrMsg;
CString m_SQLState;
long mSqlCode;
public:
void Cancel();
BOOL IsOpen();
void RollbackTrans();
void CommitTrans();
long BeginTrans();
void Close();
BOOL Open(LPCSTR lpszConnect);
BOOL Execute(LPCTSTR lpSQL);
void ProviderError();
CHmbAdoDB()
{
}
virtual ~CHmbAdoDB()
{
}
};
class CHmbAdoDBRs : public CObject
{
public:
_RecordsetPtr m_Rs;
public:
CHmbAdoDBRs();
virtual ~CHmbAdoDBRs();
BOOL Open( CHmbAdoDB *pDB, LPCSTR szSource,long option=-1);
void Close();
void GetRs(_variant_t x,_bstr_t &ret);
void GetRs(_variant_t x,CString &ret);
BOOL IsEOF();
BOOL IsOpen();
BOOL MoveNext();
BOOL MovePrev();
BOOL MoveFirst();
BOOL MoveLast();
};
#endif // !defined(AFX_RfidAdoDB_H__7C539954_1C62_47B6_AB6E_1A5BB73A67E5__INCLUDED_) truct InitOle
{
InitOle() { ::CoInitialize(NULL); }
~InitOle() { ::CoUninitialize(); }
} _init_InitOle_;
long mSqlCode;
BOOL CHmbAdoDB::Open(LPCSTR lpszConnect)
{
try
{
CREATEiNSTANCE(m_DbConn,Connection);
m_DbConn-> Open(lpszConnect, " ", " ", adModeUnknown );
}
catch( _com_error &e)
{
return FALSE;
}
catch(...)
{
return FALSE;
}
return TRUE;
}
void CHmbAdoDB::Close()
{
if( IsOpen() )
{
m_DbConn-> Close();
}
}
long CHmbAdoDB::BeginTrans()
{
return m_DbConn-> BeginTrans();
}
void CHmbAdoDB::CommitTrans()
{
m_DbConn-> CommitTrans();
}
void CHmbAdoDB::RollbackTrans()
{
m_DbConn-> RollbackTrans();
}
void CHmbAdoDB::Cancel()
{
m_DbConn-> Cancel();
}
BOOL CHmbAdoDB::IsOpen()
{
return ( (m_DbConn-> GetState() != 0) ? TRUE : FALSE );
}
BOOL CHmbAdoDB::Execute(LPCTSTR lpSQL)
{
if (!IsOpen())
{
AfxMessageBox( "Network error, Database do not open. ");
return FALSE;
}
CString strSQL(lpSQL);
strSQL.TrimLeft();
strSQL.TrimRight();
try
{
m_DbConn-> Execute(_bstr_t(strSQL), NULL, adExecuteNoRecords);
}
catch( _com_error &e)
{
return FALSE;
}
catch(...)
{
return FALSE;
}
return TRUE;
}
void CHmbAdoDB::ProviderError()
{
// Print Provider Errors from Connection object.
// pErr is a record object in the Connection 's Error collection.
ErrorPtr pErr = NULL;
if( (m_DbConn-> Errors-> Count) > 0)
{
long nCount = m_DbConn-> Errors-> Count;
// Collection ranges from 0 to nCount -1.
for(long i = 0; i < nCount; i++)
{
pErr = m_DbConn-> Errors-> GetItem(i);
mSqlCode = pErr-> NativeError;
m_ErrMsg.Format( "SQLCODE : %ld\t%s ",mSqlCode, LPCTSTR(pErr-> Description));
m_SQLState.Format( "%s ",(char*)pErr-> SQLState);
printf( "%s // %s\n ",m_SQLState,m_ErrMsg);
}
}
m_DbConn-> Errors-> Clear();
}
CHmbAdoDBRs::CHmbAdoDBRs( )
{
}
CHmbAdoDBRs::~CHmbAdoDBRs()
{
}
BOOL CHmbAdoDBRs::Open( CHmbAdoDB *pDB, LPCSTR szSource,long option )
{
CString strSQL;
strSQL = CString(szSource);
strSQL.TrimLeft();
strSQL.TrimRight();
_variant_t vRecsAffected(0L);
// CREATEiNSTANCE( m_Rs,Recordset) ;
// m_Rs-> PutRefActiveConnection( pDB-> m_DbConn );
try
{
m_Rs = pDB-> m_DbConn-> Execute(_bstr_t(strSQL),
&vRecsAffected,
adOptionUnspecified);
m_Rs-> AddRef();
// m_Rs-> Open(szSource, vtMissing, adOpenKeyset, adLockBatchOptimistic, adOptionUnspecified);
// m_Rs-> Open(szSource, vtMissing, adOpenKeyset, adLockReadOnly, option);
// m_Rs-> Open(szSource,pDB-> m_DbConn.GetInterfacePtr(),adOpenDynamic,adLockOptimistic, adCmdText);
}
catch( _com_error &e)
{
//ErrorMessage();
m_Rs-> Close();
m_Rs-> Release();
return FALSE;
}
catch(...)
{
m_Rs-> Close();
m_Rs-> Release();
return FALSE;
}
return TRUE;
}
void CHmbAdoDBRs::Close()
{
m_Rs-> Close();
m_Rs-> Release();
}
void CHmbAdoDBRs::GetRs(_variant_t x,_bstr_t &ret)
{
ret = (m_Rs-> Fields-> Item[x]-> Value);
}
void CHmbAdoDBRs::GetRs(_variant_t x,CString &ret)
{
_bstr_t retVal;
try{
retVal = (m_Rs-> Fields-> Item[x]-> Value);
}
catch(...){
ret = " ";
return;
}
ret = CString((char*) retVal);
}
BOOL CHmbAdoDBRs::IsEOF()
{
return m_Rs-> adoEOF;
}
BOOL CHmbAdoDBRs::IsOpen()
{
return ( (m_Rs-> GetState() != 0) ? TRUE : FALSE );
}
BOOL CHmbAdoDBRs::MoveNext() { return (FAILED(m_Rs-> MoveNext()) ? FALSE : TRUE ); }
BOOL CHmbAdoDBRs::MovePrev() { return (FAILED(m_Rs-> MovePrevious()) ? FALSE : TRUE ); }
BOOL CHmbAdoDBRs::MoveFirst() { return (FAILED(m_Rs-> MoveFirst()) ? FALSE : TRUE ) ; }
BOOL CHmbAdoDBRs::MoveLast() { return (FAILED(m_Rs-> MoveLast()) ? FALSE : TRUE ) ; }
以上为一个ADO的封装类.
c语言如何连接access数据库
现在一般是用ADO的方法.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
威孚半导体技术
2024-08-19 广告
2024-08-19 广告
威孚(苏州)半导体技术有限公司是一家专注生产、研发、销售晶圆传输设备整机模块(EFEM/SORTER)及核心零部件的高科技半导体公司。公司核心团队均拥有多年半导体行业从业经验,其中技术团队成员博士、硕士学历占比80%以上,依托丰富的软件底层...
点击进入详情页
本回答由威孚半导体技术提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询