void CT3000App::ImportData(){
  CADO ado;
  ado.OnInitADOConn();
  CString sql=_T("select * from ALL_NODE");
  ado.m_pRecordset=ado.OpenRecordset(sql);
  ALL_NODE temp;
  while(!ado.m_pRecordset->EndOfFile){
	   temp.MainBuilding_Name=ado.m_pRecordset->GetCollect(_T("MainBuilding_Name"));
	   temp.Building_Name=ado.m_pRecordset->GetCollect(_T("Building_Name"));
	   temp.Serial_ID=ado.m_pRecordset->GetCollect(_T("Serial_ID"));
	   temp.Floor_name=ado.m_pRecordset->GetCollect(_T("Floor_name"));
	   temp.Room_name=ado.m_pRecordset->GetCollect(_T("Room_name"));
	   temp.Product_name=ado.m_pRecordset->GetCollect(_T("Product_name"));
	   temp.Product_class_ID=ado.m_pRecordset->GetCollect(_T("Product_class_ID"));
	   temp.Product_ID=ado.m_pRecordset->GetCollect(_T("Product_ID"));
	   temp.Screen_Name=ado.m_pRecordset->GetCollect(_T("Screen_Name"));
	   temp.Bautrate=ado.m_pRecordset->GetCollect(_T("Bautrate"));
	   temp.Background_imgID=ado.m_pRecordset->GetCollect(_T("Background_imgID"));
	   temp.Hardware_Ver=ado.m_pRecordset->GetCollect(_T("Hardware_Ver"));
	   temp.Software_Ver=ado.m_pRecordset->GetCollect(_T("Software_Ver"));
	   temp.Com_Port=ado.m_pRecordset->GetCollect(_T("Com_Port"));
	   temp.EPsize=ado.m_pRecordset->GetCollect(_T("EPsize"));
	   temp.Protocol=ado.m_pRecordset->GetCollect(_T("Protocol"));
      
	   ado.m_pRecordset->MoveNext();

	   m_AllNodes.push_back(temp);
  }
  ado.CloseRecordset();
  ado.CloseConn();
 }
void CRegisterWriteValueDlg::OnBnClickedOk()
{
   if (m_col==0)
   {
	   if (!m_CanWrite)
	   {
		   AfxMessageBox(_T("The register can't be written.\nBecause it is read-only."));
		   return;
	   }
	   m_valueEdit.GetWindowText(m_value);
	   int value=_wtoi(m_value);
	   int Add=_wtoi(m_no);
	   int ret=write_one(g_tstat_id,Add,value);
	   if (ret<0)
	   {
		   AfxMessageBox(_T("Write Fail"));
		   m_color=1;
		   return;
	   }
	   product_register_value[Add]=value;
	   m_color=0;
	   AfxMessageBox(_T("Write OK"));
   } 
   else
   {
   int Address=_wtoi(m_no);
   CString description;
   m_descriptionEdit.GetWindowText(description);
   CADO ado;
   ado.OnInitADOConn();
   CString sql;
   if (product_type==T3000_5ABCDFG_LED_ADDRESS)
   {
       try
       {
		   sql.Format(_T("update T3000_Register_Address_By_ID set TSTAT5_LED_DESCRIPTION='%s' where Register_Address=%d"),description.GetBuffer(),Address);
		   ado.m_pConnection->Execute(sql.GetString(),NULL,adCmdText);
		   AfxMessageBox(_T("Save OK"));
       }
       catch (CException* e)
       {
	      AfxMessageBox(_T("Save Error"));
       }
	   
   }
   else if (product_type==T3000_5EH_LCD_ADDRESS)
   {
	   try
	   {
		   sql.Format(_T("update T3000_Register_Address_By_ID set TSTAT5_LCD_DESCRIPTION='%s' where Register_Address=%d"),description.GetBuffer(),Address);
		   ado.m_pConnection->Execute(sql.GetString(),NULL,adCmdText);
		   AfxMessageBox(_T("Save OK"));
	   }
	   catch (CException* e)
	   {
		   AfxMessageBox(_T("Save Error"));
	   }
   }
   else if (product_type==T3000_6_ADDRESS)
   {
	   try
	   {
		   sql.Format(_T("update T3000_Register_Address_By_ID set TSTAT6_DESCRIPTION='%s' where Register_Address=%d"),description.GetBuffer(),Address);
		   ado.m_pConnection->Execute(sql.GetString(),NULL,adCmdText);
		   AfxMessageBox(_T("Save OK"));
	   }
	   catch (CException* e)
	   {
		   AfxMessageBox(_T("Save Error"));
	   }
   }
   else
   {

   }

   }
	
  
}
void CAirQuality::pctodb()
{
	TRACE(_T("Updatedatabase()start!\n"));
	KillTimer(AirQualityTimer);
	//存入数据库中
	CADO saveADO;
	saveADO.OnInitADOConn();
	CString sql = _T("select * from AirQuality");
	saveADO.m_pRecordset = saveADO.OpenRecordset(sql);
	if (!saveADO.m_pRecordset->EndOfFile)
	{
		saveADO.m_pRecordset->MoveFirst();

		for(int i = 0;i<400;i++)
		{
			try 
			{

				saveADO.m_pRecordset->PutCollect("Address",(_bstr_t)vecairquality.at(i).iaddress);
				saveADO.m_pRecordset->PutCollect("Data",(_bstr_t)vecairquality.at(i).CStvalue);


				saveADO.m_pRecordset->Update();
				saveADO.m_pRecordset->MoveNext();
			}


			catch(...)
			{
				SetPaneString(2,_T("Write dababase false!"));
				return ;
			}
		}
	}
	else
	{
		for(int i = 0;i<400;i++)
		{
			try 
			{

				saveADO.m_pRecordset->AddNew();
				saveADO.m_pRecordset->PutCollect("Address",(_bstr_t)vecairquality.at(i).iaddress);
				saveADO.m_pRecordset->PutCollect("Data",(_bstr_t)vecairquality.at(i).CStvalue);

				saveADO.m_pRecordset->Update();

			}


			catch(...)
			{
				SetPaneString(2,_T("Write dababase false!"));
				return;
			}
		}

	}

	saveADO.CloseRecordset();
	saveADO.CloseConn(); 

	SetTimer(AirQualityTimer,1000,NULL);

}
void CDialogT3::Updatedatabase()
{
    TRACE(_T("Updatedatabase()start!\n"));
    KillTimer(T3_MODEL);
    //存入数据库中
    CADO saveADO;
    saveADO.OnInitADOConn();
    CString sql = _T("select * from T3_4AO");
    saveADO.m_pRecordset = saveADO.OpenRecordset(sql);
    if (!saveADO.m_pRecordset->EndOfFile)
    {
        saveADO.m_pRecordset->MoveFirst();

        for(int i = 0; i<230; i++)
        {
            try
            {

                //saveADO.m_pRecordset->AddNew();
                //saveADO.m_pRecordset->Move(pos,vtMissing);
                // 			cstnum.Format(_T("%d"),i+1);
                // 			saveADO.m_pRecordset->PutCollect("Address",(_bstr_t)cstnum);
                // 			saveADO.m_pRecordset->PutCollect("Name",(_bstr_t)_T("output"));

                saveADO.m_pRecordset->PutCollect("Address",(_bstr_t)(i+1));
                saveADO.m_pRecordset->PutCollect("Name",(_bstr_t)vect3model.at(i).CStName);
                saveADO.m_pRecordset->PutCollect("Data",(_bstr_t)shmPRegister[i]);
                saveADO.m_pRecordset->PutCollect("Range",(_bstr_t)vect3model.at(i).CStrange);


                if (i>=124&&i<=147)
                {
                    switch ((int)(_variant_t)shmPRegister[i])
                    {
                    case 0:   //0- off 1 - mannel 2 - auto
                        saveADO.m_pRecordset->PutCollect("AutoManual",(_bstr_t)"OFF");
                        break;
                    case 1:
                        saveADO.m_pRecordset->PutCollect("AutoManual",(_bstr_t)"Manual");
                        break;
                    case 2:
                        saveADO.m_pRecordset->PutCollect("AutoManual",(_bstr_t)"Auto");
                        break;
                    default:
                        break;

                    }
                }


                //  		saveADO.m_pRecordset->PutCollect("Auto/Manual",(_bstr_t)"auto");
                //  		saveADO.m_pRecordset->PutCollect("Range",(_bstr_t)"10%-100%");
                saveADO.m_pRecordset->Update();
                saveADO.m_pRecordset->MoveNext();
            }


            catch(...)
            {


                SetPaneString(2,_T("Write dababase false!"));
                return ;
            }
        }
    }
    else
    {
        for(int i = 0; i<230; i++)
        {
            try
            {

                //saveADO.m_pRecordset->AddNew();
                //saveADO.m_pRecordset->Move(pos,vtMissing);
                // 			cstnum.Format(_T("%d"),i+1);
                // 			saveADO.m_pRecordset->PutCollect("Address",(_bstr_t)cstnum);
                // 			saveADO.m_pRecordset->PutCollect("Name",(_bstr_t)_T("output"));
                saveADO.m_pRecordset->AddNew();
                saveADO.m_pRecordset->PutCollect("Address",(_bstr_t)(i+1));
                saveADO.m_pRecordset->PutCollect("Name",(_bstr_t)vect3model.at(i).CStName);
                saveADO.m_pRecordset->PutCollect("Data",(_bstr_t)shmPRegister[i]);
                saveADO.m_pRecordset->PutCollect("Range",(_bstr_t)vect3model.at(i).CStrange);


                if (i>=124&&i<=147)
                {
                    switch ((int)(_variant_t)shmPRegister[i])
                    {
                    case 0:   //0- off 1 - mannel 2 - auto
                        saveADO.m_pRecordset->PutCollect("AutoManual",(_bstr_t)"OFF");
                        break;
                    case 1:
                        saveADO.m_pRecordset->PutCollect("AutoManual",(_bstr_t)"Manual");
                        break;
                    case 2:
                        saveADO.m_pRecordset->PutCollect("AutoManual",(_bstr_t)"Auto");
                        break;
                    default:
                        break;

                    }
                }


                //  		saveADO.m_pRecordset->PutCollect("Auto/Manual",(_bstr_t)"auto");
                //  		saveADO.m_pRecordset->PutCollect("Range",(_bstr_t)"10%-100%");

                saveADO.m_pRecordset->Update();

            }


            catch(...)
            {
                //MessageBox(_T("Write dababase false!"));

                SetPaneString(2,_T("Write dababase false!"));
                return ;
            }
        }

    }

    saveADO.CloseRecordset();
    saveADO.CloseConn();

    SetTimer(T3_MODEL,1000,NULL);

}
void CARDDlg::OnBnClickedOk()
{
   CString ip;
   CString strport;
   m_ipaddress.GetWindowText(ip);
   m_porteditor.GetWindowText(strport);
   if (ip.IsEmpty()||strport.IsEmpty())
   {
	   AfxMessageBox(_T("ip or port can not be empty!"));
	   return;
   }
   short port=_wtoi(strport);
   SetCommunicationType(1);
   BOOL is_open=Open_Socket2(ip,port);
   if (is_open)
   {
       //unsigned short data[2];
	   //int ModelID=Read_Multi(209,data,6,2);//  (255,6,20);
	   //CString show;
	   //show.Format(_T("Add=%d,Model=%d"),data[0],data[1]);
	   //ModelID=read_one(data[0],7);
	   //AfxMessageBox(show);
	   //show.Format(_T("Model=%d"),ModelID);
	   //AfxMessageBox(show);
	  unsigned short Data[10];
	  int ret=Read_Multi(255,Data,0,10);
	  if (ret<0)
	  {
	  AfxMessageBox(_T("Can't get the information of your device!\n Please try again."));
	  ret;
	  }

	   CString strSql;
	   CString strMainBuildName,strSubBuildingName,strSID,strFloorName,strRoomName,strProID;
	   CString strProName,strProType,strScreenID,strBaudrate,strGraphicID,strHdVersion,strStVersion,strCom,strEPSize;

	  
	   
	  /* CString temp_str=_T("select * from Building where Default_SubBuilding=-1");
	   m_pRs->Open(_variant_t(temp_str),_variant_t((IDispatch *)m_pCon,true),adOpenStatic,adLockOptimistic,adCmdText);	
	   int recordcount=m_pRs->GetRecordCount();
	   if(m_pRs->GetRecordCount()<=0)
	   {
		   AfxMessageBox(_T("There is no default building,please select a building First."));
		   return;
	   }


	   if(m_pRs->State)
		   m_pRs->Close(); 
	   if(m_pCon->State)
           m_pCon->Close();*/
	   CMainFrame* pFrame=(CMainFrame*)(AfxGetApp()->m_pMainWnd);
	   strMainBuildName= pFrame->m_strCurMainBuildingName;
	   strSubBuildingName= pFrame->m_strCurSubBuldingName;
	   int SN=Data[3]*256*256*256+Data[2]*256*256+Data[1]*256+Data[0];
       strSID.Format(_T("%d"),SN);
	   strFloorName=_T("floor1");
	   strRoomName=_T("room1");
	   strProName.Format(_T("%s:%d--%d"),GetProductName(Data[7]).GetBuffer(),SN,Data[6]);
	   strProType.Format(_T("%d"),Data[7]);
	   strProID.Format(_T("%d"),Data[6]);

	   strScreenID.Format(_T("Screen(S:%d--%d)"),SN,Data[6]);
	   m_ipaddress.GetWindowText(strBaudrate);
	   strGraphicID=_T("Clicking here to add a image...");
	   strHdVersion=_T("0.0");
	   strStVersion=_T("0.0");
	   m_porteditor.GetWindowText(strCom);
	   strEPSize=_T("0.0");
	   try
	   {
		   CADO ado;
		   ado.OnInitADOConn();
           strSql.Format(_T("Select * from ALL_NODE where Product_name='%s'"),strProName.GetBuffer());
           ado.m_pRecordset=ado.OpenRecordset(strSql);
           if (!ado.m_pRecordset->EndOfFile)//有表但是没有对应序列号的值
           {
            AfxMessageBox(_T("Already Exists!"));
            return;
            //ado.m_pRecordset->Close();
           }
		   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,Com_Port,EPsize) values('"
			   +strMainBuildName+"','"
			   +strSubBuildingName +"','"
			   +strSID+"','"
			   +strFloorName+"','"
			   +strRoomName+"','"
			   +strProName+"','"
			   +strProType+"','"
			   +strProID+"','"
			   +strScreenID+"','"
			   +strBaudrate+"','"
			   +strGraphicID+"','"
			   +strHdVersion+"','"
			   +strStVersion+"','"
			   +strCom+"','"
			   +strEPSize+"')"));
		   ado.m_pRecordset=ado.OpenRecordset(strSql);
		   ado.CloseConn();
		   AfxMessageBox(_T("Add OK!"));
           CDialogEx::OnOK();
	   }
	   
	   catch (CException* e)
	   {
		   AfxMessageBox(_T("Add Fail!"));
	   }
	  
	   
   }
   else
   {
	   AfxMessageBox(_T("Can not Connect"));
   }

}
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::OnEnKillfocusEditName()
{
	CString strText;
	m_edit_name.GetWindowTextW(strText);
	if (strText.IsEmpty())
	{
		return;
	}
	m_MsDataGrid.put_TextMatrix(m_Current_Row,m_Current_Col,strText);
	int Index;
	if (m_Current_Col%3==0)
	{

		Index=(m_Current_Col/3-1)*(m_MsDataGrid.get_Rows()-1)+(m_Current_Row-1);
	} 
	else
	{
		Index=(m_Current_Col/3)*(m_MsDataGrid.get_Rows()-1)+(m_Current_Row-1);
	}
	m_Alias[Index]=strText;
	int RegAddress=Get_Reg_Add(Index);
	CADO ado;
	ado.OnInitADOConn();
	CString SqlText;
	if (m_cur_TableName.CompareNoCase(_T("CustomProductTable"))==0)
	{
	SqlText.Format(_T("Select * from CustomProductTable where ModelNo=%d and Reg_ID=%d"),m_cur_modelNo,RegAddress);
	ado.m_pRecordset=ado.OpenRecordset(SqlText);
	if (!ado.m_pRecordset->EndOfFile)
	{
		//strSql.Format(_T("update ALL_NODE set Hardware_Ver ='%s' where Serial_ID = '%s' and Bautrate = '%s'"),hw_instance,str_serialid,str_baudrate);
		//m_pCon->Execute(strSql.GetString(),NULL,adCmdText);
		SqlText.Format(_T("update CustomProductTable set Reg_Description='%s' where ModelNo=%d and Reg_ID=%d "),strText.GetBuffer(),m_cur_modelNo,RegAddress);
		try
		{
		ado.m_pConnection->Execute(SqlText.GetString(),NULL,adCmdText);
		}
		catch (_com_error *e)
		{
			AfxMessageBox(e->ErrorMessage());
		}
	} 
	else
	{
		SqlText.Format(_T("Insert into CustomProductTable(Reg_Description,ModelNo,Reg_ID) values('%s',%d,%d)"),strText.GetBuffer(),m_cur_modelNo,RegAddress);
		try
		{
			ado.m_pConnection->Execute(SqlText.GetString(),NULL,adCmdText);
		}
		catch (_com_error *e)
		{
			AfxMessageBox(e->ErrorMessage());
		}
	}
	   
	} 
	else
	{
	   SqlText.Format(_T("Select * from %s where %s=%d"),m_cur_TableName,m_cur_col_RegAddress,RegAddress);
	   ado.m_pRecordset=ado.OpenRecordset(SqlText);
	   if (!ado.m_pRecordset->EndOfFile)
	   {
	     SqlText.Format(_T("update %s set %s ='%s' where %s =%d "),m_cur_TableName,m_cur_Col_RegName,strText.GetBuffer(),m_cur_col_RegAddress,RegAddress);
		 try
		 {
		 ado.m_pConnection->Execute(SqlText.GetString(),NULL,adCmdText);
		 }
		 catch (_com_error *e)
		 {
		 	AfxMessageBox(e->ErrorMessage());
		 }
		 
	   }
	}

}
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 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 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);

}
   	/*
	Date:2013/05/29
	Purpose:
	parameters 1:根据层次
	2:改变的名字,有新旧比较
   	*/
BOOL CWorkspaceBar::UpdateDataToDB(){
	/* _ConnectionPtr m_pCon;
	 _RecordsetPtr m_pRs;
	 ::CoInitialize(NULL);*/
	CADO ado;
	ado.OnInitADOConn();

	CBADO bado;
	bado.SetDBPath(g_strCurBuildingDatabasefilePath);
	bado.OnInitADOConn();

	 try 
	 {
		 ////////////////////////////////////////////////////////////////////////////////////////////
		 //获取数据库名称及路径
		 /////////////////////////////////////////////////////////////////////////////////////////////////
		 //连接数据库
	/*	 m_pCon.CreateInstance("ADODB.Connection");
		 m_pRs.CreateInstance(_T("ADODB.Recordset"));
		 m_pCon->Open(g_strDatabasefilepath.GetString(),"","",adModeUnknown);*/
		 CString strSql;   BOOL is_exist=FALSE;	  CString str_temp;
         switch (m_level)
         {
         case 0:  //Subnet
		 {
				 
						
				 //strSql.Format(_T("select * from Building where Main_BuildingName = '%s'"),m_strMainBuildingName);
				 strSql.Format(_T("select * from Building order by Main_BuildingName"));
				 //m_pRs->Open((_variant_t)strSql,_variant_t((IDispatch *)m_pCon,true),adOpenStatic,adLockOptimistic,adCmdText);
				 ado.m_pRecordset=ado.OpenRecordset(strSql);
				 while(VARIANT_FALSE== ado.m_pRecordset->EndOfFile)
				 {	   	 str_temp.Empty();
					 str_temp= ado.m_pRecordset->GetCollect("Building_Name");
				 if (str_temp.Compare(m_name_new)==0)
				 {	   is_exist=TRUE;
				 break;
				 }			
					  ado.m_pRecordset->MoveNext();
				 }
				// m_pRs->Close();
				ado.CloseRecordset();
				 if (!is_exist)	 //更新的名字在数据库中查找不到的
				 {
				 ////////////先更新Building表/////////////////////
					 //CString strSql;
					 strSql.Format(_T("delete * from Building_ALL where Building_Name = '%s' "),m_name_old);
					 ado.m_pConnection->Execute(strSql.GetString(),NULL,adCmdText);

					 strSql.Format(_T("Insert into Building_ALL(Building_Name,Default_Build) values('%s','%d')"),m_name_new,1);
					 ado.m_pConnection->Execute(strSql.GetString(),NULL,adCmdText);

					 strSql.Format(_T("update Building set Building_Name='%s',Main_BuildingName= where Building_Name='%s'"),m_name_new,m_name_new,m_name_old);
					 //MessageBox(strSql);
					 ado.m_pConnection->Execute(strSql.GetString(),NULL,adCmdText);


					 strSql.Format(_T("select * from ALL_NODE where Building_Name='%s' order by Building_Name"),m_name_old);

					 bado.m_pRecordset=bado.OpenRecordset(strSql);
					 while(VARIANT_FALSE== bado.m_pRecordset->EndOfFile)
					 {
						 strSql.Format(_T("update ALL_NODE set Building_Name='%s' where Building_Name='%s'"),m_name_new,m_name_old);
						 bado.m_pConnection->Execute(strSql.GetString(),NULL,adCmdText);
						 bado.m_pRecordset->MoveNext();
					 }

					 //m_pRs->Close();
					 bado.CloseRecordset();

				 }
				 else
				 {

					 return FALSE;
				 }

				 break;	
				 
          
			 }
		 case 1:		//Floor
		 {	 // strSql=_T("select * from Building where Default_SubBuilding=-1");
             // m_pRs->Open((_variant_t)strSql,_variant_t((IDispatch *)m_pCon,true),adOpenStatic,adLockOptimistic,adCmdText);
			 HTREEITEM parentnode=m_TreeCtrl.GetParentItem(m_hSelItem);

			  CString subnetname=m_TreeCtrl.GetItemText(parentnode);//m_pRs->GetCollect("Building_Name");
	 
			 //  m_pRs->Close();
			   strSql.Format(_T("select * from ALL_NODE where Building_Name='%s' and Floor_name='%s' order by Building_Name"),subnetname,m_name_new);
				// m_pRs->Open((_variant_t)strSql,_variant_t((IDispatch *)m_pCon,true),adOpenStatic,adLockOptimistic,adCmdText);
			  bado.m_pRecordset=bado.OpenRecordset(strSql);
				 while(VARIANT_FALSE== bado.m_pRecordset->EndOfFile)
				 {	   	 str_temp.Empty();
				 str_temp= bado.m_pRecordset->GetCollect("Floor_name");
				 if (str_temp.Compare(m_name_new)==0)
				 {is_exist=TRUE;
				 break;
				 }			
				  bado.m_pRecordset->MoveNext();
				 }
				 bado.CloseRecordset();

				if (is_exist)
				{
				return FALSE;
				} 
				else
				{  
				strSql.Format(_T("select * from ALL_NODE where Building_Name='%s' and Floor_name='%s' order by Building_Name"),subnetname,m_name_old);
				//m_pRs->Open((_variant_t)strSql,_variant_t((IDispatch *)m_pCon,true),adOpenStatic,adLockOptimistic,adCmdText);
				bado.m_pRecordset=bado.OpenRecordset(strSql);
				while(VARIANT_FALSE==bado.m_pRecordset->EndOfFile)
				{
					strSql.Format(_T("update ALL_NODE set Floor_name='%s' where Floor_name='%s'"),m_name_new,m_name_old);
					bado.m_pConnection->Execute(strSql.GetString(),NULL,adCmdText);
					bado.m_pRecordset->MoveNext();
				}
				//m_pRs->Close();
				bado.CloseRecordset();
				return TRUE;
				}
			
		 
		 break;
         }
		 case 2:		//Room
		 {
			//Subnet
			HTREEITEM root=m_TreeCtrl.GetRootItem();
			CString subnetname=m_TreeCtrl.GetItemText(root);
		   //Floor
			HTREEITEM parentnode=m_TreeCtrl.GetParentItem(m_hSelItem);
			CString Floorname=m_TreeCtrl.GetItemText(parentnode);
			  strSql.Format(_T("select * from ALL_NODE where Building_Name='%s' and Floor_name='%s' and Room_name='%s' order by Building_Name"),subnetname,Floorname,m_name_new);
			 // m_pRs->Open((_variant_t)strSql,_variant_t((IDispatch *)m_pCon,true),adOpenStatic,adLockOptimistic,adCmdText);
			 bado.m_pRecordset=bado.OpenRecordset(strSql);
			  while(VARIANT_FALSE==bado.m_pRecordset->EndOfFile)
			  {	   	 str_temp.Empty();
			  str_temp=bado.m_pRecordset->GetCollect("Room_name");
			  if (str_temp.Compare(m_name_new)==0)
			  {	   is_exist=TRUE;
			  break;
			  }			
			  bado.m_pRecordset->MoveNext();
			  }
			 // bado.m_pRecordset->Close();
			 bado.CloseRecordset();

			  if (is_exist)
			  {
			  return FALSE;
			  } 
			  else
			  {strSql.Format(_T("select * from ALL_NODE where Building_Name='%s' and Floor_name='%s' and Room_name='%s' order by Building_Name"),subnetname,Floorname,m_name_old);
			  //bado.m_pConnection->Open((_variant_t)strSql,_variant_t((IDispatch *)m_pCon,true),adOpenStatic,adLockOptimistic,adCmdText);
			   bado.m_pRecordset=bado.OpenRecordset(strSql);
			  while(VARIANT_FALSE==bado.m_pRecordset->EndOfFile)
			  {
				  strSql.Format(_T("update ALL_NODE set Room_name='%s' where Room_name='%s'"),m_name_new,m_name_old);
				  bado.m_pConnection->Execute(strSql.GetString(),NULL,adCmdText);
				  bado.m_pRecordset->MoveNext();
			  }
			  //m_pRs->Close();
			  bado.CloseRecordset();
			  return TRUE; 
			  }
			  																				  
			    
		  break;
		 }
		 case 3:		//Device Name Leaf
		 {
			 //Subnet
			 HTREEITEM root=m_TreeCtrl.GetRootItem();
			 CString subnetname=m_TreeCtrl.GetItemText(root);
			 //Room
			 HTREEITEM parentnode=m_TreeCtrl.GetParentItem(m_hSelItem);
			 CString Roomname=m_TreeCtrl.GetItemText(parentnode);
			 //Floor
			 HTREEITEM floornode=m_TreeCtrl.GetParentItem(parentnode);
			 CString Floorname=m_TreeCtrl.GetItemText(floornode);
			 strSql.Format(_T("select * from ALL_NODE where Building_Name='%s' and Floor_name='%s' and Room_name='%s' and Product_name='%s' order by Building_Name"),subnetname,Floorname,Roomname,m_name_new);
			// m_pRs->Open((_variant_t)strSql,_variant_t((IDispatch *)m_pCon,true),adOpenStatic,adLockOptimistic,adCmdText);
			 bado.m_pRecordset=bado.OpenRecordset(strSql);
			 while(VARIANT_FALSE== bado.m_pRecordset->EndOfFile)
			 {	   	 str_temp.Empty();
			 str_temp= bado.m_pRecordset->GetCollect("Product_name");
			 if (str_temp.Compare(m_name_new)==0)
			 {	   is_exist=TRUE;
			 break;
			 }			
			  bado.m_pRecordset->MoveNext();
			 }
			 
			// m_pRs->Close();	
			  bado.CloseRecordset();
			 if (is_exist)
			 {
			 return FALSE;
			 }
			 else
			 {
				  strSql.Format(_T("select * from ALL_NODE where Building_Name='%s' and Floor_name='%s' and Room_name='%s' and Product_name='%s' order by Building_Name"),subnetname,Floorname,Roomname,m_name_old);
			// m_pRs->Open((_variant_t)strSql,_variant_t((IDispatch *)m_pCon,true),adOpenStatic,adLockOptimistic,adCmdText);
			 bado.m_pRecordset=bado.OpenRecordset(strSql);
			 while(VARIANT_FALSE== bado.m_pRecordset->EndOfFile)
			 {
				 strSql.Format(_T("update ALL_NODE set Product_name='%s' where Product_name='%s'"),m_name_new,m_name_old);
				 bado.m_pConnection->Execute(strSql.GetString(),NULL,adCmdText);
				  bado.m_pRecordset->MoveNext();
			 }
			 //m_pRs->Close();
			 bado.CloseRecordset();
			 return TRUE;
			 }
			 
		 break;
		 }
		 }
	 }
	 catch(_com_error e)
	 {
		/* AfxMessageBox(e.Description());*/
		 //MessageBox(m_name_new+_T("  has been here\n Please change another name!"));
		 bado.CloseConn();
		 return FALSE;
		 //m_pCon->Close();

	 }
	 ado.CloseRecordset();
	 ado.CloseConn();
	 bado.CloseRecordset();
	bado.CloseConn();
	return TRUE;
 }