- <pre class="cpp" name="code">//1、连接数据库类
- BOOL CSqlDlg::Ado(CString strConn)
- {
- ::CoInitialize(NULL); // 初始化OLE/COM库环境
- try
- {
- m_pConn.CreateInstance("ADODB.Connection"); //创建Connection对象
- m_pConn->ConnectionTimeout=5; //设置超时时间为5秒
- m_pConn->Open((_bstr_t)strConn,"", "", adModeUnknown);//连接数据库
- }
- catch(_com_error e)
- {
- CATCH_ERROR;
- return false;
- }
- return true;
- }
- //2、执行strSQL的SQL语句,返回集录集
- _RecordsetPtr& CSqlDlg::GetRS(CString strSQL)
- {
- try
- {
- m_pRS.CreateInstance(__uuidof(Recordset));
- m_pRS->Open((_bstr_t)strSQL,m_pConn.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);//执行SQL语句
- }
- catch(_com_error e)
- {
- CATCH_ERROR
- }
- return m_pRS;
- }
- //3、获取所有表名-SQL SERVER
- _RecordsetPtr pRS = GetRS("select name from sysobjects where xtype='U'");
- CString user;
- int x=0;
- strArry.RemoveAll();
- try
- {
- while (pRS->adoEOF==0)
- {
- strArry.Add((LPCSTR)(_bstr_t)pRS->GetCollect(_variant_t((long)x)));
- pRS->MoveNext();
- }
- }
- catch(_com_error e)
- {
- CATCH_ERROR
- return;
- }
- //4、将表名写入列表中
- for(int i=0; i<strArry.GetSize(); i++)
- {
- m_list2.AddString(strArry.GetAt(i));
- }
- //5、连接设置为不可用,断开设置为可用
- GetDlgItem(IDC_LIST)->EnableWindow(0);
- GetDlgItem(IDC_OPEN)->EnableWindow(1);
- //列表控件双击事件读出表中记录
- //6、
- m_list1.DeleteAllItems();
- UpdateData(1);
- int q=0;
- //获取记录条数
- q=GetNum(m_str_list);
- //获取字段个数======================================
- CString strSql1;
- strSql1="select * from";
- strSql1+=" ";
- strSql1+=m_str_list;
- _RecordsetPtr m_pRS2 = GetRS(strSql1);
- dataSize=GetFieldsCount(m_pRS2);
- //==================================================
- if (dataSize==0)
- {
- return;
- }
- //获取字段名strName==================================
- CString *strName=new CString[dataSize];
- for (int bb=0;bb<dataSize;bb++)
- {
- GetFieldsName(m_pRS,bb,*(strName+bb));
- }
- //====================================================
- //清空表头
- while(m_list1.DeleteColumn(0));
- //将字段显示出来
- for (int cc=0;cc<dataSize;cc++)
- {
- m_list1.InsertColumn(cc, *(strName+cc), LVCFMT_LEFT, 150);
- }
- if (q==0)
- {
- return;
- }
- CString strSql;
- strSql="select * from";
- strSql+=" ";
- strSql+=m_str_list;
- _RecordsetPtr pRS = GetRS(strSql);
- CStringArray *strdataArray=new CStringArray[dataSize];
- //获取字段内容,并存入strdataArray+aa中==============
- try{
- for (int aa=0;aa<dataSize;aa++)
- {
- while (pRS->adoEOF ==0)
- {
- CString str1;
- _variant_t varTemp;
- // str1.Format("%s",(LPCSTR)(_bstr_t)pRS->GetCollect(_variant_t((long)aa)));
- //判断数据库中的NULL值
- varTemp=pRS->GetCollect(_variant_t((long)aa));
- if(varTemp.vt ==VT_NULL)
- str1="<NULL>";
- else
- str1.Format("%s",(LPCSTR)(_bstr_t)pRS->GetCollect(_variant_t((long)aa)));
- (strdataArray+aa)->Add(str1);
- pRS->MoveNext();
- }
- pRS->MoveFirst();
- }
- }
- catch(_com_error e)
- {
- CATCH_ERROR
- return;
- }
- //=================================================== CString str="";
- CString str;
- for (int tt=0;tt<q;tt++)
- {
- m_list1.InsertItem(q,"1",0);//插入行
- for(int i=0; i<dataSize; i++)
- {
- str =(strdataArray+i)->GetAt(tt);
- TRACE(" 行:%d, 列:%d,数据:%s\n",tt,i,str);
- m_list1.SetItemText(tt,i,str);//插入内容
- // MessageBox(str);
- }
- }
- delete []strName;
- delete []strdataArray; //释放申请的空间
- }
- //得到记录条数函数
- int CSqlDlg::GetNum(CString strc)
- {
- int nSize=0;
- CString strSql;
- strSql="select count(*) from";
- strSql+=" ";
- strSql+=strc;
- _RecordsetPtr pRS = GetRS(strSql);
- CString s=(LPCSTR)(_bstr_t)pRS->GetCollect(_variant_t((long)0));
- char *ch=new char[s.GetLength()];
- ch=(LPSTR)(LPCTSTR)s;
- nSize=atoi(ch);
- return nSize;
- delete []ch;
- }
- //获取字段名函数
- BOOL CSqlDlg::GetFieldsName(_RecordsetPtr RcdPtr, int nField, CString & strFieldName)
- {
- if(NULL == RcdPtr || nField >= RcdPtr->GetFields()->Count)
- return FALSE;
- _variant_t vt((long)nField);
- strFieldName.Format(_T("%s"), (char*)(RcdPtr->GetFields()->Item[vt]->Name));
- return true;
- }
- //获取字段个数函数
- int CSqlDlg::GetFieldsCount(_RecordsetPtr RcdPtr)
- {
- int nCount=0;
- if(NULL != RcdPtr)
- {
- nCount = RcdPtr->GetFields()->Count;
- }
- return nCount;
- }
- //断开连接
- void CSqlDlg::OnOpen()
- {
- // TODO: Add your control notification handler code here
- GetDlgItem(IDC_LIST)->EnableWindow(1);
- GetDlgItem(IDC_OPEN)->EnableWindow(0);
- //清空数据
- m_list1.DeleteAllItems();
- //清空list列表
- while(m_list2.GetCount())
- m_list2.DeleteString(0);
- //释放ADO环境
- m_pConn->Close();
- m_pConn = NULL;
- ::CoUninitialize();
- }
- </pre><br>
- <pre></pre>
- <img alt="" src="http://hi.csdn.net/attachment/201110/14/0_1318556215RgFv.gif">
- <p>源代码下载地址:下载地址:<a href="http://download.csdn.net/user/huliang66">http://download.csdn.net/user/huliang66</a></p>
- <p> </p>
- <p> </p>
- <pre></pre>