CString strOpen = "DSN=AddressBook;UID=;PWD=";
m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection->Open((const char *)strOpen,"","",-1);
但要求,先在ODBC中建立访问ACCESS数据库的DSN,
觉得这比较麻烦,想直接写strOpen =””,将数据库等信息都包括在其中,
不知如何写:strOpen =”????????”,
5 个解决方案
#1
参见:http://www.csdn.net/develop/read_article.asp?id=682
#2
连接数据库查询手册(不仅仅适用于asp)(ghj1976 转贴)
出处:http://www.active.com.cn
正文:
下面将简单介绍一下几种ADO连接方式:ODBC DSN,ODBC DSN-Less,
OLE DB Provider,和"MS Remote" Provider.
1。ODBC DSN连接
I.DSN
oConn.Open "DSN=AdvWorks;" & _
"UID=Admin;" & _
"PWD=;"
注意:从MDAC2.1开始就不能够在使用这样的方式了,就是只把DSN文件名放在ConnectString中
你必须同时使用DSN,UID,PWD标志。例如下面的方式在MDAC 2.1中将会出错:
oConn.Open "AdvWorks"
II.File DSN
oConn.Open "FILEDSN=\somepath\mydb.dsn;" & _
"UID=Admin;" & _
"PWD=;"
III.ODBC DSN-Less Connections
a)ODBC Text Driver
oConn.Open _
"Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
"Dbq=\somepath\;" & _
"Extensions=asc,csv,tab,txt;" & _
"Persist Security Info=False"
注意:需要在SQL语句中指定使用到的文件名。例如:
oRs.Open "Select * From customer.csv", _
oConn, adOpenStatic, adLockReadOnly, adCmdText
b)ODBC Driver for Access
i)普通安全模式:
oConn.Open _
"Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=\somepath\mydb.mdb;" & _
"Uid=Admin;" & _
"Pwd=;"
ii)如果使用了System database:
oConn.Open _
"Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=\somepath\mydb.mdb;" & _
"SystemDB=\somepath\mydb.mdw;", _
"admin", ""
c)ODBC Driver for SQL Server
i)普通安全模式
oConn.Open "Driver={SQL Server};" & _
"Server=carl2;" & _
"Database=pubs;" & _
"Uid=sa;" & _
"Pwd=;"
ii)使用信任安全模式:
oConn.Open "Driver={SQL Server};" & _
"Server=carl2;" & _
"Database=pubs;" & _
"Uid=;" & _
"Pwd=;"
注意:要使用空白的Uid和Pwd
d)ODBC Driver for Oracle
i)使用现有的Oracle ODBC Driver from Microsoft:
oConn.Open _
"Driver={Microsoft ODBC for Oracle};" & _
"Server=OracleServer.world;" & _
"Uid=demo;" & _
"Pwd=demo;"
ii)使用老版本的Oracle ODBC Driver from Microsoft:
oConn.Open _
"Driver={Microsoft ODBC Driver for Oracle};" & _
"ConnectString=OracleServer.world;" & _
"Uid=demo;" & _
"Pwd=demo;"
IIII)使用微软的OLE DB Data Link Connections方式Data Link File (UDL)
a)使用绝对路径
oConn.Open "File Name=\somepath\pubs.udl;"
b)使用相对路径
oConn.Open "File Name=pubs.udl;"
V)OLE DB Provider Connections方式
a)OLE DB Provider for ODBC Databases
i)Access (Jet):
oConn.Open _
"Provider=MSDASQL;" & _
"Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=\somepath\mydb.mdb;" & _
"Uid=Admin;" & _
"Pwd=;"
ii)SQL Server:
oConn.Open _
"Provider=MSDASQL;" & _
"Driver={SQL Server};" & _
"Server=carl2;" & _
"Database=pubs;" & _
"Uid=sa;" & _
"Pwd=;"
b)OLE DB Provider for Microsoft Jet (Access)
i)普通安全模式:
oConn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=\somepath\mydb.mdb;" & _
"User Id=admin;" & _
"Password=;"
ii)如果使用了System database:
oConn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=\somepath\mydb.mdb;" & _
"Jet OLEDB:System Database=system.mdw;", _
"admin", ""
注意:当使用OLE DB Provider4.0版本是,需要把MDB和MDW文件转换成4.0的数据库格式
iii)如果MDB需要一个数据库密码的话:
oConn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=\somepath\mydb.mdb;" & _
"Jet OLEDB:Database Password=MyDbPassword;", _
"admin", ""
c)OLE DB Provider for Excel Spreadsheet:
oConn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=\somepath\expenses.xls;" & _
"Extended Properties=""Excel 8.0;HDR=Yes;"";"
注意: "HDR=Yes"表示在第一行中是行标题,在provider中将不把第一行包括入recordset中
d)OLE DB Provider for SQL Server
i)普通安全模式:
oConn.Open "Provider=sqloledb;" & _
"Network Library=DBMSSOCN;" & _
"Data Source=carl2;" & _
"Initial Catalog=pubs;" & _
"User Id=sa;" & _
"Password=;"
ii)使用信任安全模式:
oConn.Open "Provider=sqloledb;" & _
Network Library=DBMSSOCN;" & _
"Data Source=carl2;" & _
"Initial Catalog=pubs;" & _
"Trusted_Connection=yes;"
注意:"Network Library=DBMSSOCN"声明OLE DB使用TCP/IP替代Named Pipes.
e)OLE DB Provider for Oracle
oConn.Open "Provider=msdaora;" & _
"Data Source=OracleServer.world;" & _
"User Id=sa;" & _
"Password=;"
(VI)Remote OLE DB Provider Connections方式(就是我一直在研究的RDS方式哦,呵呵。):
a)MS Remote - Access (Jet)
i)ODBC DSN:
oConn.Open "Remote Server=http://carl2;" & _
"Remote Provider=MSDASQL;" & _
"DSN=AdvWorks;" & _
"Uid=admin" & _
"Pwd=;"
ii)OLE DB Provider:
oConn.Open "Provider=MS Remote;" & _
"Remote Server=http://carl2;" & _
"Remote Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=\somepath\mydb.mdb;", _
"admin", ""
iii)OLE DB Provider自定义事务对象
oConn.Open "Provider=MS Remote;" & _
"Remote Server=http://carl2;" & _
"Handler=MSDFMAP.Handler;" & _
"Data Source=MyAdvworksOLEDBConnectTag;"
b)MS Remote - SQL Server
i)ODBC DSN:
oConn.Open "Remote Server=http://carl2;" & _
"Remote Provider=MSDASQL;" & _
"Network Library=DBMSSOCN;" & _
"DSN=Pubs;" & _
"Uid=sa" & _
"Pwd=;"
ii)OLE DB Provider
oConn.Open "Provider=MS Remote;" & _
"Remote Server=http://carl2;" & _
"Remote Provider=SQLOLEDB;" & _
"Network Library=DBMSSOCN;" & _
"Data Source=carl2;" & _
"Initial Catalog=pubs;" & _
"User ID=sa;" & _
"Password=;"
出处:http://www.active.com.cn
正文:
下面将简单介绍一下几种ADO连接方式:ODBC DSN,ODBC DSN-Less,
OLE DB Provider,和"MS Remote" Provider.
1。ODBC DSN连接
I.DSN
oConn.Open "DSN=AdvWorks;" & _
"UID=Admin;" & _
"PWD=;"
注意:从MDAC2.1开始就不能够在使用这样的方式了,就是只把DSN文件名放在ConnectString中
你必须同时使用DSN,UID,PWD标志。例如下面的方式在MDAC 2.1中将会出错:
oConn.Open "AdvWorks"
II.File DSN
oConn.Open "FILEDSN=\somepath\mydb.dsn;" & _
"UID=Admin;" & _
"PWD=;"
III.ODBC DSN-Less Connections
a)ODBC Text Driver
oConn.Open _
"Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
"Dbq=\somepath\;" & _
"Extensions=asc,csv,tab,txt;" & _
"Persist Security Info=False"
注意:需要在SQL语句中指定使用到的文件名。例如:
oRs.Open "Select * From customer.csv", _
oConn, adOpenStatic, adLockReadOnly, adCmdText
b)ODBC Driver for Access
i)普通安全模式:
oConn.Open _
"Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=\somepath\mydb.mdb;" & _
"Uid=Admin;" & _
"Pwd=;"
ii)如果使用了System database:
oConn.Open _
"Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=\somepath\mydb.mdb;" & _
"SystemDB=\somepath\mydb.mdw;", _
"admin", ""
c)ODBC Driver for SQL Server
i)普通安全模式
oConn.Open "Driver={SQL Server};" & _
"Server=carl2;" & _
"Database=pubs;" & _
"Uid=sa;" & _
"Pwd=;"
ii)使用信任安全模式:
oConn.Open "Driver={SQL Server};" & _
"Server=carl2;" & _
"Database=pubs;" & _
"Uid=;" & _
"Pwd=;"
注意:要使用空白的Uid和Pwd
d)ODBC Driver for Oracle
i)使用现有的Oracle ODBC Driver from Microsoft:
oConn.Open _
"Driver={Microsoft ODBC for Oracle};" & _
"Server=OracleServer.world;" & _
"Uid=demo;" & _
"Pwd=demo;"
ii)使用老版本的Oracle ODBC Driver from Microsoft:
oConn.Open _
"Driver={Microsoft ODBC Driver for Oracle};" & _
"ConnectString=OracleServer.world;" & _
"Uid=demo;" & _
"Pwd=demo;"
IIII)使用微软的OLE DB Data Link Connections方式Data Link File (UDL)
a)使用绝对路径
oConn.Open "File Name=\somepath\pubs.udl;"
b)使用相对路径
oConn.Open "File Name=pubs.udl;"
V)OLE DB Provider Connections方式
a)OLE DB Provider for ODBC Databases
i)Access (Jet):
oConn.Open _
"Provider=MSDASQL;" & _
"Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=\somepath\mydb.mdb;" & _
"Uid=Admin;" & _
"Pwd=;"
ii)SQL Server:
oConn.Open _
"Provider=MSDASQL;" & _
"Driver={SQL Server};" & _
"Server=carl2;" & _
"Database=pubs;" & _
"Uid=sa;" & _
"Pwd=;"
b)OLE DB Provider for Microsoft Jet (Access)
i)普通安全模式:
oConn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=\somepath\mydb.mdb;" & _
"User Id=admin;" & _
"Password=;"
ii)如果使用了System database:
oConn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=\somepath\mydb.mdb;" & _
"Jet OLEDB:System Database=system.mdw;", _
"admin", ""
注意:当使用OLE DB Provider4.0版本是,需要把MDB和MDW文件转换成4.0的数据库格式
iii)如果MDB需要一个数据库密码的话:
oConn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=\somepath\mydb.mdb;" & _
"Jet OLEDB:Database Password=MyDbPassword;", _
"admin", ""
c)OLE DB Provider for Excel Spreadsheet:
oConn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=\somepath\expenses.xls;" & _
"Extended Properties=""Excel 8.0;HDR=Yes;"";"
注意: "HDR=Yes"表示在第一行中是行标题,在provider中将不把第一行包括入recordset中
d)OLE DB Provider for SQL Server
i)普通安全模式:
oConn.Open "Provider=sqloledb;" & _
"Network Library=DBMSSOCN;" & _
"Data Source=carl2;" & _
"Initial Catalog=pubs;" & _
"User Id=sa;" & _
"Password=;"
ii)使用信任安全模式:
oConn.Open "Provider=sqloledb;" & _
Network Library=DBMSSOCN;" & _
"Data Source=carl2;" & _
"Initial Catalog=pubs;" & _
"Trusted_Connection=yes;"
注意:"Network Library=DBMSSOCN"声明OLE DB使用TCP/IP替代Named Pipes.
e)OLE DB Provider for Oracle
oConn.Open "Provider=msdaora;" & _
"Data Source=OracleServer.world;" & _
"User Id=sa;" & _
"Password=;"
(VI)Remote OLE DB Provider Connections方式(就是我一直在研究的RDS方式哦,呵呵。):
a)MS Remote - Access (Jet)
i)ODBC DSN:
oConn.Open "Remote Server=http://carl2;" & _
"Remote Provider=MSDASQL;" & _
"DSN=AdvWorks;" & _
"Uid=admin" & _
"Pwd=;"
ii)OLE DB Provider:
oConn.Open "Provider=MS Remote;" & _
"Remote Server=http://carl2;" & _
"Remote Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=\somepath\mydb.mdb;", _
"admin", ""
iii)OLE DB Provider自定义事务对象
oConn.Open "Provider=MS Remote;" & _
"Remote Server=http://carl2;" & _
"Handler=MSDFMAP.Handler;" & _
"Data Source=MyAdvworksOLEDBConnectTag;"
b)MS Remote - SQL Server
i)ODBC DSN:
oConn.Open "Remote Server=http://carl2;" & _
"Remote Provider=MSDASQL;" & _
"Network Library=DBMSSOCN;" & _
"DSN=Pubs;" & _
"Uid=sa" & _
"Pwd=;"
ii)OLE DB Provider
oConn.Open "Provider=MS Remote;" & _
"Remote Server=http://carl2;" & _
"Remote Provider=SQLOLEDB;" & _
"Network Library=DBMSSOCN;" & _
"Data Source=carl2;" & _
"Initial Catalog=pubs;" & _
"User ID=sa;" & _
"Password=;"
#3
我写成这样,还是说连接不上,不知错在哪里?
CString strOpen ="Driver={Microsoft Access Driver (*.mdb)};Dbq=E:\DOWNLOAD\vc\pj_dbacc\pj\ado\AddressBook.mdb;Uid=;Pwd=";
try
{
m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection->Open((const char *)strOpen,"","",-1);
}
catch(...)
{
AfxMessageBox("数据库初始化错误,程序将关闭!");
return FALSE;
}
CString strOpen ="Driver={Microsoft Access Driver (*.mdb)};Dbq=E:\DOWNLOAD\vc\pj_dbacc\pj\ado\AddressBook.mdb;Uid=;Pwd=";
try
{
m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection->Open((const char *)strOpen,"","",-1);
}
catch(...)
{
AfxMessageBox("数据库初始化错误,程序将关闭!");
return FALSE;
}
#4
BOOL CGetemailApp::InitInstance()
{
// Initialize OLE libraries
if (!AfxOleInit())
{
AfxMessageBox("ole 出错");
return FALSE;
}
::CoInitialize( NULL );
}
int CGetemailApp::ExitInstance()
{
// TODO: Add your specialized code here and/or call the base class
::CoUninitialize();
return CWinApp::ExitInstance();
}
bool getrecordset()
{
char SqlStr[255],account[50];
char Error[500];
char putfilestr[300];
long recordnum;
m_pConnection = NULL;
m_pRecordset = NULL;
_variant_t TheEmail,ThePhone_office,TheAccount,TheUser_id;
char strEmail[50],strPhone_offiece[50],strAccount[50],strUser_id[50];
if(!OpenFileForReadWrite())
return ;
try{
m_pConnection.CreateInstance(__uuidof(Connection));
m_pRecordset.CreateInstance(__uuidof(Recordset));
recordnum=m_pConnection->Open("DSN=stat","test","test",-1);
sprintf(putfilestr,"%20s %30s %40s %30s\n","用户编码","用户帐号","用户Email","用户办公电话");
fwrite(putfilestr,strlen(putfilestr),1,fpout);
while(!feof(fpin))
{
fscanf(fpin,"%s",account);
sprintf(SqlStr,"select a.user_id,a.account, b.email,b.phone_office from userbasicinfo a,usercontactinfo b where a.account='%s' and b.user_id=a.user_id",account);
recordnum=m_pRecordset->Open(SqlStr,m_pConnection.GetInterfacePtr(),adOpenKeyset,adLockOptimistic,adCmdText);
recordnum=m_pRecordset->adoEOF;
//执行到此出错,为何。跳到catch(...)
if(!m_pRecordset->adoEOF)
{
TheUser_id=m_pRecordset->GetCollect("user_id");
TheAccount=m_pRecordset->GetCollect("account");
TheEmail=m_pRecordset->GetCollect("email");
ThePhone_office=m_pRecordset->GetCollect("phone_office");
sprintf(putfilestr,"%20s %30s %40s %30s\n",TheUser_id.bstrVal,TheAccount.bstrVal,TheEmail.bstrVal,ThePhone_office.bstrVal);
fwrite(putfilestr,strlen(putfilestr),1,fpout);
}//end if
m_pRecordset->Close();
}//end while
}//end try
catch(_com_error *e)
{
//CString Error = e->ErrorMessage();
//char Error[500];
strcpy(Error , (char*)e->Description());
AfxMessageBox(e->ErrorMessage());
}
catch(_com_error e)
{
//CString Error = e->ErrorMessage();
strcpy(Error , (char*)e.Description());
//AfxMessageBox(e->ErrorMessage());
AfxMessageBox(Error);
}
catch(...)
{
AfxMessageBox("ado 出错");
}
m_pRecordset->close();;
m_pConnection->Close();
m_pConnection->Release();
CloseAllFile();
// 执行到recordnum=m_pRecordset->adoEOF; 出错
}
{
// Initialize OLE libraries
if (!AfxOleInit())
{
AfxMessageBox("ole 出错");
return FALSE;
}
::CoInitialize( NULL );
}
int CGetemailApp::ExitInstance()
{
// TODO: Add your specialized code here and/or call the base class
::CoUninitialize();
return CWinApp::ExitInstance();
}
bool getrecordset()
{
char SqlStr[255],account[50];
char Error[500];
char putfilestr[300];
long recordnum;
m_pConnection = NULL;
m_pRecordset = NULL;
_variant_t TheEmail,ThePhone_office,TheAccount,TheUser_id;
char strEmail[50],strPhone_offiece[50],strAccount[50],strUser_id[50];
if(!OpenFileForReadWrite())
return ;
try{
m_pConnection.CreateInstance(__uuidof(Connection));
m_pRecordset.CreateInstance(__uuidof(Recordset));
recordnum=m_pConnection->Open("DSN=stat","test","test",-1);
sprintf(putfilestr,"%20s %30s %40s %30s\n","用户编码","用户帐号","用户Email","用户办公电话");
fwrite(putfilestr,strlen(putfilestr),1,fpout);
while(!feof(fpin))
{
fscanf(fpin,"%s",account);
sprintf(SqlStr,"select a.user_id,a.account, b.email,b.phone_office from userbasicinfo a,usercontactinfo b where a.account='%s' and b.user_id=a.user_id",account);
recordnum=m_pRecordset->Open(SqlStr,m_pConnection.GetInterfacePtr(),adOpenKeyset,adLockOptimistic,adCmdText);
recordnum=m_pRecordset->adoEOF;
//执行到此出错,为何。跳到catch(...)
if(!m_pRecordset->adoEOF)
{
TheUser_id=m_pRecordset->GetCollect("user_id");
TheAccount=m_pRecordset->GetCollect("account");
TheEmail=m_pRecordset->GetCollect("email");
ThePhone_office=m_pRecordset->GetCollect("phone_office");
sprintf(putfilestr,"%20s %30s %40s %30s\n",TheUser_id.bstrVal,TheAccount.bstrVal,TheEmail.bstrVal,ThePhone_office.bstrVal);
fwrite(putfilestr,strlen(putfilestr),1,fpout);
}//end if
m_pRecordset->Close();
}//end while
}//end try
catch(_com_error *e)
{
//CString Error = e->ErrorMessage();
//char Error[500];
strcpy(Error , (char*)e->Description());
AfxMessageBox(e->ErrorMessage());
}
catch(_com_error e)
{
//CString Error = e->ErrorMessage();
strcpy(Error , (char*)e.Description());
//AfxMessageBox(e->ErrorMessage());
AfxMessageBox(Error);
}
catch(...)
{
AfxMessageBox("ado 出错");
}
m_pRecordset->close();;
m_pConnection->Close();
m_pConnection->Release();
CloseAllFile();
// 执行到recordnum=m_pRecordset->adoEOF; 出错
}
#5
以下是我写的一ADO类,供你参考:
// Access.cpp: implementation of the CAccess class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Access.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CAccess::CAccess()
{
AfxOleInit();
pConn.CreateInstance(__uuidof(Connection));
blConnected=FALSE;
}
CAccess::~CAccess()
{
if(blConnected)
{
pConn->Close();
}
}
BOOL CAccess::ConnectDb(LPCTSTR psDbname,LPCTSTR psUid,LPCTSTR psPwd)
{
CString psConnect;
psConnect.Format("Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=%s",psDbname);
HRESULT hr;
try{
hr=pConn->Open(_bstr_t(psConnect),_bstr_t(L""),_bstr_t(L""),adModeUnknown);
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDesc(e.Description());
CString psError;
psError.Format("Source:%s\nDesc:%s\nNumber:%u",(LPCTSTR)bstrSource,(LPCTSTR)bstrDesc,e.Error);
AfxMessageBox(psError);
}
if(SUCCEEDED(hr))
{
blConnected=TRUE;
return TRUE;
}
else
return FALSE;
}
//CRecordset functions
long CRecordPtr::GetNumericValue(LPCTSTR psFieldname)
{
try{
_variant_t vValue=pRs->GetCollect(_bstr_t(psFieldname));
vValue.ChangeType(VT_I4);
return vValue.lVal;
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDesc(e.Description());
CString psError;
psError.Format("Source:%s\nDesc:%s\nNumber:%u",(LPCTSTR)bstrSource,(LPCTSTR)bstrDesc,e.Error);
AfxMessageBox(psError);
return NULL;
}
return 0;
}
CString CRecordPtr::GetStringValue(LPCTSTR psFieldname)
{
try{
_variant_t vValue;
vValue=pRs->GetCollect(_bstr_t(psFieldname));
vValue.ChangeType(VT_BSTR);
return vValue.bstrVal ;
}
catch(_com_error &e)
{
if(e.Error()==4200736) return "";
_bstr_t bstrSource(e.Source());
_bstr_t bstrDesc(e.Description());
CString psError;
psError.Format("Source:%s\nDesc:%s\nNumber:%u",(LPCTSTR)bstrSource,(LPCTSTR)bstrDesc,e.Error());
AfxMessageBox(psError);
return "";
}
}
//DEL void CRecordPtr::operator =(_RecordsetPtr ptrRs)
//DEL {
//DEL pRs=ptrRs;
//DEL }
BOOL CRecordPtr::MoveNext()
{
return (SUCCEEDED(pRs->MoveNext()))?TRUE:FALSE;
}
BOOL CRecordPtr::MovePrev()
{
return (SUCCEEDED(pRs->MovePrevious()))?TRUE:FALSE;
}
BOOL CRecordPtr::IsEof()
{
return (SUCCEEDED(pRs->adoEOF))?FALSE:TRUE;
}
CRecordPtr::~CRecordPtr()
{
if((char *)pRs->DataMember) pRs->Close();
}
CRecordPtr::CRecordPtr(_ConnectionPtr pCon)
{
pRs.CreateInstance (__uuidof(Recordset));
pRs->PutRefActiveConnection(pCon);
RemoveArray();
}
BOOL CRecordPtr::Open(LPCTSTR psSql)
{
// ASSERT(pConn);
try{
_bstr_t bstrSql(psSql);
_variant_t vNULL;
vNULL.vt =VT_ERROR;
vNULL.scode =DISP_E_PARAMNOTFOUND;
pRs->Open ((_variant_t)bstrSql,vNULL,adOpenForwardOnly,
adLockOptimistic,adCmdText);
return TRUE;
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDesc(e.Description());
CString psError;
psError.Format("Source:%s\nDesc:%s\nNumber:%u",(LPCTSTR)bstrSource,(LPCTSTR)bstrDesc,e.Error);
AfxMessageBox(psError);
return FALSE;
}
}
void CRecordPtr::SetActiveConnection(_ConnectionPtr pCon)
{
pRs->PutRefActiveConnection(pCon);
}
CRecordPtr::AddNew()
{
long lSize=saFieldname.GetSize();
COleSafeArray vaField,vaValue;
vaField.CreateOneDim(VT_VARIANT,lSize);
vaValue.CreateOneDim(VT_VARIANT,lSize);
try
{
long lIndex[1];
for(long l=0;l<lSize;l++)
{
lIndex[0]=l;
vaField.PutElement(lIndex,&((_variant_t)saFieldname.GetAt(l)));
vaValue.PutElement(lIndex,&((_variant_t)saFieldvalue.GetAt(l)));
}
pRs->AddNew(vaField,vaValue);
RemoveArray();
return TRUE;
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDesc(e.Description());
CString psError;
psError.Format("Source:%s\nDesc:%s\nNumber:%u",(LPCTSTR)bstrSource,(LPCTSTR)bstrDesc,e.Error);
AfxMessageBox(psError);
return FALSE;
}
}
CRecordPtr::RemoveArray()
{
saFieldname.RemoveAll();
saFieldvalue.RemoveAll();
}
int CRecordPtr::AddFileldValue(CString psField, CString psValue)
{
saFieldname.Add(psField);
saFieldvalue.Add(psValue);
return saFieldname.GetSize();
}
BOOL CRecordPtr::UpdateValue(LPCTSTR psFieldname, LPCTSTR psFieldvalue)
{
_bstr_t bstrField(psFieldname),bstrValue(psFieldvalue);
return (pRs->Update ((_variant_t)bstrField,(_variant_t)bstrValue))?TRUE:FALSE;
}
void CRecordPtr::Close()
{
pRs->Close();
}
BOOL CAccess::Execute(LPCTSTR psSql)
{
try{
_bstr_t bstrSql(psSql);
_variant_t vAffect(0L);
pConn->Execute(bstrSql,&vAffect,adOptionUnspecified);
return TRUE;
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDesc(e.Description());
CString psError;
psError.Format("Source:%s\nDesc:%s\nNumber:%u",(LPCTSTR)bstrSource,(LPCTSTR)bstrDesc,e.Error);
AfxMessageBox(psError);
return NULL;
}
}
// Access.cpp: implementation of the CAccess class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Access.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CAccess::CAccess()
{
AfxOleInit();
pConn.CreateInstance(__uuidof(Connection));
blConnected=FALSE;
}
CAccess::~CAccess()
{
if(blConnected)
{
pConn->Close();
}
}
BOOL CAccess::ConnectDb(LPCTSTR psDbname,LPCTSTR psUid,LPCTSTR psPwd)
{
CString psConnect;
psConnect.Format("Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=%s",psDbname);
HRESULT hr;
try{
hr=pConn->Open(_bstr_t(psConnect),_bstr_t(L""),_bstr_t(L""),adModeUnknown);
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDesc(e.Description());
CString psError;
psError.Format("Source:%s\nDesc:%s\nNumber:%u",(LPCTSTR)bstrSource,(LPCTSTR)bstrDesc,e.Error);
AfxMessageBox(psError);
}
if(SUCCEEDED(hr))
{
blConnected=TRUE;
return TRUE;
}
else
return FALSE;
}
//CRecordset functions
long CRecordPtr::GetNumericValue(LPCTSTR psFieldname)
{
try{
_variant_t vValue=pRs->GetCollect(_bstr_t(psFieldname));
vValue.ChangeType(VT_I4);
return vValue.lVal;
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDesc(e.Description());
CString psError;
psError.Format("Source:%s\nDesc:%s\nNumber:%u",(LPCTSTR)bstrSource,(LPCTSTR)bstrDesc,e.Error);
AfxMessageBox(psError);
return NULL;
}
return 0;
}
CString CRecordPtr::GetStringValue(LPCTSTR psFieldname)
{
try{
_variant_t vValue;
vValue=pRs->GetCollect(_bstr_t(psFieldname));
vValue.ChangeType(VT_BSTR);
return vValue.bstrVal ;
}
catch(_com_error &e)
{
if(e.Error()==4200736) return "";
_bstr_t bstrSource(e.Source());
_bstr_t bstrDesc(e.Description());
CString psError;
psError.Format("Source:%s\nDesc:%s\nNumber:%u",(LPCTSTR)bstrSource,(LPCTSTR)bstrDesc,e.Error());
AfxMessageBox(psError);
return "";
}
}
//DEL void CRecordPtr::operator =(_RecordsetPtr ptrRs)
//DEL {
//DEL pRs=ptrRs;
//DEL }
BOOL CRecordPtr::MoveNext()
{
return (SUCCEEDED(pRs->MoveNext()))?TRUE:FALSE;
}
BOOL CRecordPtr::MovePrev()
{
return (SUCCEEDED(pRs->MovePrevious()))?TRUE:FALSE;
}
BOOL CRecordPtr::IsEof()
{
return (SUCCEEDED(pRs->adoEOF))?FALSE:TRUE;
}
CRecordPtr::~CRecordPtr()
{
if((char *)pRs->DataMember) pRs->Close();
}
CRecordPtr::CRecordPtr(_ConnectionPtr pCon)
{
pRs.CreateInstance (__uuidof(Recordset));
pRs->PutRefActiveConnection(pCon);
RemoveArray();
}
BOOL CRecordPtr::Open(LPCTSTR psSql)
{
// ASSERT(pConn);
try{
_bstr_t bstrSql(psSql);
_variant_t vNULL;
vNULL.vt =VT_ERROR;
vNULL.scode =DISP_E_PARAMNOTFOUND;
pRs->Open ((_variant_t)bstrSql,vNULL,adOpenForwardOnly,
adLockOptimistic,adCmdText);
return TRUE;
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDesc(e.Description());
CString psError;
psError.Format("Source:%s\nDesc:%s\nNumber:%u",(LPCTSTR)bstrSource,(LPCTSTR)bstrDesc,e.Error);
AfxMessageBox(psError);
return FALSE;
}
}
void CRecordPtr::SetActiveConnection(_ConnectionPtr pCon)
{
pRs->PutRefActiveConnection(pCon);
}
CRecordPtr::AddNew()
{
long lSize=saFieldname.GetSize();
COleSafeArray vaField,vaValue;
vaField.CreateOneDim(VT_VARIANT,lSize);
vaValue.CreateOneDim(VT_VARIANT,lSize);
try
{
long lIndex[1];
for(long l=0;l<lSize;l++)
{
lIndex[0]=l;
vaField.PutElement(lIndex,&((_variant_t)saFieldname.GetAt(l)));
vaValue.PutElement(lIndex,&((_variant_t)saFieldvalue.GetAt(l)));
}
pRs->AddNew(vaField,vaValue);
RemoveArray();
return TRUE;
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDesc(e.Description());
CString psError;
psError.Format("Source:%s\nDesc:%s\nNumber:%u",(LPCTSTR)bstrSource,(LPCTSTR)bstrDesc,e.Error);
AfxMessageBox(psError);
return FALSE;
}
}
CRecordPtr::RemoveArray()
{
saFieldname.RemoveAll();
saFieldvalue.RemoveAll();
}
int CRecordPtr::AddFileldValue(CString psField, CString psValue)
{
saFieldname.Add(psField);
saFieldvalue.Add(psValue);
return saFieldname.GetSize();
}
BOOL CRecordPtr::UpdateValue(LPCTSTR psFieldname, LPCTSTR psFieldvalue)
{
_bstr_t bstrField(psFieldname),bstrValue(psFieldvalue);
return (pRs->Update ((_variant_t)bstrField,(_variant_t)bstrValue))?TRUE:FALSE;
}
void CRecordPtr::Close()
{
pRs->Close();
}
BOOL CAccess::Execute(LPCTSTR psSql)
{
try{
_bstr_t bstrSql(psSql);
_variant_t vAffect(0L);
pConn->Execute(bstrSql,&vAffect,adOptionUnspecified);
return TRUE;
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDesc(e.Description());
CString psError;
psError.Format("Source:%s\nDesc:%s\nNumber:%u",(LPCTSTR)bstrSource,(LPCTSTR)bstrDesc,e.Error);
AfxMessageBox(psError);
return NULL;
}
}
#1
参见:http://www.csdn.net/develop/read_article.asp?id=682
#2
连接数据库查询手册(不仅仅适用于asp)(ghj1976 转贴)
出处:http://www.active.com.cn
正文:
下面将简单介绍一下几种ADO连接方式:ODBC DSN,ODBC DSN-Less,
OLE DB Provider,和"MS Remote" Provider.
1。ODBC DSN连接
I.DSN
oConn.Open "DSN=AdvWorks;" & _
"UID=Admin;" & _
"PWD=;"
注意:从MDAC2.1开始就不能够在使用这样的方式了,就是只把DSN文件名放在ConnectString中
你必须同时使用DSN,UID,PWD标志。例如下面的方式在MDAC 2.1中将会出错:
oConn.Open "AdvWorks"
II.File DSN
oConn.Open "FILEDSN=\somepath\mydb.dsn;" & _
"UID=Admin;" & _
"PWD=;"
III.ODBC DSN-Less Connections
a)ODBC Text Driver
oConn.Open _
"Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
"Dbq=\somepath\;" & _
"Extensions=asc,csv,tab,txt;" & _
"Persist Security Info=False"
注意:需要在SQL语句中指定使用到的文件名。例如:
oRs.Open "Select * From customer.csv", _
oConn, adOpenStatic, adLockReadOnly, adCmdText
b)ODBC Driver for Access
i)普通安全模式:
oConn.Open _
"Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=\somepath\mydb.mdb;" & _
"Uid=Admin;" & _
"Pwd=;"
ii)如果使用了System database:
oConn.Open _
"Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=\somepath\mydb.mdb;" & _
"SystemDB=\somepath\mydb.mdw;", _
"admin", ""
c)ODBC Driver for SQL Server
i)普通安全模式
oConn.Open "Driver={SQL Server};" & _
"Server=carl2;" & _
"Database=pubs;" & _
"Uid=sa;" & _
"Pwd=;"
ii)使用信任安全模式:
oConn.Open "Driver={SQL Server};" & _
"Server=carl2;" & _
"Database=pubs;" & _
"Uid=;" & _
"Pwd=;"
注意:要使用空白的Uid和Pwd
d)ODBC Driver for Oracle
i)使用现有的Oracle ODBC Driver from Microsoft:
oConn.Open _
"Driver={Microsoft ODBC for Oracle};" & _
"Server=OracleServer.world;" & _
"Uid=demo;" & _
"Pwd=demo;"
ii)使用老版本的Oracle ODBC Driver from Microsoft:
oConn.Open _
"Driver={Microsoft ODBC Driver for Oracle};" & _
"ConnectString=OracleServer.world;" & _
"Uid=demo;" & _
"Pwd=demo;"
IIII)使用微软的OLE DB Data Link Connections方式Data Link File (UDL)
a)使用绝对路径
oConn.Open "File Name=\somepath\pubs.udl;"
b)使用相对路径
oConn.Open "File Name=pubs.udl;"
V)OLE DB Provider Connections方式
a)OLE DB Provider for ODBC Databases
i)Access (Jet):
oConn.Open _
"Provider=MSDASQL;" & _
"Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=\somepath\mydb.mdb;" & _
"Uid=Admin;" & _
"Pwd=;"
ii)SQL Server:
oConn.Open _
"Provider=MSDASQL;" & _
"Driver={SQL Server};" & _
"Server=carl2;" & _
"Database=pubs;" & _
"Uid=sa;" & _
"Pwd=;"
b)OLE DB Provider for Microsoft Jet (Access)
i)普通安全模式:
oConn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=\somepath\mydb.mdb;" & _
"User Id=admin;" & _
"Password=;"
ii)如果使用了System database:
oConn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=\somepath\mydb.mdb;" & _
"Jet OLEDB:System Database=system.mdw;", _
"admin", ""
注意:当使用OLE DB Provider4.0版本是,需要把MDB和MDW文件转换成4.0的数据库格式
iii)如果MDB需要一个数据库密码的话:
oConn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=\somepath\mydb.mdb;" & _
"Jet OLEDB:Database Password=MyDbPassword;", _
"admin", ""
c)OLE DB Provider for Excel Spreadsheet:
oConn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=\somepath\expenses.xls;" & _
"Extended Properties=""Excel 8.0;HDR=Yes;"";"
注意: "HDR=Yes"表示在第一行中是行标题,在provider中将不把第一行包括入recordset中
d)OLE DB Provider for SQL Server
i)普通安全模式:
oConn.Open "Provider=sqloledb;" & _
"Network Library=DBMSSOCN;" & _
"Data Source=carl2;" & _
"Initial Catalog=pubs;" & _
"User Id=sa;" & _
"Password=;"
ii)使用信任安全模式:
oConn.Open "Provider=sqloledb;" & _
Network Library=DBMSSOCN;" & _
"Data Source=carl2;" & _
"Initial Catalog=pubs;" & _
"Trusted_Connection=yes;"
注意:"Network Library=DBMSSOCN"声明OLE DB使用TCP/IP替代Named Pipes.
e)OLE DB Provider for Oracle
oConn.Open "Provider=msdaora;" & _
"Data Source=OracleServer.world;" & _
"User Id=sa;" & _
"Password=;"
(VI)Remote OLE DB Provider Connections方式(就是我一直在研究的RDS方式哦,呵呵。):
a)MS Remote - Access (Jet)
i)ODBC DSN:
oConn.Open "Remote Server=http://carl2;" & _
"Remote Provider=MSDASQL;" & _
"DSN=AdvWorks;" & _
"Uid=admin" & _
"Pwd=;"
ii)OLE DB Provider:
oConn.Open "Provider=MS Remote;" & _
"Remote Server=http://carl2;" & _
"Remote Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=\somepath\mydb.mdb;", _
"admin", ""
iii)OLE DB Provider自定义事务对象
oConn.Open "Provider=MS Remote;" & _
"Remote Server=http://carl2;" & _
"Handler=MSDFMAP.Handler;" & _
"Data Source=MyAdvworksOLEDBConnectTag;"
b)MS Remote - SQL Server
i)ODBC DSN:
oConn.Open "Remote Server=http://carl2;" & _
"Remote Provider=MSDASQL;" & _
"Network Library=DBMSSOCN;" & _
"DSN=Pubs;" & _
"Uid=sa" & _
"Pwd=;"
ii)OLE DB Provider
oConn.Open "Provider=MS Remote;" & _
"Remote Server=http://carl2;" & _
"Remote Provider=SQLOLEDB;" & _
"Network Library=DBMSSOCN;" & _
"Data Source=carl2;" & _
"Initial Catalog=pubs;" & _
"User ID=sa;" & _
"Password=;"
出处:http://www.active.com.cn
正文:
下面将简单介绍一下几种ADO连接方式:ODBC DSN,ODBC DSN-Less,
OLE DB Provider,和"MS Remote" Provider.
1。ODBC DSN连接
I.DSN
oConn.Open "DSN=AdvWorks;" & _
"UID=Admin;" & _
"PWD=;"
注意:从MDAC2.1开始就不能够在使用这样的方式了,就是只把DSN文件名放在ConnectString中
你必须同时使用DSN,UID,PWD标志。例如下面的方式在MDAC 2.1中将会出错:
oConn.Open "AdvWorks"
II.File DSN
oConn.Open "FILEDSN=\somepath\mydb.dsn;" & _
"UID=Admin;" & _
"PWD=;"
III.ODBC DSN-Less Connections
a)ODBC Text Driver
oConn.Open _
"Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
"Dbq=\somepath\;" & _
"Extensions=asc,csv,tab,txt;" & _
"Persist Security Info=False"
注意:需要在SQL语句中指定使用到的文件名。例如:
oRs.Open "Select * From customer.csv", _
oConn, adOpenStatic, adLockReadOnly, adCmdText
b)ODBC Driver for Access
i)普通安全模式:
oConn.Open _
"Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=\somepath\mydb.mdb;" & _
"Uid=Admin;" & _
"Pwd=;"
ii)如果使用了System database:
oConn.Open _
"Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=\somepath\mydb.mdb;" & _
"SystemDB=\somepath\mydb.mdw;", _
"admin", ""
c)ODBC Driver for SQL Server
i)普通安全模式
oConn.Open "Driver={SQL Server};" & _
"Server=carl2;" & _
"Database=pubs;" & _
"Uid=sa;" & _
"Pwd=;"
ii)使用信任安全模式:
oConn.Open "Driver={SQL Server};" & _
"Server=carl2;" & _
"Database=pubs;" & _
"Uid=;" & _
"Pwd=;"
注意:要使用空白的Uid和Pwd
d)ODBC Driver for Oracle
i)使用现有的Oracle ODBC Driver from Microsoft:
oConn.Open _
"Driver={Microsoft ODBC for Oracle};" & _
"Server=OracleServer.world;" & _
"Uid=demo;" & _
"Pwd=demo;"
ii)使用老版本的Oracle ODBC Driver from Microsoft:
oConn.Open _
"Driver={Microsoft ODBC Driver for Oracle};" & _
"ConnectString=OracleServer.world;" & _
"Uid=demo;" & _
"Pwd=demo;"
IIII)使用微软的OLE DB Data Link Connections方式Data Link File (UDL)
a)使用绝对路径
oConn.Open "File Name=\somepath\pubs.udl;"
b)使用相对路径
oConn.Open "File Name=pubs.udl;"
V)OLE DB Provider Connections方式
a)OLE DB Provider for ODBC Databases
i)Access (Jet):
oConn.Open _
"Provider=MSDASQL;" & _
"Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=\somepath\mydb.mdb;" & _
"Uid=Admin;" & _
"Pwd=;"
ii)SQL Server:
oConn.Open _
"Provider=MSDASQL;" & _
"Driver={SQL Server};" & _
"Server=carl2;" & _
"Database=pubs;" & _
"Uid=sa;" & _
"Pwd=;"
b)OLE DB Provider for Microsoft Jet (Access)
i)普通安全模式:
oConn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=\somepath\mydb.mdb;" & _
"User Id=admin;" & _
"Password=;"
ii)如果使用了System database:
oConn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=\somepath\mydb.mdb;" & _
"Jet OLEDB:System Database=system.mdw;", _
"admin", ""
注意:当使用OLE DB Provider4.0版本是,需要把MDB和MDW文件转换成4.0的数据库格式
iii)如果MDB需要一个数据库密码的话:
oConn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=\somepath\mydb.mdb;" & _
"Jet OLEDB:Database Password=MyDbPassword;", _
"admin", ""
c)OLE DB Provider for Excel Spreadsheet:
oConn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=\somepath\expenses.xls;" & _
"Extended Properties=""Excel 8.0;HDR=Yes;"";"
注意: "HDR=Yes"表示在第一行中是行标题,在provider中将不把第一行包括入recordset中
d)OLE DB Provider for SQL Server
i)普通安全模式:
oConn.Open "Provider=sqloledb;" & _
"Network Library=DBMSSOCN;" & _
"Data Source=carl2;" & _
"Initial Catalog=pubs;" & _
"User Id=sa;" & _
"Password=;"
ii)使用信任安全模式:
oConn.Open "Provider=sqloledb;" & _
Network Library=DBMSSOCN;" & _
"Data Source=carl2;" & _
"Initial Catalog=pubs;" & _
"Trusted_Connection=yes;"
注意:"Network Library=DBMSSOCN"声明OLE DB使用TCP/IP替代Named Pipes.
e)OLE DB Provider for Oracle
oConn.Open "Provider=msdaora;" & _
"Data Source=OracleServer.world;" & _
"User Id=sa;" & _
"Password=;"
(VI)Remote OLE DB Provider Connections方式(就是我一直在研究的RDS方式哦,呵呵。):
a)MS Remote - Access (Jet)
i)ODBC DSN:
oConn.Open "Remote Server=http://carl2;" & _
"Remote Provider=MSDASQL;" & _
"DSN=AdvWorks;" & _
"Uid=admin" & _
"Pwd=;"
ii)OLE DB Provider:
oConn.Open "Provider=MS Remote;" & _
"Remote Server=http://carl2;" & _
"Remote Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=\somepath\mydb.mdb;", _
"admin", ""
iii)OLE DB Provider自定义事务对象
oConn.Open "Provider=MS Remote;" & _
"Remote Server=http://carl2;" & _
"Handler=MSDFMAP.Handler;" & _
"Data Source=MyAdvworksOLEDBConnectTag;"
b)MS Remote - SQL Server
i)ODBC DSN:
oConn.Open "Remote Server=http://carl2;" & _
"Remote Provider=MSDASQL;" & _
"Network Library=DBMSSOCN;" & _
"DSN=Pubs;" & _
"Uid=sa" & _
"Pwd=;"
ii)OLE DB Provider
oConn.Open "Provider=MS Remote;" & _
"Remote Server=http://carl2;" & _
"Remote Provider=SQLOLEDB;" & _
"Network Library=DBMSSOCN;" & _
"Data Source=carl2;" & _
"Initial Catalog=pubs;" & _
"User ID=sa;" & _
"Password=;"
#3
我写成这样,还是说连接不上,不知错在哪里?
CString strOpen ="Driver={Microsoft Access Driver (*.mdb)};Dbq=E:\DOWNLOAD\vc\pj_dbacc\pj\ado\AddressBook.mdb;Uid=;Pwd=";
try
{
m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection->Open((const char *)strOpen,"","",-1);
}
catch(...)
{
AfxMessageBox("数据库初始化错误,程序将关闭!");
return FALSE;
}
CString strOpen ="Driver={Microsoft Access Driver (*.mdb)};Dbq=E:\DOWNLOAD\vc\pj_dbacc\pj\ado\AddressBook.mdb;Uid=;Pwd=";
try
{
m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection->Open((const char *)strOpen,"","",-1);
}
catch(...)
{
AfxMessageBox("数据库初始化错误,程序将关闭!");
return FALSE;
}
#4
BOOL CGetemailApp::InitInstance()
{
// Initialize OLE libraries
if (!AfxOleInit())
{
AfxMessageBox("ole 出错");
return FALSE;
}
::CoInitialize( NULL );
}
int CGetemailApp::ExitInstance()
{
// TODO: Add your specialized code here and/or call the base class
::CoUninitialize();
return CWinApp::ExitInstance();
}
bool getrecordset()
{
char SqlStr[255],account[50];
char Error[500];
char putfilestr[300];
long recordnum;
m_pConnection = NULL;
m_pRecordset = NULL;
_variant_t TheEmail,ThePhone_office,TheAccount,TheUser_id;
char strEmail[50],strPhone_offiece[50],strAccount[50],strUser_id[50];
if(!OpenFileForReadWrite())
return ;
try{
m_pConnection.CreateInstance(__uuidof(Connection));
m_pRecordset.CreateInstance(__uuidof(Recordset));
recordnum=m_pConnection->Open("DSN=stat","test","test",-1);
sprintf(putfilestr,"%20s %30s %40s %30s\n","用户编码","用户帐号","用户Email","用户办公电话");
fwrite(putfilestr,strlen(putfilestr),1,fpout);
while(!feof(fpin))
{
fscanf(fpin,"%s",account);
sprintf(SqlStr,"select a.user_id,a.account, b.email,b.phone_office from userbasicinfo a,usercontactinfo b where a.account='%s' and b.user_id=a.user_id",account);
recordnum=m_pRecordset->Open(SqlStr,m_pConnection.GetInterfacePtr(),adOpenKeyset,adLockOptimistic,adCmdText);
recordnum=m_pRecordset->adoEOF;
//执行到此出错,为何。跳到catch(...)
if(!m_pRecordset->adoEOF)
{
TheUser_id=m_pRecordset->GetCollect("user_id");
TheAccount=m_pRecordset->GetCollect("account");
TheEmail=m_pRecordset->GetCollect("email");
ThePhone_office=m_pRecordset->GetCollect("phone_office");
sprintf(putfilestr,"%20s %30s %40s %30s\n",TheUser_id.bstrVal,TheAccount.bstrVal,TheEmail.bstrVal,ThePhone_office.bstrVal);
fwrite(putfilestr,strlen(putfilestr),1,fpout);
}//end if
m_pRecordset->Close();
}//end while
}//end try
catch(_com_error *e)
{
//CString Error = e->ErrorMessage();
//char Error[500];
strcpy(Error , (char*)e->Description());
AfxMessageBox(e->ErrorMessage());
}
catch(_com_error e)
{
//CString Error = e->ErrorMessage();
strcpy(Error , (char*)e.Description());
//AfxMessageBox(e->ErrorMessage());
AfxMessageBox(Error);
}
catch(...)
{
AfxMessageBox("ado 出错");
}
m_pRecordset->close();;
m_pConnection->Close();
m_pConnection->Release();
CloseAllFile();
// 执行到recordnum=m_pRecordset->adoEOF; 出错
}
{
// Initialize OLE libraries
if (!AfxOleInit())
{
AfxMessageBox("ole 出错");
return FALSE;
}
::CoInitialize( NULL );
}
int CGetemailApp::ExitInstance()
{
// TODO: Add your specialized code here and/or call the base class
::CoUninitialize();
return CWinApp::ExitInstance();
}
bool getrecordset()
{
char SqlStr[255],account[50];
char Error[500];
char putfilestr[300];
long recordnum;
m_pConnection = NULL;
m_pRecordset = NULL;
_variant_t TheEmail,ThePhone_office,TheAccount,TheUser_id;
char strEmail[50],strPhone_offiece[50],strAccount[50],strUser_id[50];
if(!OpenFileForReadWrite())
return ;
try{
m_pConnection.CreateInstance(__uuidof(Connection));
m_pRecordset.CreateInstance(__uuidof(Recordset));
recordnum=m_pConnection->Open("DSN=stat","test","test",-1);
sprintf(putfilestr,"%20s %30s %40s %30s\n","用户编码","用户帐号","用户Email","用户办公电话");
fwrite(putfilestr,strlen(putfilestr),1,fpout);
while(!feof(fpin))
{
fscanf(fpin,"%s",account);
sprintf(SqlStr,"select a.user_id,a.account, b.email,b.phone_office from userbasicinfo a,usercontactinfo b where a.account='%s' and b.user_id=a.user_id",account);
recordnum=m_pRecordset->Open(SqlStr,m_pConnection.GetInterfacePtr(),adOpenKeyset,adLockOptimistic,adCmdText);
recordnum=m_pRecordset->adoEOF;
//执行到此出错,为何。跳到catch(...)
if(!m_pRecordset->adoEOF)
{
TheUser_id=m_pRecordset->GetCollect("user_id");
TheAccount=m_pRecordset->GetCollect("account");
TheEmail=m_pRecordset->GetCollect("email");
ThePhone_office=m_pRecordset->GetCollect("phone_office");
sprintf(putfilestr,"%20s %30s %40s %30s\n",TheUser_id.bstrVal,TheAccount.bstrVal,TheEmail.bstrVal,ThePhone_office.bstrVal);
fwrite(putfilestr,strlen(putfilestr),1,fpout);
}//end if
m_pRecordset->Close();
}//end while
}//end try
catch(_com_error *e)
{
//CString Error = e->ErrorMessage();
//char Error[500];
strcpy(Error , (char*)e->Description());
AfxMessageBox(e->ErrorMessage());
}
catch(_com_error e)
{
//CString Error = e->ErrorMessage();
strcpy(Error , (char*)e.Description());
//AfxMessageBox(e->ErrorMessage());
AfxMessageBox(Error);
}
catch(...)
{
AfxMessageBox("ado 出错");
}
m_pRecordset->close();;
m_pConnection->Close();
m_pConnection->Release();
CloseAllFile();
// 执行到recordnum=m_pRecordset->adoEOF; 出错
}
#5
以下是我写的一ADO类,供你参考:
// Access.cpp: implementation of the CAccess class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Access.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CAccess::CAccess()
{
AfxOleInit();
pConn.CreateInstance(__uuidof(Connection));
blConnected=FALSE;
}
CAccess::~CAccess()
{
if(blConnected)
{
pConn->Close();
}
}
BOOL CAccess::ConnectDb(LPCTSTR psDbname,LPCTSTR psUid,LPCTSTR psPwd)
{
CString psConnect;
psConnect.Format("Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=%s",psDbname);
HRESULT hr;
try{
hr=pConn->Open(_bstr_t(psConnect),_bstr_t(L""),_bstr_t(L""),adModeUnknown);
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDesc(e.Description());
CString psError;
psError.Format("Source:%s\nDesc:%s\nNumber:%u",(LPCTSTR)bstrSource,(LPCTSTR)bstrDesc,e.Error);
AfxMessageBox(psError);
}
if(SUCCEEDED(hr))
{
blConnected=TRUE;
return TRUE;
}
else
return FALSE;
}
//CRecordset functions
long CRecordPtr::GetNumericValue(LPCTSTR psFieldname)
{
try{
_variant_t vValue=pRs->GetCollect(_bstr_t(psFieldname));
vValue.ChangeType(VT_I4);
return vValue.lVal;
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDesc(e.Description());
CString psError;
psError.Format("Source:%s\nDesc:%s\nNumber:%u",(LPCTSTR)bstrSource,(LPCTSTR)bstrDesc,e.Error);
AfxMessageBox(psError);
return NULL;
}
return 0;
}
CString CRecordPtr::GetStringValue(LPCTSTR psFieldname)
{
try{
_variant_t vValue;
vValue=pRs->GetCollect(_bstr_t(psFieldname));
vValue.ChangeType(VT_BSTR);
return vValue.bstrVal ;
}
catch(_com_error &e)
{
if(e.Error()==4200736) return "";
_bstr_t bstrSource(e.Source());
_bstr_t bstrDesc(e.Description());
CString psError;
psError.Format("Source:%s\nDesc:%s\nNumber:%u",(LPCTSTR)bstrSource,(LPCTSTR)bstrDesc,e.Error());
AfxMessageBox(psError);
return "";
}
}
//DEL void CRecordPtr::operator =(_RecordsetPtr ptrRs)
//DEL {
//DEL pRs=ptrRs;
//DEL }
BOOL CRecordPtr::MoveNext()
{
return (SUCCEEDED(pRs->MoveNext()))?TRUE:FALSE;
}
BOOL CRecordPtr::MovePrev()
{
return (SUCCEEDED(pRs->MovePrevious()))?TRUE:FALSE;
}
BOOL CRecordPtr::IsEof()
{
return (SUCCEEDED(pRs->adoEOF))?FALSE:TRUE;
}
CRecordPtr::~CRecordPtr()
{
if((char *)pRs->DataMember) pRs->Close();
}
CRecordPtr::CRecordPtr(_ConnectionPtr pCon)
{
pRs.CreateInstance (__uuidof(Recordset));
pRs->PutRefActiveConnection(pCon);
RemoveArray();
}
BOOL CRecordPtr::Open(LPCTSTR psSql)
{
// ASSERT(pConn);
try{
_bstr_t bstrSql(psSql);
_variant_t vNULL;
vNULL.vt =VT_ERROR;
vNULL.scode =DISP_E_PARAMNOTFOUND;
pRs->Open ((_variant_t)bstrSql,vNULL,adOpenForwardOnly,
adLockOptimistic,adCmdText);
return TRUE;
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDesc(e.Description());
CString psError;
psError.Format("Source:%s\nDesc:%s\nNumber:%u",(LPCTSTR)bstrSource,(LPCTSTR)bstrDesc,e.Error);
AfxMessageBox(psError);
return FALSE;
}
}
void CRecordPtr::SetActiveConnection(_ConnectionPtr pCon)
{
pRs->PutRefActiveConnection(pCon);
}
CRecordPtr::AddNew()
{
long lSize=saFieldname.GetSize();
COleSafeArray vaField,vaValue;
vaField.CreateOneDim(VT_VARIANT,lSize);
vaValue.CreateOneDim(VT_VARIANT,lSize);
try
{
long lIndex[1];
for(long l=0;l<lSize;l++)
{
lIndex[0]=l;
vaField.PutElement(lIndex,&((_variant_t)saFieldname.GetAt(l)));
vaValue.PutElement(lIndex,&((_variant_t)saFieldvalue.GetAt(l)));
}
pRs->AddNew(vaField,vaValue);
RemoveArray();
return TRUE;
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDesc(e.Description());
CString psError;
psError.Format("Source:%s\nDesc:%s\nNumber:%u",(LPCTSTR)bstrSource,(LPCTSTR)bstrDesc,e.Error);
AfxMessageBox(psError);
return FALSE;
}
}
CRecordPtr::RemoveArray()
{
saFieldname.RemoveAll();
saFieldvalue.RemoveAll();
}
int CRecordPtr::AddFileldValue(CString psField, CString psValue)
{
saFieldname.Add(psField);
saFieldvalue.Add(psValue);
return saFieldname.GetSize();
}
BOOL CRecordPtr::UpdateValue(LPCTSTR psFieldname, LPCTSTR psFieldvalue)
{
_bstr_t bstrField(psFieldname),bstrValue(psFieldvalue);
return (pRs->Update ((_variant_t)bstrField,(_variant_t)bstrValue))?TRUE:FALSE;
}
void CRecordPtr::Close()
{
pRs->Close();
}
BOOL CAccess::Execute(LPCTSTR psSql)
{
try{
_bstr_t bstrSql(psSql);
_variant_t vAffect(0L);
pConn->Execute(bstrSql,&vAffect,adOptionUnspecified);
return TRUE;
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDesc(e.Description());
CString psError;
psError.Format("Source:%s\nDesc:%s\nNumber:%u",(LPCTSTR)bstrSource,(LPCTSTR)bstrDesc,e.Error);
AfxMessageBox(psError);
return NULL;
}
}
// Access.cpp: implementation of the CAccess class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Access.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CAccess::CAccess()
{
AfxOleInit();
pConn.CreateInstance(__uuidof(Connection));
blConnected=FALSE;
}
CAccess::~CAccess()
{
if(blConnected)
{
pConn->Close();
}
}
BOOL CAccess::ConnectDb(LPCTSTR psDbname,LPCTSTR psUid,LPCTSTR psPwd)
{
CString psConnect;
psConnect.Format("Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=%s",psDbname);
HRESULT hr;
try{
hr=pConn->Open(_bstr_t(psConnect),_bstr_t(L""),_bstr_t(L""),adModeUnknown);
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDesc(e.Description());
CString psError;
psError.Format("Source:%s\nDesc:%s\nNumber:%u",(LPCTSTR)bstrSource,(LPCTSTR)bstrDesc,e.Error);
AfxMessageBox(psError);
}
if(SUCCEEDED(hr))
{
blConnected=TRUE;
return TRUE;
}
else
return FALSE;
}
//CRecordset functions
long CRecordPtr::GetNumericValue(LPCTSTR psFieldname)
{
try{
_variant_t vValue=pRs->GetCollect(_bstr_t(psFieldname));
vValue.ChangeType(VT_I4);
return vValue.lVal;
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDesc(e.Description());
CString psError;
psError.Format("Source:%s\nDesc:%s\nNumber:%u",(LPCTSTR)bstrSource,(LPCTSTR)bstrDesc,e.Error);
AfxMessageBox(psError);
return NULL;
}
return 0;
}
CString CRecordPtr::GetStringValue(LPCTSTR psFieldname)
{
try{
_variant_t vValue;
vValue=pRs->GetCollect(_bstr_t(psFieldname));
vValue.ChangeType(VT_BSTR);
return vValue.bstrVal ;
}
catch(_com_error &e)
{
if(e.Error()==4200736) return "";
_bstr_t bstrSource(e.Source());
_bstr_t bstrDesc(e.Description());
CString psError;
psError.Format("Source:%s\nDesc:%s\nNumber:%u",(LPCTSTR)bstrSource,(LPCTSTR)bstrDesc,e.Error());
AfxMessageBox(psError);
return "";
}
}
//DEL void CRecordPtr::operator =(_RecordsetPtr ptrRs)
//DEL {
//DEL pRs=ptrRs;
//DEL }
BOOL CRecordPtr::MoveNext()
{
return (SUCCEEDED(pRs->MoveNext()))?TRUE:FALSE;
}
BOOL CRecordPtr::MovePrev()
{
return (SUCCEEDED(pRs->MovePrevious()))?TRUE:FALSE;
}
BOOL CRecordPtr::IsEof()
{
return (SUCCEEDED(pRs->adoEOF))?FALSE:TRUE;
}
CRecordPtr::~CRecordPtr()
{
if((char *)pRs->DataMember) pRs->Close();
}
CRecordPtr::CRecordPtr(_ConnectionPtr pCon)
{
pRs.CreateInstance (__uuidof(Recordset));
pRs->PutRefActiveConnection(pCon);
RemoveArray();
}
BOOL CRecordPtr::Open(LPCTSTR psSql)
{
// ASSERT(pConn);
try{
_bstr_t bstrSql(psSql);
_variant_t vNULL;
vNULL.vt =VT_ERROR;
vNULL.scode =DISP_E_PARAMNOTFOUND;
pRs->Open ((_variant_t)bstrSql,vNULL,adOpenForwardOnly,
adLockOptimistic,adCmdText);
return TRUE;
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDesc(e.Description());
CString psError;
psError.Format("Source:%s\nDesc:%s\nNumber:%u",(LPCTSTR)bstrSource,(LPCTSTR)bstrDesc,e.Error);
AfxMessageBox(psError);
return FALSE;
}
}
void CRecordPtr::SetActiveConnection(_ConnectionPtr pCon)
{
pRs->PutRefActiveConnection(pCon);
}
CRecordPtr::AddNew()
{
long lSize=saFieldname.GetSize();
COleSafeArray vaField,vaValue;
vaField.CreateOneDim(VT_VARIANT,lSize);
vaValue.CreateOneDim(VT_VARIANT,lSize);
try
{
long lIndex[1];
for(long l=0;l<lSize;l++)
{
lIndex[0]=l;
vaField.PutElement(lIndex,&((_variant_t)saFieldname.GetAt(l)));
vaValue.PutElement(lIndex,&((_variant_t)saFieldvalue.GetAt(l)));
}
pRs->AddNew(vaField,vaValue);
RemoveArray();
return TRUE;
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDesc(e.Description());
CString psError;
psError.Format("Source:%s\nDesc:%s\nNumber:%u",(LPCTSTR)bstrSource,(LPCTSTR)bstrDesc,e.Error);
AfxMessageBox(psError);
return FALSE;
}
}
CRecordPtr::RemoveArray()
{
saFieldname.RemoveAll();
saFieldvalue.RemoveAll();
}
int CRecordPtr::AddFileldValue(CString psField, CString psValue)
{
saFieldname.Add(psField);
saFieldvalue.Add(psValue);
return saFieldname.GetSize();
}
BOOL CRecordPtr::UpdateValue(LPCTSTR psFieldname, LPCTSTR psFieldvalue)
{
_bstr_t bstrField(psFieldname),bstrValue(psFieldvalue);
return (pRs->Update ((_variant_t)bstrField,(_variant_t)bstrValue))?TRUE:FALSE;
}
void CRecordPtr::Close()
{
pRs->Close();
}
BOOL CAccess::Execute(LPCTSTR psSql)
{
try{
_bstr_t bstrSql(psSql);
_variant_t vAffect(0L);
pConn->Execute(bstrSql,&vAffect,adOptionUnspecified);
return TRUE;
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDesc(e.Description());
CString psError;
psError.Format("Source:%s\nDesc:%s\nNumber:%u",(LPCTSTR)bstrSource,(LPCTSTR)bstrDesc,e.Error);
AfxMessageBox(psError);
return NULL;
}
}