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;
}
int CMyDatabase::getProvinceid(CString provinceName)
{
	CString SqlString2 = _T("SELECT * FROM Province WHERE Province='")+provinceName+_T("' ");
	CRecordset *record;
	record= new CRecordset(this->m_nDatabase);
	record->Open(CRecordset::dynaset,SqlString2);
	CString tempStr=_T("0");
	int PID=0;
	if (!record->IsEOF())
	{
		record->MoveFirst();
		record->GetFieldValue(_T("ProvinceID"),tempStr);
	}
	PID	= _ttoi(tempStr) ;
	record->Close();
	return PID;
}
int CMyDatabase::getTypesID(CString TypesName)
{
	CString SqlString2 = _T("SELECT * FROM types WHERE Name='")+TypesName+_T("' ");
	CRecordset *record;
	record= new CRecordset(this->m_nDatabase);
	record->Open(CRecordset::dynaset,SqlString2);
	CString tempStr=_T("0");
	int PID=0;
	if (!record->IsEOF())
	{
		record->MoveFirst();
		record->GetFieldValue(_T("CID"),tempStr);
	}
	PID	= _ttoi(tempStr) ;
	record->Close();
	return PID;
}
void CDBManageTrueDlg::UpdateIndexTable(void)
{
	//CTrueWineLib TrueWineLib;

	CDatabase m_db;
	CRecordset rs;
	m_db.OpenEx(_T("DSN=白酒鉴定与溯源数据库;"),CDatabase::noOdbcDialog);
	rs.m_pDatabase = &m_db;

	CString WineName;
	CString AlcoholContent;
	CString Flavour;
	CString Brand;
	CString ProductionBatchNo;

	//更新白酒类型索引
	CWineTypeIndex WineTypeIndex;

	if(!WineTypeIndex.Open())
	{
		MessageBox(L"打开光谱索引库失败",L"更新失败",MB_ICONERROR);
		return;
	}
	if(!WineTypeIndex.IsEOF())
	{
		WineTypeIndex.MoveFirst();
		while(!WineTypeIndex.IsEOF())
		{
			WineTypeIndex.Delete();
			WineTypeIndex.MoveNext();
		}
	}
	CString command=_T("SELECT DISTINCT WineName,AlcoholContent,Flavour,Brand,ProductionBatchNo FROM TrueWineLib");
	if(!rs.Open(AFX_DB_USE_DEFAULT_TYPE,command))
	{
		MessageBox(L"打开真酒库失败",L"更新失败",MB_ICONERROR);
		return;
	}

	if(!rs.IsEOF())
	{
		int count=1;
		rs.MoveFirst();
		while(!rs.IsEOF())
		{
			rs.GetFieldValue(L"WineName",WineName);
			rs.GetFieldValue(L"AlcoholContent",AlcoholContent);
			rs.GetFieldValue(L"Flavour",Flavour);
			rs.GetFieldValue(L"Brand",Brand);
			rs.GetFieldValue(L"ProductionBatchNo",ProductionBatchNo);

			WineTypeIndex.AddNew();
			WineTypeIndex.m_WineName=WineName;   //不知道为什么使用了select distinct 得到某几列记录时,自动把这一列分配给最前面的类型相同的列
			WineTypeIndex.m_AlcoholContent=AlcoholContent;
			WineTypeIndex.m_Flavour=Flavour;
			WineTypeIndex.m_Brand=Brand;
			WineTypeIndex.m_ProductionBatchNo=ProductionBatchNo;
			WineTypeIndex.m_TypeIndex=count;
			count++;
			rs.MoveNext();
			WineTypeIndex.Update();
		}
	}
	rs.Close();
	//更新酒精度索引
	CAlcoholTypeIndex AlcoholTypeIndex;

	if(!AlcoholTypeIndex.Open())
	{
		MessageBox(L"打开光谱索引库失败",L"更新失败",MB_ICONERROR);
		return;
	}
	if(!AlcoholTypeIndex.IsEOF())
	{
		AlcoholTypeIndex.MoveFirst();
		while(!AlcoholTypeIndex.IsEOF())
		{
			AlcoholTypeIndex.Delete();
			AlcoholTypeIndex.MoveNext();
		}
	}

	command=_T("SELECT DISTINCT AlcoholContent FROM TrueWineLib");
	if(!rs.Open(AFX_DB_USE_DEFAULT_TYPE,command))
	{
		MessageBox(L"打开真酒库失败",L"更新失败",MB_ICONERROR);
		return;
	}
	CString alcohol;


	if(!rs.IsEOF())
	{
		rs.MoveFirst();
		int i=1;
		while(!rs.IsEOF())
		{		
			rs.GetFieldValue(_T("AlcoholContent"),alcohol); 
			AlcoholTypeIndex.AddNew();
			AlcoholTypeIndex.m_AlcoholContent=alcohol;
			AlcoholTypeIndex.m_AlcoholIndex=i;
			i++;
			AlcoholTypeIndex.Update();
			rs.MoveNext();

		}
	}
	AlcoholTypeIndex.Close();
	rs.Close();

	//更新香型索引
	CFlavourTypeIndex FlavourTypeIndex;

	if(!FlavourTypeIndex.Open())
	{
		MessageBox(L"打开光谱索引库失败",L"更新失败",MB_ICONERROR);
		return;
	}
	if(!FlavourTypeIndex.IsEOF())
	{
		FlavourTypeIndex.MoveFirst();
		while(!FlavourTypeIndex.IsEOF())
		{
			FlavourTypeIndex.Delete();
			FlavourTypeIndex.MoveNext();
		}
	}
	command=_T("SELECT DISTINCT Flavour FROM TrueWineLib");
	if(!rs.Open(AFX_DB_USE_DEFAULT_TYPE,command))
	{
		MessageBox(L"打开真酒库失败",L"更新失败",MB_ICONERROR);
		return;
	}

	if(!rs.IsEOF())
	{
		int count=1;
		rs.MoveFirst();
		while(!rs.IsEOF())
		{
			rs.GetFieldValue(L"Flavour",Flavour);
			FlavourTypeIndex.AddNew();
			FlavourTypeIndex.m_Flavour=Flavour;
			FlavourTypeIndex.m_FlavourIndex=count;
			count++;
			rs.MoveNext();
			FlavourTypeIndex.Update();
		}
	}
	rs.Close();

	//更新品牌索引
	CBrandTypeIndex BrandTypeIndex;

	if(!BrandTypeIndex.Open())
	{
		MessageBox(L"打开光谱索引库失败",L"更新失败",MB_ICONERROR);
		return;
	}
	if(!BrandTypeIndex.IsEOF())
	{
		BrandTypeIndex.MoveFirst();
		while(!BrandTypeIndex.IsEOF())
		{
			BrandTypeIndex.Delete();
			BrandTypeIndex.MoveNext();
		}
	}
	command=_T("SELECT DISTINCT Brand FROM TrueWineLib");
	if(!rs.Open(AFX_DB_USE_DEFAULT_TYPE,command))
	{
		MessageBox(L"打开真酒库失败",L"更新失败",MB_ICONERROR);
		return;
	}

	if(!rs.IsEOF())
	{
		int count=1;
		rs.MoveFirst();
		while(!rs.IsEOF())
		{
			rs.GetFieldValue(L"Brand",Brand);
			BrandTypeIndex.AddNew();
			BrandTypeIndex.m_Brand=Brand;
			BrandTypeIndex.m_BrandIndex=count;
			count++;
			rs.MoveNext();
			BrandTypeIndex.Update();
		}
	}
	rs.Close();


}
Beispiel #5
0
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
}