利用ADO从数据库的表中提取数据

本文的代码没有按照教材上的来





在initDlg()中

将listctrl风格改为整行选取

m_list.SetExtendedStyle(m_list.GetExtendedStyle()|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES );

按键提取数据

连接对象 记录集对象

void CMy0714BDlg::OnBnClickedButton1()
{
	// TODO: Add your control notification handler code here

	CoInitialize(NULL);				//初始化COM环境  
	_ConnectionPtr pMyConnect(__uuidof(Connection));//定义连接对象并实例化对象   
<span style="white-space:pre">							//_ConnectionPtr智能指针</span>
    _RecordsetPtr pRst(__uuidof(Recordset));		//定义记录集对象并实例化对象   

	try
	{
		//步骤2:创建数据源连接  
		/*打开数据库“SQLServer”,这里需要根据自己PC的数据库的情况 */               
		pMyConnect->Open("Provider=SQLOLEDB; Server=.;Database=B; uid=sa; pwd=123;","","",adModeUnknown); 
	}
	catch(_com_error &e)   
	{
		//cout<<"Initiate failed!"<<endl;      
		CString str;
		str="初始化连接对象失败!\r\n";
		str+=(LPCSTR)(e.Description());		//_bstr_t 转CString
		str+="\r\n";
		str+=(LPCSTR)(e.HelpFile());
		::AfxMessageBox(str);
		//cout<<e.Description()<<endl;                 
		//cout<<e.HelpFile()<<endl;  
		//_getch();
		//return 0;
	}
	//cout<<"Connect succeed!"<<endl;   
	//::AfxMessageBox("Connect succeed!");
	this->m_static1="Connect succeed!";
	this->UpdateData(FALSE);


	//步骤3:对数据源中的数据库/表进行操作  
    try             
    {  
        pRst = pMyConnect->Execute("select * from Table_BB",NULL,adCmdText);
		//执行SQL: select * from Table_BB          
        if(!pRst->BOF)   
        {  
            pRst->MoveFirst();   
        }                 
        else  
        {                      
	<span style="white-space:pre">	</span>::AfxMessageBox("Data is empty!");                
        }                 
        vector<_bstr_t> column_name;        
           
        //存储表的所有列名,显示表的列名               
        for(int i=0; i< pRst->Fields->GetCount();i++)                 
        {                                          
			_bstr_t x=pRst->Fields->GetItem(_variant_t((long)i))->Name;
			column_name.push_back(x);
			this->m_list.InsertColumn(i,x,2,100);	//这里_bstr_t 自动转化为了 LPCSTR
        }      
           
        //对表进行遍历访问,显示表中每一行的内容           
<span style="white-space:pre">	</span>int row=0;
        while(!pRst->adoEOF)                 
        {                      
            vector<_bstr_t>::iterator iter=column_name.begin();           
		if(pRst->GetCollect(*iter).vt !=VT_NULL)                           
                {    
			this->m_list.InsertItem(row,(_bstr_t)pRst->GetCollect(*iter));                        
                }                           
                else  
                {  
			this->m_list.InsertItem(row,"NULL");
                }     
			iter++;
            for(iter;iter!=column_name.end();iter++)                      
            {                           
		int line=iter-column_name.begin();
                if(pRst->GetCollect(*iter).vt !=VT_NULL)                           
                {    
					this->m_list.SetItemText(row,line,(_bstr_t)pRst->GetCollect(*iter));                      
                }                           
                else  
                {  
					this->m_list.SetItemText(row,line,"NULL");
                }                    
            }  
            pRst->MoveNext();             
	<span style="white-space:pre">	</span>row++;             
        }             
    }  
    catch(_com_error &e)             
    {                 
		CString str;
		str="初始化连接对象失败!\r\n";
		str+=(LPCSTR)(e.Description());		//_bstr_t 转CString
		str+="\r\n";
		str+=(LPCSTR)(e.HelpFile());
		::AfxMessageBox(str);       
    }    
<span style="white-space:pre">	</span>//步骤4  关闭数据源
<span style="white-space:pre">	</span>try
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>pRst->Close();<span style="white-space:pre">		</span>//关记录集
<span style="white-space:pre">		</span>pMyConnect->Close();<span style="white-space:pre">	</span>//关闭数据库
<span style="white-space:pre">		</span>pRst.Release();<span style="white-space:pre">		</span>//释放记录集
<span style="white-space:pre">		</span>pMyConnect.Release();<span style="white-space:pre">	</span>//释放连接对象指针
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>catch(_com_error &e)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>CString str;
<span style="white-space:pre">		</span>str+=(LPCSTR)(e.Description());<span style="white-space:pre">		</span>//_bstr_t 转CString
<span style="white-space:pre">		</span>str+="\r\n";
<span style="white-space:pre">		</span>str+=(LPCSTR)(e.HelpFile());
<span style="white-space:pre">		</span>::AfxMessageBox(str);<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>::CoUninitialize();<span style="white-space:pre">	</span>//释放COM环境
}


清空列表

void CMy0714BDlg::OnBnClickedButton2()		//清空列表
{
	// TODO: Add your control notification handler code here
	this->m_list.DeleteAllItems();
	while(this->m_list.GetHeaderCtrl()->GetItemCount())
	{
		this->m_list.DeleteColumn(0);
	}
}

添加列表行

void CMy0714BDlg::OnBnClickedButton3()
{
	// TODO: Add your control notification handler code here
	CoInitialize(NULL);								//初始化COM环境  
	_ConnectionPtr pMyConnect(__uuidof(Connection));//定义连接对象并实例化对象   
    _RecordsetPtr pRst(__uuidof(Recordset));		//定义记录集对象并实例化对象   
	BOOL custom=TRUE;

	try
	{
		//步骤2:创建数据源连接  
		/*打开数据库“SQLServer”,这里需要根据自己PC的数据库的情况 */               
		pMyConnect->Open("Provider=SQLOLEDB; Server=.;Database=B; uid=sa; pwd=123;","","",adModeUnknown); 
		custom=TRUE;
	}
	catch(_com_error &e)   
	{  
		CString str;
		str="初始化连接对象失败!\r\n";
		str+=(LPCSTR)(e.Description());		//_bstr_t 转CString
		str+="\r\n";
		str+=(LPCSTR)(e.HelpFile());
		::AfxMessageBox(str);
		custom=FALSE;
	}
	CString str=(TRUE==custom)?"连接成功":"连接失败";
	str="\r\n"+str;
	this->m_static1+=str;
	this->UpdateData(FALSE);

	 try             
    {  
        pMyConnect->Execute("insert into Table_BB (ID,name,age,sex) values ('018','敌法','17','男')",NULL,adCmdText);               
    }  
    catch(_com_error &e)             
    {                 
		CString str;
		str+=(LPCSTR)(e.Description());		//_bstr_t 转CString
		str+="\r\n";
		str+=(LPCSTR)(e.HelpFile());
		::AfxMessageBox(str);           
    }    

	//步骤4  关闭数据源
	try
	{
		//pRst->Close();		//关记录集
		pMyConnect->Close();	//关闭数据库
		pRst.Release();		//释放记录集
		pMyConnect.Release();	//释放连接对象指针
	}
	catch(_com_error &e)
	{
		CString str;
		str+=(LPCSTR)(e.Description());		//_bstr_t 转CString
		str+="\r\n";
		str+=(LPCSTR)(e.HelpFile());
		::AfxMessageBox(str);	
	}
	::CoUninitialize();	//释放COM环境
}



风行得引领群雄者行风

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页