ADO数据库操作之ACCESS和EXCEL和获取结果集表
数据库连接和操作
int CTYPE_TRANSFORMDlg::connect_db()
{
HRESULT hr;
hr = m_rea.CreateInstance(_uuidof(Recordset));//
if(FAILED(hr))
{
AfxMessageBox(L"m_rea CreateInstance failed....");
return -1;
}
hr = m_ree.CreateInstance(_uuidof(Recordset));//
if(FAILED(hr))
{
AfxMessageBox(L"m_ree CreateInstance failed....");
return -1;
}
_bstr_t strConnecta = _T("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\1000263417\\Desktop\\1_database.accdb;Persist Security Info=False;");
_bstr_t strConnecte = _T("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\1000263417\\Desktop\\foup.xlsx;Extended Properties=\"Excel 12.0;HDR=YES;\"");
try
{
hr = m_coa.CreateInstance(_uuidof(Connection));//实例化m_co对象、COM技术ADODB.Connection
if(SUCCEEDED(hr))
{
hr = m_coe.CreateInstance(_uuidof(Connection));//实例化m_co对象、COM技术ADODB.Connection
if(SUCCEEDED(hr))//实例化是否成功
{
m_coa->ConnectionTimeout = 20;//设置连接超时时间
m_coe->ConnectionTimeout = 20;//设置连接超时时间
hr = m_coa->Open(_bstr_t( LPCTSTR(strConnecta)),"","",adConnectUnspecified);//同步连接数据库
if(SUCCEEDED(hr))
{
hr = m_coe->Open(_bstr_t( LPCTSTR(strConnecte)),"","",adConnectUnspecified);//同步连接数据库
if(SUCCEEDED(hr))
{
AfxMessageBox(L"m_coe and m_coa all connect success....");
return -1 ;
}
else
{
AfxMessageBox(L"m_coe connect failed....");
}
}
else
{
AfxMessageBox(L"m_coa connect failed....");
}
}
else
{
AfxMessageBox(L"m_coe createInstance failed...");
return -1;
}
}
else
{
AfxMessageBox(L"m_coa createInstance failed...");
}
}
catch (_com_error &e)//错误捕获
{
_bstr_t bstrSouce(e.Source());
_bstr_t bstrDescription(e.Description());
AfxMessageBox(bstrDescription+bstrSouce);
return -1;
}
return 0;
}
int CTYPE_TRANSFORMDlg::excel_to_access()
{
/**数据转换/
CString sql;
CString columname;
BSTR filedname;
Fields * fields = NULL;
HRESULT hr;
long filedcount;
if(sqlOpen(CString (_T(“EXCEL”)),CString(_T(“select from [sheet2
]
"
)
)
)
<
0
)
/
/
/
∗
i
n
s
e
r
t
i
n
t
o
[
s
h
e
e
t
2
]")))<0)///*insert into [sheet2
]")))<0)///∗insertinto[sheet2] values(‘fff’,33)/
{
AfxMessageBox(L"Excute failed”);
return -1;
}
hr = m_ree->get_Fields (&fields); //得到记录集的字段集和
///
if(FAILED(hr))
{
AfxMessageBox(L"Excute failed");
return -1;
}
filedcount = (m_ree->Fields)->Count;//获取列的个数
if(sqlExcute(CString (_T(“ACCESS”)),CString(_T(“create table [food]”)))<0)
{
AfxMessageBox(L"Excute failed");
}
for(int i = 0;i < filedcount;++i)
{
//fields->Item[(long)i]->get_Name(&filedname);可用于获取表头数据
m_ree->GetFields()->GetItem((long)i)->get_Name(&filedname);//可用于获取表头数据
columname = COLE2T(filedname);//
sql.Format(_T(“alter table food add [%s] varchar(10)”),columname);
if(sqlExcute(CString (_T(“ACCESS”)),sql)<0)
{
AfxMessageBox(L"Excute failed");
}
}
/*CString str;
str.Format(L"%d",a);
a = m_ree->GetRecordCount();*/
m_ree->Close();
m_ree = NULL;
/****************************************************************************************************/
return 0;
}
int CTYPE_TRANSFORMDlg::sqlOpen(CString &sqlname,CString &cstring)
{
HRESULT hr;
if(m_ree == NULL)
{
hr = m_ree.CreateInstance(_uuidof(Recordset));//
if(FAILED(hr))
{
AfxMessageBox(L"m_re failed…“);
return -1;
}
if(m_rea == NULL)
{
hr = m_rea.CreateInstance(_uuidof(Recordset));//
if(FAILED(hr))
{
AfxMessageBox(L"m_re failed…”);
return -1;
}
}
}
try
{
CString sql = cstring;
//sql = _T("create table sheet2(name TEXT,age TEXT)");
if(sqlname == L"EXCEL")
{
hr = m_ree->Open(_variant_t (sql),m_coe.GetInterfacePtr(),/*adOpenDynamic*/adOpenStatic ,adLockOptimistic,adCmdText);
if(FAILED(hr))
{
AfxMessageBox(L"open failed .....");
return -1;
}
}
else if(sqlname == L"ACCESS")
{
hr = m_rea->Open(_variant_t (sql),m_coa.GetInterfacePtr(),/*adOpenDynamic*/adOpenStatic ,adLockOptimistic,adCmdText);
if(FAILED(hr))
{
AfxMessageBox(L"open failed .....");
return -1;
}
}
else
{
AfxMessageBox(L"sqlname errror");
return -1;
}
}
catch(_com_error &e)
{
_bstr_t bstrSouce(e.Source());
_bstr_t bstrDescription(e.Description());
AfxMessageBox(bstrDescription+bstrSouce);
AfxMessageBox(e.ErrorMessage());
return -1;
}
//m_re = m_co->Execute(_bstr_t (csql),NULL,adCmdText);
// TODO: Add your control notification handler code here
return 0;
}
int CTYPE_TRANSFORMDlg::sqlExcute(CString &sqlname,CString &cstring)
{
HRESULT hr;
CString sql = cstring;
try
{
if(sqlname == L"EXCEL")
{
hr = m_coe->Execute(_bstr_t(sql) ,NULL,adCmdText);
if(FAILED(hr))
{
AfxMessageBox(L"create failed 2…“);
return -1 ;
}
}
else if(sqlname == L"ACCESS”)
{
hr = m_coa->Execute(_bstr_t(sql) ,NULL,adCmdText);
if(FAILED(hr))
{
AfxMessageBox(L"create failed 2......");
return -1 ;
}
}
else
{
AfxMessageBox(L"sqlname errror");
return -1;
}
}
catch(_com_error &e)
{
_bstr_t bstrSouce(e.Source());
_bstr_t bstrDescription(e.Description());
AfxMessageBox(bstrDescription+bstrSouce);
AfxMessageBox(e.ErrorMessage());
return -1;
}
return 0;
}