void CT3000App::JudgeDB(){
 CADO ado;
 int versionno=0;
 ado.OnInitADOConn();
 if (ado.IsHaveTable(ado,_T("Version")))//有Version表
 {
   CString sql=_T("Select * from Version");
   ado.m_pRecordset=ado.OpenRecordset(sql);
   ado.m_pRecordset->MoveFirst();
   while (!ado.m_pRecordset->EndOfFile)
   {
     versionno=ado.m_pRecordset->GetCollect(_T("VersionNO"));
	 ado.m_pRecordset->MoveNext();
   }
   ado.CloseRecordset();
  
   
 } 
  ado.CloseConn();

 if (g_versionNO>versionno)//版本过低
 {
     SetPaneString(0,_T("The version of DB is lower,Updating....."));
	 _ConnectionPtr srcConTmp;
	 _RecordsetPtr srcRsTemp;
	 srcConTmp.CreateInstance("ADODB.Connection");
	 srcRsTemp.CreateInstance("ADODB.Recordset");
	 srcConTmp->Open(g_strDatabasefilepath.GetString(),"","",adModeUnknown);
	 srcRsTemp->Open(_T("select * from ALL_NODE"),_variant_t((IDispatch *)srcConTmp,true),adOpenStatic,adLockOptimistic,adCmdText);		

	 ALL_NODE temp;
	 while(!srcRsTemp->EndOfFile){
		 temp.MainBuilding_Name=srcRsTemp->GetCollect(_T("MainBuilding_Name"));
		 temp.Building_Name=srcRsTemp->GetCollect(_T("Building_Name"));
		 temp.Serial_ID=srcRsTemp->GetCollect(_T("Serial_ID"));
		 temp.Floor_name=srcRsTemp->GetCollect(_T("Floor_name"));
		 temp.Room_name=srcRsTemp->GetCollect(_T("Room_name"));
		 temp.Product_name=srcRsTemp->GetCollect(_T("Product_name"));
		 temp.Product_class_ID=srcRsTemp->GetCollect(_T("Product_class_ID"));
		 temp.Product_ID=srcRsTemp->GetCollect(_T("Product_ID"));
		 temp.Screen_Name=srcRsTemp->GetCollect(_T("Screen_Name"));
		 temp.Bautrate=srcRsTemp->GetCollect(_T("Bautrate"));
		 temp.Background_imgID=srcRsTemp->GetCollect(_T("Background_imgID"));
		 temp.Hardware_Ver=srcRsTemp->GetCollect(_T("Hardware_Ver"));
		 temp.Software_Ver=srcRsTemp->GetCollect(_T("Software_Ver"));
//		 temp.Com_Port=srcRsTemp->GetCollect(_T("Com_Port"));
		 temp.EPsize=srcRsTemp->GetCollect(_T("EPsize"));
		 // temp.Protocol=srcRsTemp->GetCollect(_T("Protocol"));

		 srcRsTemp->MoveNext();

		 m_AllNodes.push_back(temp);
	 }

	 srcRsTemp->Close();
	 if (srcConTmp->State)
	 {srcConTmp->Close();
	 }
	 
	 

	 
	 CString filePath=g_strExePth+_T("Database\\T3000.mdb");
	 DeleteFile(filePath);



	 HANDLE hFind;//
	 WIN32_FIND_DATA wfd;//
	 hFind = FindFirstFile(filePath, &wfd);//
	 if (hFind==INVALID_HANDLE_VALUE)//说明当前目录下无t3000.mdb
	 {
		 //CopyFile(g_strOrigDatabaseFilePath,g_strDatabasefilepath,FALSE);//
		 //没有找到就创建一个默认的数据库
		 filePath=g_strExePth+_T("Database\\T3000.mdb");
		 HRSRC hrSrc = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(IDR_T3000_DATABASE), _T("DB"));   
		 HGLOBAL hGlobal = LoadResource(AfxGetResourceHandle(), hrSrc);   


		 LPVOID lpExe = LockResource(hGlobal);   
		 CFile file;
		 if(file.Open(filePath, CFile::modeCreate | CFile::modeWrite))    
			 file.Write(lpExe, (UINT)SizeofResource(AfxGetResourceHandle(), hrSrc));    
		 file.Close();    
		 ::UnlockResource(hGlobal);   
		 ::FreeResource(hGlobal);
	 }//
	 CString strsql;
	 srcConTmp->Open(g_strDatabasefilepath.GetString(),"","",adModeUnknown);

	 try
	 {

		 for (int i=0;i<m_AllNodes.size();i++)
		 {
			 strsql.Format(_T("insert into ALL_NODE (MainBuilding_Name,Building_Name,Serial_ID,Floor_name,Room_name,Product_name,Product_class_ID,Product_ID,Screen_Name,Bautrate,Background_imgID,Hardware_Ver,Software_Ver,EPsize) values('"
				 +m_AllNodes[i].MainBuilding_Name+"','"
				 +m_AllNodes[i].Building_Name+"','"
				 +m_AllNodes[i].Serial_ID+"','"
				 +m_AllNodes[i].Floor_name+"','"
				 +m_AllNodes[i].Room_name+"','"
				 +m_AllNodes[i].Product_name+"','"
				 +m_AllNodes[i].Product_class_ID+"','"
				 +m_AllNodes[i].Product_ID+"','"
				 +m_AllNodes[i].Screen_Name+"','"
				 +m_AllNodes[i].Bautrate+"','"
				 +m_AllNodes[i].Background_imgID+"','"
				 +m_AllNodes[i].Hardware_Ver+"','"
				 +m_AllNodes[i].Software_Ver+"','"
				 +m_AllNodes[i].EPsize+"')"));


			 srcConTmp->Execute(strsql.GetString(),NULL,adCmdText);	
		 }
	 }
	 catch(_com_error *e)
	 {
		 AfxMessageBox(e->ErrorMessage());
	 }

	 if (srcConTmp->State)
	 {srcConTmp->Close();
	 }
	 srcConTmp=NULL;

 }



}
void CModbusPollView::Initial_RegName(){
	_variant_t temp_var;
	if ((!m_isgetmodel)&&(!m_ischangedAddress)&&(!m_ischangeModelName))
	{
		return;
	}
	 
	 //	m_ischangeModelName=FALSE
	m_VecregisterData.clear();
	for (int i=0;i<127;i++)
	{
		m_Alias[i]=_T("");
	}
	CString TableName,RegName,RegAddress;
	CADO ado;
	ado.OnInitADOConn();
	if(ado.IsHaveTable(ado,_T("ProductsTypeRegisterTables")))
	{  
		CString sql,temp;
		sql.Format(_T("Select * from ProductsTypeRegisterTables where ProductType=%d"),m_modeldata[1]);
		m_cur_modelNo=m_modeldata[1];
		ado.m_pRecordset=ado.OpenRecordset(sql);
		if (!ado.m_pRecordset->EndOfFile)
		{
            temp_var=ado.m_pRecordset->GetCollect(_T("TableName"));
            if (temp_var.vt!=VT_NULL)
            {
                TableName=temp_var;
            }
            else
            {
                TableName=_T("");
            }
			m_cur_TableName=TableName;
			temp_var=ado.m_pRecordset->GetCollect(_T("Col_RegName"));
			if (temp_var.vt!=VT_NULL)
			{
				RegName=temp_var;
			}
			else
			{
				RegName=_T("");
			}
			m_cur_Col_RegName=RegName;
			temp_var=ado.m_pRecordset->GetCollect(_T("Col_RegAddress"));
			if (temp_var.vt!=VT_NULL)
			{
				RegAddress=temp_var;
			}
			else
			{
				RegAddress=_T("");
			}
			m_cur_col_RegAddress=RegAddress;
			temp_var=ado.m_pRecordset->GetCollect(_T("ProductName"));
			if (temp_var.vt!=VT_NULL)
			{
				m_modelname=temp_var;
			}
			else
			{
				m_modelname=_T("");
			}
			m_cur_modelName=m_modelname;
		}
	 
	}
	ado.CloseRecordset();
	if (TableName.GetLength()!=0)
	{
		if (ado.IsHaveTable(ado,TableName))
		{
			DBRegister tempstuct;
			CString sql;
		    if (TableName.CompareNoCase(_T("CustomProductTable"))==0)
		    {
				sql.Format(_T("Select [%s],[%s] from %s where ModelNo=%d "),RegName.GetBuffer(),RegAddress.GetBuffer(),TableName.GetBuffer(),m_cur_modelNo);
				ado.m_pRecordset=ado.OpenRecordset(sql);
				while (!ado.m_pRecordset->EndOfFile)
				{

					temp_var=ado.m_pRecordset->GetCollect(RegAddress.GetBuffer());
					if (temp_var.vt!=VT_NULL)
					{
						tempstuct.RegAddress=temp_var;
					}
					temp_var=ado.m_pRecordset->GetCollect(RegName.GetBuffer());

					if (temp_var.vt!=VT_NULL)
					{
						tempstuct.RegName=temp_var;
					}
					m_VecregisterData.push_back(tempstuct);
					ado.m_pRecordset->MoveNext();
				}
		    } 
		    else
		    {
				sql.Format(_T("Select [%s],[%s] from %s "),RegName.GetBuffer(),RegAddress.GetBuffer(),TableName.GetBuffer());
				ado.m_pRecordset=ado.OpenRecordset(sql);
				while (!ado.m_pRecordset->EndOfFile)
				{

					temp_var=ado.m_pRecordset->GetCollect(RegAddress.GetBuffer());
					if (temp_var.vt!=VT_NULL)
					{
						tempstuct.RegAddress=temp_var;
					}
					temp_var=ado.m_pRecordset->GetCollect(RegName.GetBuffer());

					if (temp_var.vt!=VT_NULL)
					{
						tempstuct.RegName=temp_var;
					}
					m_VecregisterData.push_back(tempstuct);
					ado.m_pRecordset->MoveNext();
				}
		    }
		
			
		}


		if (m_VecregisterData.size()>=1)
		{
			int reglen=127;
			if (m_VecregisterData.size()<127)
			{
				reglen=m_VecregisterData.size();
			}
			for (int i=0;i<reglen;i++)
			{
				m_Alias[i]=Find_RegName(i);
			}
		}

		m_ischangedAddress=FALSE;
		m_isnewmodel=FALSE;
	}
 	else
 	{
 		m_isnewmodel=TRUE;
 		for (int i=0;i<127;i++)
 		{
 			m_Alias[i]=_T("");
 		}
 	}
	ado.CloseConn();


	/*m_ischangeModelName=FALSE;*/
}
void CModbusPollView::OnEnKillfocusModelname()
{
    CString ModelName;
    GetDlgItem(IDC_MODELNAME)->GetWindowText(ModelName);


// 	cf=RGB(212,208,200);
// 	m_ModelNameRichEdit.SetReadOnly(TRUE);
// 	m_ModelNameRichEdit.SetBackgroundColor(FALSE,cf);
// 	showmodelname.Format(_T("%s"),m_modelname.GetBuffer());
// 	m_ModelNameRichEdit.SetWindowText(showmodelname);
// 	m_ModelNameRichEdit.SetStringFontSize(13);
if (ModelName.CompareNoCase(m_cur_modelName)==0)
{
return;
}
    
   if (m_isnewmodel)
   {
    int ret=   AfxMessageBox(_T("This is a new product model!\nDo you want to add it to Database?"),MB_OKCANCEL,0);

	if (ret==1)
	{
		CADO ado;
		ado.OnInitADOConn();
		if(ado.IsHaveTable(ado,_T("ProductsTypeRegisterTables")))
		{  
			CString sql;
			sql.Format(_T("Insert into ProductsTypeRegisterTables(ProductType,TableName,ProductName,Col_RegName,Col_RegAddress) values('%d','CustomProductTable','%s','Reg_Description','Reg_ID')"),m_modeldata[1],ModelName);
			try
			{
				ado.m_pConnection->Execute(sql.GetString(),NULL,adCmdText);
			}
			catch (_com_error *e)
			{
				AfxMessageBox(e->ErrorMessage());
			}
		}
	} 
	 
   }
   if (m_ischangeModelName)
   {
      int ret=   AfxMessageBox(_T("Are you sure to change the name of the unit?"),MB_OKCANCEL,0);
	   
		  if (ret==1)
		  {
			  CADO ado;
			  ado.OnInitADOConn();
			  if(ado.IsHaveTable(ado,_T("ProductsTypeRegisterTables")))
			  {  
				  CString sql;
				  sql.Format(_T("update  ProductsTypeRegisterTables Set ProductName='%s'  where ProductType=%d"),ModelName,m_cur_modelNo);
				  try
				  {
					  ado.m_pConnection->Execute(sql.GetString(),NULL,adCmdText);
				  }
				  catch (_com_error *e)
				  {
					  AfxMessageBox(e->ErrorMessage());
				  }
			  }
		  } 

	  }
    
 COLORREF  cf=RGB(212,208,200);
   m_ModelNameRichEdit.SetReadOnly(TRUE);
   m_ModelNameRichEdit.SetBackgroundColor(FALSE,cf);
   CString showmodelname;
   showmodelname.Format(_T("%s"),ModelName.GetBuffer());
   m_ModelNameRichEdit.SetWindowText(showmodelname);
   m_ModelNameRichEdit.SetStringFontSize(13);
   m_ischangeModelName=FALSE;
}
void CRegisterViewerDlg::LoadDataFromDB()
{   CString ProductModelName =Get_ProductModel();
	_variant_t temp_var;
    if (ProductModelName.Find(_T("TStat"))!=-1)//Tstat serial
    {
		CADO ado;
		ado.OnInitADOConn();
		if (ado.IsHaveTable(ado,_T("T3000_Register_Address_By_ID")))
		{  
			CString sql,temp;
			DBRegister tempstruct;
			sql.Format(_T("Select * from T3000_Register_Address_By_ID order by Register_Address"));
			ado.m_pRecordset=ado.OpenRecordset(sql);



			while (!ado.m_pRecordset->EndOfFile)//有表但是没有对应序列号的值
			{
				if (product_type==T3000_5ABCDFG_LED_ADDRESS)
				{
					tempstruct.Register_Address=ado.m_pRecordset->GetCollect(_T("Register_Address"));
					tempstruct.AddressName=ado.m_pRecordset->GetCollect(_T("TSTAT5_LED_AddressName"));
					tempstruct.DataType=ado.m_pRecordset->GetCollect(_T("TSTAT5_LED_DATATYPE"));
					tempstruct.length=ado.m_pRecordset->GetCollect(_T("TSTAT5_LED_LEN"));
					temp_var=ado.m_pRecordset->GetCollect(_T("TSTAT5_LED_DESCRIPTION"));
					if (temp_var.vt==VT_NULL)
					{
						tempstruct.Description=_T("");
					} 
					else
					{
						tempstruct.Description=temp_var;
					}
					temp_var=ado.m_pRecordset->GetCollect(_T("TSTAT5_LED_Operation"));
					if (temp_var.vt==VT_NULL)
					{
						tempstruct.Description=_T("");
					} 
					else
					{
						tempstruct.Operation=temp_var;
					}
					/*	tempstruct.Description=(CString)ado.m_pRecordset->GetCollect(_T("TSTAT5_LED_DESCRIPTION"));*/

				} 
				else if (product_type==T3000_5EH_LCD_ADDRESS)
				{
					_variant_t vartemp;
					tempstruct.Register_Address=ado.m_pRecordset->GetCollect(_T("Register_Address"));
					tempstruct.AddressName=ado.m_pRecordset->GetCollect(_T("TSTAT5_LCD_AddressName"));
					tempstruct.DataType=ado.m_pRecordset->GetCollect(_T("TSTAT5_LCD_DATATYPE"));
					tempstruct.length=ado.m_pRecordset->GetCollect(_T("TSTAT5_LCD_LEN"));
					//tempstruct.Description=(CString)ado.m_pRecordset->GetCollect(_T("TSTAT5_LCD_DESCRIPTION"));
					vartemp=ado.m_pRecordset->GetCollect(_T("TSTAT5_LCD_DESCRIPTION"));
					if (vartemp.vt==VT_NULL)
					{
						tempstruct.Description=_T("");
					} 
					else
					{
						tempstruct.Description=vartemp;
					} 
					temp_var=ado.m_pRecordset->GetCollect(_T("TSTAT5_LCD_Operation"));
					if (temp_var.vt==VT_NULL)
					{
						tempstruct.Description=_T("");
					} 
					else
					{
						tempstruct.Operation=temp_var;
					}
				}
				else if (product_type==T3000_6_ADDRESS)
				{
					tempstruct.Register_Address=ado.m_pRecordset->GetCollect(_T("Register_Address"));
					tempstruct.AddressName=ado.m_pRecordset->GetCollect(_T("TSTAT6_AddressName"));
					tempstruct.DataType=ado.m_pRecordset->GetCollect(_T("TSTAT6_DATATYPE"));
					tempstruct.length=ado.m_pRecordset->GetCollect(_T("TSTAT6_LEN"));
					temp_var=ado.m_pRecordset->GetCollect(_T("TSTAT6_DESCRIPTION")); 
					if (temp_var.vt==VT_NULL)
					{
						tempstruct.Description=_T("");
					} 
					else
					{
						tempstruct.Description=temp_var;
					}
					temp_var=ado.m_pRecordset->GetCollect(_T("TSTAT6_Operation"));
					if (temp_var.vt==VT_NULL)
					{
						tempstruct.Description=_T("");
					} 
					else
					{
						tempstruct.Operation=temp_var;
					}
				}
				if (tempstruct.AddressName.CompareNoCase(_T("RESERVED"))==0)
				{
					ado.m_pRecordset->MoveNext();
					continue;
				}
				m_VecregisterData.push_back(tempstruct);
				m_recordcount++;
				ado.m_pRecordset->MoveNext();
			}
		} 
		ado.CloseRecordset();
		ado.CloseConn();
    } 
    else if (ProductModelName.Find(_T("T3"))!=-1)//T3 Serial
    {
		CADO ado;
		ado.OnInitADOConn();
		if (ado.IsHaveTable(ado,_T("T3_RegisterList")))
		{
			

			CString sql,temp;
			DBRegister tempstruct;
			sql.Format(_T("Select * from T3_RegisterList order by RegID"));
			ado.m_pRecordset=ado.OpenRecordset(sql);
			if (m_modelno==PM_T3PT10)
			{
				while(!ado.m_pRecordset->EndOfFile){
					tempstruct.Register_Address=ado.m_pRecordset->GetCollect(_T("RegID"));
					temp_var=ado.m_pRecordset->GetCollect(_T("T3-RTD"));
					if (temp_var.vt==VT_NULL)
					{
						tempstruct.AddressName=_T("");
					} 
					else
					{
						tempstruct.AddressName=temp_var;
					}
					if ((tempstruct.AddressName.CompareNoCase(_T("RESERVED"))==0)||tempstruct.AddressName.IsEmpty())
					{
						ado.m_pRecordset->MoveNext();
						continue;
					}
					tempstruct.DataType=ado.m_pRecordset->GetCollect(_T("T3-8AI8AO_DATATYPE"));
					tempstruct.length=ado.m_pRecordset->GetCollect(_T("T3-8AI8AO_LEN"));
					temp_var=ado.m_pRecordset->GetCollect(_T("T3-8AI8AO_DESCRIPTION"));
					if (temp_var.vt==VT_NULL)
					{
						tempstruct.Description=_T("");
					} 
					else
					{
						tempstruct.Description=temp_var;
					}
					temp_var=ado.m_pRecordset->GetCollect(_T("T3-8AI8AO_OPERATION"));
					if (temp_var.vt==VT_NULL)
					{
						tempstruct.Description=_T("");
					} 
					else
					{
						tempstruct.Operation=temp_var;
					}
					m_VecregisterData.push_back(tempstruct);
					m_recordcount++;
					ado.m_pRecordset->MoveNext();
				}



			} 
			else if (m_modelno==PM_T3PT10)
			{
			while(!ado.m_pRecordset->EndOfFile){
				tempstruct.Register_Address=ado.m_pRecordset->GetCollect(_T("RegID"));
				temp_var=ado.m_pRecordset->GetCollect(_T("T3-RTD"));
				if (temp_var.vt==VT_NULL)
				{
					tempstruct.AddressName=_T("");
				} 
				else
				{
					tempstruct.AddressName=temp_var;
				}
				if ((tempstruct.AddressName.CompareNoCase(_T("RESERVED"))==0)||tempstruct.AddressName.IsEmpty())
				{
					ado.m_pRecordset->MoveNext();
					continue;
				}
				tempstruct.DataType=ado.m_pRecordset->GetCollect(_T("T3-8AI16O_DATATYPE"));
				tempstruct.length=ado.m_pRecordset->GetCollect(_T("T3-8AI16O_LEN"));
				temp_var=ado.m_pRecordset->GetCollect(_T("T3-8AI16O_DESCRIPTION"));
				if (temp_var.vt==VT_NULL)
				{
					tempstruct.Description=_T("");
				} 
				else
				{
					tempstruct.Description=temp_var;
				}
				temp_var=ado.m_pRecordset->GetCollect(_T("T3-8AI16O_OPERATION"));
				if (temp_var.vt==VT_NULL)
				{
					tempstruct.Description=_T("");
				} 
				else
				{
					tempstruct.Operation=temp_var;
				}
				m_VecregisterData.push_back(tempstruct);
				m_recordcount++;
				ado.m_pRecordset->MoveNext();
			}	
			}
			else if (m_modelno==PM_T3IOA)
			{
			}
			else if (m_modelno==PM_T332AI)
			{
			}
			else if (m_modelno==PM_T3AI16O)
			{
			}
			else if (m_modelno==PM_T3PERFORMANCE)
			{
			}
			else if (m_modelno==PM_T34AO)
			{
			while(!ado.m_pRecordset->EndOfFile){
				tempstruct.Register_Address=ado.m_pRecordset->GetCollect(_T("RegID"));
				temp_var=ado.m_pRecordset->GetCollect(_T("T3-4AO"));
				if (temp_var.vt==VT_NULL)
				{
					tempstruct.AddressName=_T("");
				} 
				else
				{
					tempstruct.AddressName=temp_var;
				}
				if ((tempstruct.AddressName.CompareNoCase(_T("RESERVED"))==0)||tempstruct.AddressName.IsEmpty())
				{
					ado.m_pRecordset->MoveNext();
					continue;
				}
				tempstruct.DataType=ado.m_pRecordset->GetCollect(_T("T3-4AO_DATATYPE"));
				tempstruct.length=ado.m_pRecordset->GetCollect(_T("T3-4AO_LEN"));
				temp_var=ado.m_pRecordset->GetCollect(_T("T3-4AO_DESCRIPTION"));
				if (temp_var.vt==VT_NULL)
				{
					tempstruct.Description=_T("");
				} 
				else
				{
					tempstruct.Description=temp_var;
				}
				temp_var=ado.m_pRecordset->GetCollect(_T("T3-4AO_OPERATION"));
				if (temp_var.vt==VT_NULL)
				{
					tempstruct.Description=_T("");
				} 
				else
				{
					tempstruct.Operation=temp_var;
				}
				m_VecregisterData.push_back(tempstruct);
				m_recordcount++;
				ado.m_pRecordset->MoveNext();

			}
			}
			else if (m_modelno==PM_T36CT)
			{
			}




			
		}
		ado.CloseRecordset();
		ado.CloseConn();
    }
	else
    {
    }


	CString temp;
	m_Datapages=(m_recordcount/ROWS)+1;
	temp.Format(_T("Product Name"));
	GetDlgItem(IDC_PRODUCTNAME)->SetWindowText(temp);
	
	GetDlgItem(IDC_PRODUCTNAME_T)->SetWindowText(GetProductName(product_register_value[7]));
	temp.Format(_T("%d/%d"),m_curPage,m_Datapages);
	GetDlgItem(IDC_PAGES)->SetWindowText(temp);

}