xmldlg.cpp
来自「Userful I think you need now please down」· C++ 代码 · 共 727 行 · 第 1/2 页
CPP
727 行
pNodeList3=pDoc->getElementsByTagName((_bstr_t)"专业课");
psNode=pNodeList->Getitem(i);
cs_english =(char *)pNodeList1->Getitem(i)->Gettext();
cs_maths =(char *)pNodeList2->Getitem(i)->Gettext();
cs_profession=(char *)pNodeList3->Getitem(i)->Gettext();
psNode->get_attributes(&methodpAttrs);
methodpAttrs->get_length(&nCount);
m_list.InsertItem(0,LPCTSTR (cs_english),0);
m_list.SetItemText ( 0, 1, LPCTSTR (cs_maths ) );
m_list.SetItemText ( 0, 2, LPCTSTR (cs_profession ) );
for(int nmethod=0;nmethod<nCount;nmethod++) //属性值的循环
{
methodpAttrs->get_item(nmethod,&pmethodAttrItem);
CString strgetName =(char*)(_bstr_t)pmethodAttrItem->nodeName;
CString strgetValue=(char*)(_bstr_t)pmethodAttrItem->nodeTypedValue;
m_list . SetItemText ( 0, nmethod+3, LPCTSTR (strgetValue) );
}
}
UpdateData(FALSE);
}
void CXMLDlg::OnBUTTONclear()
{
// TODO: Add your control notification handler code here
UpdateData(true);
m_list.DeleteAllItems();
m_name.Empty();
m_no.Empty();
m_maths.Empty();
m_english.Empty();
m_profession.Empty();
m_number=0;
UpdateData(false);
}
void CXMLDlg::OnBUTTONimport()
{
//首先判断数据库是否可以进行连接
OnBUTTONclear();
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
// TODO: Add your control notification handler code here
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
//不通过DSN对SQL SERVER数据库进行连接
m_pConnection->CommandTimeout=25;
m_pConnection->Open("driver={SQL Server};Server=127.0.0.1;DATABASE=xml;ID=sa;PWD=","","",adModeUnknown);
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
UpdateData(true);
//加载文件
MSXML2::IXMLDOMNodePtr psNode;
MSXML2::IXMLDOMNodePtr pmethodAttrItem;
MSXML2::IXMLDOMNodePtr pAttrItem;
MSXML2::IXMLDOMNamedNodeMapPtr pAttrMap=NULL;
MSXML2::IXMLDOMNamedNodeMapPtr methodpAttrs=NULL;
_variant_t variantValue;
CString strName;
long nCount;
MSXML2::IXMLDOMNodeListPtr pNodeList = NULL; //解析student
MSXML2::IXMLDOMNodeListPtr pNodeList1 = NULL;//解析英语
MSXML2::IXMLDOMNodeListPtr pNodeList2 = NULL;//解析数学
MSXML2::IXMLDOMNodeListPtr pNodeList3 = NULL;//解析专业课
pDoc->load("wopapapa.xml");
pNodeList=pDoc->getElementsByTagName((_bstr_t)"Student");
int num=pNodeList->Getlength();
CString tt;
CString cs_english,cs_maths,cs_profession;
CString cs_name,cs_id;
for(int i=0;i<num;i++)
{
pNodeList1=pDoc->getElementsByTagName((_bstr_t)"英语");
pNodeList2=pDoc->getElementsByTagName((_bstr_t)"数学");
pNodeList3=pDoc->getElementsByTagName((_bstr_t)"专业课");
psNode=pNodeList->Getitem(i);
cs_name.Empty();
cs_id.Empty();
cs_maths.Empty();
cs_english.Empty();
cs_profession.Empty();
cs_english =(char *)pNodeList1->Getitem(i)->Gettext();
cs_maths =(char *)pNodeList2->Getitem(i)->Gettext();
cs_profession=(char *)pNodeList3->Getitem(i)->Gettext();
psNode->get_attributes(&methodpAttrs);
methodpAttrs->get_length(&nCount);
for(int nmethod=0;nmethod<nCount;nmethod++) //属性值的循环
{
methodpAttrs->get_item(nmethod,&pmethodAttrItem);
CString strgetName =(char*)(_bstr_t)pmethodAttrItem->nodeName;
CString strgetValue=(char*)(_bstr_t)pmethodAttrItem->nodeTypedValue;
if(nmethod==0) cs_name=strgetValue;
if(nmethod==1) cs_id =strgetValue;
}
//写入数据库
m_pRecordset.CreateInstance("ADODB.Recordset");
try
{
m_pRecordset->Open("SELECT * FROM Book ", _variant_t((IDispatch *)m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);
_variant_t RecordsAffected;
CString sql;
sql.Format("Insert into Book(name,number,english,maths,profession) VALUES('%s','%s','%s','%s','%s')",cs_name,cs_id,cs_english,cs_maths,cs_profession);
m_pConnection->Execute(_bstr_t(sql),&RecordsAffected,adCmdText);//往表格里面添加记录
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("打开表失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
}
AfxMessageBox("~~已经成功将XML中的数据导入SQL中~~");
UpdateData(false);
}
void CXMLDlg::OnBUTTONout()
{
// TODO: Add your control notification handler code here
UpdateData(true);
OnBUTTONclear();
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
//不通过DSN对SQL SERVER数据库进行连接
m_pConnection->CommandTimeout=25;
m_pConnection->Open("driver={SQL Server};Server=127.0.0.1;DATABASE=xml;ID=sa;PWD=","","",adModeUnknown);
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
m_pRecordset.CreateInstance("ADODB.Recordset");
CString cs_name,cs_id,cs_english,cs_maths,cs_profession;
MSXML2::IXMLDOMDocumentPtr pDoc;
MSXML2::IXMLDOMElementPtr pNode;
//创建DOMDocument对象
hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument40));
if(!SUCCEEDED(hr))
{
MessageBox("无法创建DOMDocument对象,请检查是否安装了MS XML Parser运行库!");
return ;
}
xmlRoot=pDoc->createElement((_bstr_t)"Book");//创建名称为book的element
pDoc->appendChild(xmlRoot);//pDoc为父元素,xmlRoot为要附加到pDoc之下的结点
//从SQL读到XML中
try
{
m_pRecordset->Open("SELECT * FROM Book ",_variant_t((IDispatch *)m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);
while(!m_pRecordset->adoEOF)
{
_variant_t theValue1 = m_pRecordset->GetCollect("name");
_variant_t theValue2 = m_pRecordset->GetCollect("number");
_variant_t theValue3 = m_pRecordset->GetCollect("english");
_variant_t theValue4 = m_pRecordset->GetCollect("maths");
_variant_t theValue5 = m_pRecordset->GetCollect("profession");
cs_name=(char *)_bstr_t(theValue1);
cs_id=(char *)_bstr_t(theValue2);
cs_english=(char *)_bstr_t(theValue3);
cs_maths=(char *)_bstr_t(theValue4);
cs_profession=(char *)_bstr_t(theValue5);
//添加元素
pNode=pDoc->createElement((_bstr_t)"Student"); //创建
pNode->setAttribute("s_name",(const char *)cs_name);
pNode->setAttribute("s_number",(const char *)cs_id);
xmlRoot->appendChild(pNode);
//继续添加子结点
MSXML2::IXMLDOMElementPtr wopapapa;
wopapapa=pDoc->createElement((_bstr_t)"英语");
wopapapa->Puttext((const char *)cs_english);
pNode->appendChild(wopapapa);
wopapapa=pDoc->createElement((_bstr_t)"数学");
wopapapa->Puttext((const char *)cs_maths);
pNode->appendChild(wopapapa);
wopapapa=pDoc->createElement((_bstr_t)"专业课");
wopapapa->Puttext((const char *)cs_profession);
pNode->appendChild(wopapapa);
m_pRecordset->MoveNext();
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("打开表失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
//保存到文件.如果不存在就建立,存在就覆盖
pDoc->save("wopapapa.xml");
AfxMessageBox("~~已经成功将SQL中的数据导出XML中~~");
UpdateData(false);
}
void CXMLDlg::OnBUTTONexplain()
{
// TODO: Add your control notification handler code here
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
void CXMLDlg::OnBUTTONallsql()
{
// TODO: Add your control notification handler code here
// TODO: Add your control notification handler code here
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
OnBUTTONclear();
// TODO: Add your control notification handler code here
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
//不通过DSN对SQL SERVER数据库进行连接
m_pConnection->CommandTimeout=25;
m_pConnection->Open("driver={SQL Server};Server=127.0.0.1;DATABASE=xml;ID=sa;PWD=","","",adModeUnknown);
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
m_pRecordset.CreateInstance("ADODB.Recordset");
try
{
m_pRecordset->Open("SELECT * FROM Book ", _variant_t((IDispatch *)m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);
while(!m_pRecordset->adoEOF)
{
_variant_t theValue1 = m_pRecordset->GetCollect("english");
_variant_t theValue2 = m_pRecordset->GetCollect("maths");
_variant_t theValue3 = m_pRecordset->GetCollect("profession");
_variant_t theValue4 = m_pRecordset->GetCollect("name");
_variant_t theValue5 = m_pRecordset->GetCollect("number");
m_list.InsertItem(0,LPCTSTR ( (char *)_bstr_t(theValue1) ),0);
m_list . SetItemText ( 0, 1, LPCTSTR ((char *)_bstr_t(theValue2) ) );
m_list . SetItemText ( 0, 2, LPCTSTR ((char *)_bstr_t(theValue3) ) );
m_list . SetItemText ( 0, 3, LPCTSTR ((char *)_bstr_t(theValue4) ) );
m_list . SetItemText ( 0, 4, LPCTSTR ((char *)_bstr_t(theValue5) ) );
m_pRecordset->MoveNext();
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("打开表失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
}
void CXMLDlg::OnBUTTONclearsql()
{
// TODO: Add your control notification handler code here
_ConnectionPtr m_pConnection;
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
//不通过DSN对SQL SERVER数据库进行连接
m_pConnection->CommandTimeout=25;
m_pConnection->Open("driver={SQL Server};Server=127.0.0.1;DATABASE=xml;ID=sa;PWD=","","",adModeUnknown);
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance("ADODB.Recordset");
try
{
m_pRecordset->Open("SELECT * FROM Book", _variant_t((IDispatch *)m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);
_variant_t RecordsAffected;
CString sql;
sql.Format("DELETE FROM Book ");
m_pConnection->Execute(_bstr_t(sql),&RecordsAffected,adCmdText);//往表格里删除记录
AfxMessageBox("~~清除成功~~");
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("打开表失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
OnBUTTONclear();
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?