数据库浏览程序,能打开和浏览SQL SERVER、ACCESS数据库 VC ADO ACCESS SQLSERVER 数据库 //连接数据库函数 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; } //打开ACCESS数据库 // TODO: Add your control notification handler code here UpdateData(TRUE); if(m_type==0) { MessageBox("您选择的是SQL SERVER数据库,请点击连接!"); return; } CFileDialog flDlg(TRUE, "*.mdb", NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, "ACCESS数据库(*.md)|*.mdb|所有格式(*.*)|*.*|", NULL); DWORD dwlen; char *pbuf; if(IDOK== flDlg.DoModal()) { fnamepath=flDlg.GetPathName(); CFile myfile; myfile.Open(fnamepath, CFile::modeRead | CFile::typeBinary|CFile::shareDenyNone); dwlen=myfile.GetLength(); pbuf=new char[dwlen]; pbuf[dwlen]=0; myfile.Read(pbuf,dwlen); myfile.Close(); SetDlgItemText(IDC_ACCESS_STR,fnamepath); GetDlgItem(IDC_LIST)->EnableWindow(1); // MessageBox(fnamepath); } else { MessageBox("数据库没有打开!"); return; } //执行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); // 取得表中的记录 } catch(_com_error e) { CATCH_ERROR } return m_pRS; } //连接,显示表 UpdateData(1); // TODO: Add your control notification handler code here //数据库连接 CString m_strConn; _RecordsetPtr pRS; if (m_type==0) { m_strConn.Format("driver={SQL Server}; Server=%s; DATABASE=%s; UID=%s; PWD=%s", m_server,m_db,m_user, m_pwd); } else if (m_type==1) { m_strConn.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s",fnamepath,"","",adModeUnknown); } if(!Ado(m_strConn)) return; //获取所有表名-SQL SERVER if (m_type==0) { pRS=GetRS("select name from sysobjects where xtype='U'"); } else if (m_type==1) { pRS = GetRS("SELECT MSysObjects.Name FROM MSysObjects WHERE type=1"); } 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; } for(int i=0; iEnableWindow(0); GetDlgItem(IDC_OPEN)->EnableWindow(1); //得到字段个数 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; } //打开表,显示字段和值 // TODO: Add your control notification handler code here 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;bbadoEOF ==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=""; 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;ttGetAt(tt); TRACE(" 行:%d, 列:%d,数据:%s\n",tt,i,str); m_list1.SetItemText(tt,i,str);//插入内容 // MessageBox(str); } } delete []strName; delete []strdataArray; //释放申请的空间 } //断开连接 void CSqlDlg::OnBreak() { // 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(); }