BOOL CBCGPODBCGridCtrl::GetFieldList (LPCTSTR lpszTable, CStringList& lstField) { ASSERT (lpszTable != NULL); lstField.RemoveAll (); if (m_pDataBase == NULL) { ASSERT (FALSE); return FALSE; } try { ASSERT_VALID (m_pDataBase); if (!m_pDataBase->IsOpen ()) { ASSERT (FALSE); return FALSE; } CRecordset* pRecordSet = new CRecordset (m_pDataBase); CString strSQL = _T("SELECT * FROM "); strSQL += lpszTable; if (!pRecordSet->Open (CRecordset::forwardOnly, strSQL)) { delete pRecordSet; return FALSE; } const short nColumns = pRecordSet->GetODBCFieldCount (); for (short nColumn = 0; nColumn < nColumns; nColumn++) { CODBCFieldInfo fieldinfo; pRecordSet->GetODBCFieldInfo (nColumn, fieldinfo); lstField.AddTail (fieldinfo.m_strName); } pRecordSet->Close (); delete pRecordSet; } catch (CDBException* pEx) { OnODBCException (pEx); pEx->Delete (); return FALSE; } return TRUE; }
BOOL CFieldContentsDlg::OnInitDialog() { CDialog::OnInitDialog(); try { // prüfe Parameter ASSERT (NULL != m_pDatabase && m_pDatabase -> IsOpen ()); ASSERT (!m_strField.IsEmpty ()); ASSERT (!m_strTable.IsEmpty ()); // Listbox füllen CWaitCursor wc; // zuerst SelectString zusammenbauen CString strSelect; AfxFormatString2 (strSelect, IDS_SELECT_FIELD, m_strField, m_strTable); CRecordset rs (m_pDatabase); VERIFY (rs.Open (CRecordset::forwardOnly, strSelect, CRecordset::readOnly)); CString strVal; int iMax = 0; while (!rs.IsEOF ()) { rs.GetFieldValue (m_strField, strVal); if (!strVal.IsEmpty ()) { if (m_lbFields.AddString (strVal) < 0) AfxThrowMemoryException (); iMax = max (iMax, strVal.GetLength ()); } rs.MoveNext (); } // HorizontalExtent setzen if (iMax > 0) { WORD wUnits = LOWORD (:: GetDialogBaseUnits ()); m_lbFields.SetHorizontalExtent (wUnits * iMax); } // TypInfo ausgeben CODBCFieldInfo FieldInfo; rs.GetODBCFieldInfo (m_strField, FieldInfo); switch (FieldInfo.m_nSQLType) { case SQL_DATE: // Zeitformate case SQL_TIME: case SQL_TIMESTAMP: { m_uiResID = IDS_DATETIME_FORMAT; VERIFY (m_strFieldType.LoadString (IDS_DATE_TIME)); } break; case SQL_DECIMAL: case SQL_NUMERIC: case SQL_BIGINT: case SQL_CHAR: case SQL_VARCHAR: { m_uiResID = IDS_STRING_FORMAT; VERIFY (m_strFieldType.LoadString (IDS_TEXT)); } break; default: // alle Zahlenformate VERIFY (m_strFieldType.LoadString (IDS_ZAHL)); break; } // Caption setzen CString strFormat; GetWindowText (strFormat); strVal.Format (strFormat, m_strTable); SetWindowText (strVal); // Feldname setzen m_strFieldName = FieldInfo.m_strName; // Controls setzen m_lbFields.EnableWindow (m_lbFields.GetCount () > 0); m_btOk.EnableWindow (FALSE); rs.Close (); UpdateData (FALSE); } catch (CException *e) { e -> ReportError (); e -> Delete (); } return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE }
void CEdRptDoc::GetStaticData(CRecordset &rc) { if (!rc.IsOpen()) return; CString csTmp; GV_ITEM Item; Item.mask = GVIF_TEXT|GVIF_FORMAT; Item.nFormat = DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS|DT_NOPREFIX; Item.row = m_Grid.GetRowCount() - 1; Item.col = 0; Item.szText = "总计"; m_Grid.SetItem(&Item); CString csFormat; CODBCFieldInfo fi; for (Item.col = 1; Item.col < m_ColFmt.GetSize(); Item.col++) { if (m_ColFmt[Item.col].format.IsEmpty()) { rc.GetODBCFieldInfo(short(Item.col), fi); if (fi.m_nSQLType == SQL_NUMERIC || fi.m_nSQLType == SQL_DECIMAL) { if (fi.m_nScale == 0) { fi.m_nSQLType = SQL_INTEGER; csFormat.Format("%%%dd", fi.m_nPrecision); } else { fi.m_nSQLType = SQL_FLOAT; csFormat.Format("%%%d.%df", fi.m_nPrecision, fi.m_nScale); } } } else { csFormat = m_ColFmt[Item.col].format; if (csFormat.FindOneOf("dioux") > 0) fi.m_nSQLType = SQL_INTEGER; else if (csFormat.FindOneOf("eEfgG") > 0) fi.m_nSQLType = SQL_FLOAT; else fi.m_nSQLType = SQL_DATETIME; } switch(fi.m_nSQLType) { case SQL_INTEGER: case SQL_SMALLINT: case SQL_TINYINT: case SQL_BIGINT: fi.m_nSQLType = SQL_INTEGER; if (csFormat.IsEmpty()) csFormat = _T("%d"); break; case SQL_FLOAT: case SQL_REAL: case SQL_DOUBLE: fi.m_nSQLType = SQL_FLOAT; if (csFormat.IsEmpty()) csFormat = _T("%f"); break; default: break; } double fSum = 0.0; if (fi.m_nSQLType == SQL_INTEGER || fi.m_nSQLType == SQL_FLOAT) { CString csTmp; for (int j = 1; j < m_Grid.GetRowCount() - 1; j++) { csTmp = m_Grid.GetItemText(j, Item.col); fSum += atof(csTmp); } } if (fi.m_nSQLType == SQL_INTEGER) Item.szText.Format(csFormat, (int)fSum); else if (fi.m_nSQLType == SQL_FLOAT) Item.szText.Format(csFormat, fSum); else Item.szText = "--:--"; Item.szText.TrimLeft(); Item.szText.TrimRight(); m_Grid.SetItem(&Item); } }