MFC获取SqlServer数据库所有表、字段名、记录数据

代码如下过程其实不是每一步都有,但是主要功能都在!

//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; iEnableWindow(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;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; //释放申请的空间
}

//得到记录条数函数
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();
}

      转载自 : https://blog.csdn.net/xgx198831/article/details/6872303#

你可能感兴趣的:(♬,--,C\C++)