Ado 连接Excel 数据库链接问题

tanguangzhang 2011-08-20 04:53:14
CString sql=_T("");
sql="select count(*) as geshu from [道路$] where 城市 like '%"+str_city+"%'";
Rs1=(((CDataManaApp*)AfxGetApp())->pCon_ex)->Execute((_bstr_t)sql,NULL,adCmdText);
_variant_t vCount=Rs1->GetCollect("geshu");
这段代码中的CDataManaApp*是什么意思,怎么来的?????如何定义??????


完整代码如下:

VC 利用ADO操作Excel(原创)
把Excel当做数据库来操作,步骤如下:

1、在stdafx.h中加入#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename ("EOF", "adoEOF")

2、在工程的App类的构造函数中打开要操作的Excel表格

_ConnectionPtr pCon_ex;
CString ConnectionString;

CToolsApp::CToolsApp()
{
CString m_strAppPath=_T("");
CString excel_path=_T("");
CString con_str=_T("");

//程序所在目录路径
TCHAR exeFullPath[MAX_PATH];
GetModuleFileName(NULL,exeFullPath,MAX_PATH);

CString str;
str.Format("%s",exeFullPath);

m_strAppPath = str.Left( str.ReverseFind( '\\' ) );
excel_path = m_strAppPath+"\\Database"+"\\新全国图数据统计模版--.xls";

CoInitialize(NULL);

//打开excel
/*"HDR=Yes;" 表示工作表的第一行是表头,没有数据。 "HDR=No;"与之相反。
"IMEX=1;"告诉驱动程序始终将"intermixed"数据类型(numbers, dates, strings等等)作为文本型读取。
注意:该选项可能引起Excel工作表写权限的修改。如果想写入数据,创建新表等必须使其为0*/
ConnectionString = _T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=");
ConnectionString += excel_path; //excel file name
ConnectionString += _T(";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=0\"");

BSTR resultsString = ConnectionString.AllocSysString();
pCon_ex.CreateInstance(__uuidof(Connection));

resultsString = ConnectionString.AllocSysString();
pCon_ex->Open(resultsString,"","",adModeUnknown);


}

3、从Excel中读数据

读数据比较简单,可以使用SQL查询语句来找到自己感兴趣的记录。

CString strSQL=_T("");
_RecordsetPtr pRst(__uuidof(Recordset)); //数据集
_RecordsetPtr Rs1(__uuidof(Recordset)); //数目集

strSQL="select * from [道路$] where 城市 like '%"+str_city+"%'"; //[道路$]为sheet的名称
pRst=(((CDataManaApp*)AfxGetApp())->pCon_ex)->Execute((_bstr_t)strSQL,NULL,adCmdText); //指定的城市

CString sql=_T("");
sql="select count(*) as geshu from [道路$] where 城市 like '%"+str_city+"%'";
Rs1=(((CDataManaApp*)AfxGetApp())->pCon_ex)->Execute((_bstr_t)sql,NULL,adCmdText);
_variant_t vCount=Rs1->GetCollect("geshu");
int num1=vCount.lVal; //符合条件的记录个数

pRst->MoveFirst(); //只读取第一行

_variant_t t = _variant_t(long(6));
result = (LPCSTR)_bstr_t(pRecordset->GetCollect(t));//以列序号的方式来读取字段内容 0based

result = (LPCSTR)_bstr_t(pRecordset->GetCollect("人口"));//以字段名的方式来读字段内容

4 增加新行以及填写某个cell

_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance(__uuidof(Recordset));

try
{
m_pRecordset->Open("SELECT * FROM [道路$]",// 查询道路表中所有字段
((CToolsApp*)AfxGetApp())->pCon_ex.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}

try
{
CString strDate= "123.698";
//添加新记录
m_pRecordset->MoveFirst();
m_pRecordset->Move(15); //此行没用 无论当前的指针在什么位置,都将插入到最后一行 数据库是不能在中间插入新行的
m_pRecordset->AddNew(); //如是要更新cell 不要这句
_variant_t t = _variant_t(long(6));
m_pRecordset->PutCollect(&t,_variant_t(strDate)); //更新第七个字段 也可以用字段名的指定
m_pRecordset->Update();

m_pRecordset->Close();
m_pRecordset = NULL;
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}

...全文
157 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
晋晔 2012-03-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hdt 的回复:]
你的代码不全
cdatamanapp 应该是cwinapp的子类,
应该是代码自己提供的
[/Quote]
请问一下,这个是怎么改正的,我现在正在用这个程序,但是同样的错误,没能发现,希望大侠们能帮我渡过难关,小弟在这多谢了
晋晔 2012-03-22
  • 打赏
  • 举报
回复
请问一下,这个是怎么改正的,我现在正在用这个程序,但是同样的错误,没能发现,希望大侠们能帮我渡过难关,小弟在这多谢了
真相重于对错 2011-08-21
  • 打赏
  • 举报
回复
你的代码不全
cdatamanapp 应该是cwinapp的子类,
应该是代码自己提供的


tanguangzhang 2011-08-20
  • 打赏
  • 举报
回复
以上代码执行出现如下问题,请牛人帮忙解决:

1>d:\visual c++.net 实用编程百例\第二例\tools\tools\toolsdlg.cpp(163) : error C2065: “CDataManaApp”: 未声明的标识符
1>d:\visual c++.net 实用编程百例\第二例\tools\tools\toolsdlg.cpp(163) : error C2059: 语法错误 : “)”
1>d:\visual c++.net 实用编程百例\第二例\tools\tools\toolsdlg.cpp(167) : error C2065: “CDataManaApp”: 未声明的标识符
1>d:\visual c++.net 实用编程百例\第二例\tools\tools\toolsdlg.cpp(167) : error C2059: 语法错误 : “)”
1>d:\visual c++.net 实用编程百例\第二例\tools\tools\toolsdlg.cpp(189) : error C2039: “pCon_ex”: 不是“CToolsApp”的成员
1> d:\visual c++.net 实用编程百例\第二例\tools\tools\tools.h(17) : 参见“CToolsApp”的声明
1>d:\visual c++.net 实用编程百例\第二例\tools\tools\toolsdlg.cpp(189) : error C2228: “.GetInterfacePtr”的左边必须有类/结构/联合

7,540

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 VC.NET
社区管理员
  • VC.NET社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧