⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainfrm.cpp

📁 从FTP下载文件下载到本地
💻 CPP
📖 第 1 页 / 共 4 页
字号:
		   }
		   else
		   {
             strTemp =_T("在FTP服務器中不存在該文件!");   
			   this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
			    
			   SetDoc(strTemp);
			   return; 
		   }
		   if (!DownFile())
		   {
			  strTemp =_T("從FTP中讀取文件失敗!");
             //AfxMessageBox(_T("從FTP中讀取文件失敗!"));
			  this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
			 SetDoc(strTemp);
			 return;
		   }
		   else
		   {
			   strTemp =_T("從FTP中讀取文件成功!");
			   //AfxMessageBox(_T("從FTP中讀取文件成功!"));
			   this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
			   SetDoc(strTemp);
				
		   }
		   OnLocalSearch() ;
		
		   if(OnRead())
		   {
			   strTemp =_T("數據庫已經準備好!");
			   //AfxMessageBox(_T("數據庫已經準備好!!"));
			   this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
			   SetDoc(strTemp);
		   }
		   else
		   {
			   strTemp =_T("對數據庫執行操作失敗!");
			   //AfxMessageBox(_T("對數據庫執行操作失敗!"));
			   this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
			   SetDoc(strTemp);
			   return;
		   }
		  
		   if (ReadCSV())
		   {
			    strTemp =_T("文件導入數據庫成功!");
                CString abc;
			    abc.Format(_T("%s 共写入了%d条记录。"),strTemp,m_nTimes);
			    AfxMessageBox(abc);
			    this->SendMessage( WM_STATUSMSG, (WPARAM)&abc );
			    SetDoc(abc);
		   }
		   else
		   {
			   strTemp =_T("數據導入數據庫失敗!");
			   AfxMessageBox(_T("數據導入數據庫失敗!"));
			   this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
			   SetDoc(strTemp);
			   return;
		   }          
		   
	}else
	{
        OnButtonSearch();
		if (bFileFind)
		{
		   strTemp =_T("您要讀取的文件存在!");
		   //AfxMessageBox(_T("您要讀取的文件存在!"));
		   this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
		   SetDoc(strTemp);
		}
		else
		{
            //AfxMessageBox(_T("您要文件在當前目錄中不存在!"));
			strTemp =_T("您要文件在當前目錄中不存在!");
			this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
			SetDoc(strTemp);
			return;
		}		
		if (OnRead())
		{
           //AfxMessageBox(_T("數據庫準備就序!!"));
		   strTemp =_T("數據庫準備就序!");
		   this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
		   SetDoc(strTemp);
		}else
		{
			//AfxMessageBox(_T("數據庫操作错误!!"));
			strTemp =_T("數據庫操作错误!");
			this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
			SetDoc(strTemp);
			return;
		}
		if (ReadCSV())
		{
			
			strTemp =_T("文件導入數據庫成功!");
            CString abc;
			abc.Format(_T("%s 共写入了%d条记录。"),strTemp,m_nTimes);
			AfxMessageBox(abc);
			this->SendMessage( WM_STATUSMSG, (WPARAM)&abc );
			SetDoc(abc);
		}
	    else
		{
			strTemp =_T("文件導入數據庫失敗!");
			AfxMessageBox(_T("文件導入數據庫失敗!"));
			this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
			SetDoc(strTemp);
			return;
		}

	 }
}
BOOL CMainFrame::OnRead() 
{
	// TODO: Add your control notification handler code here
	if(m_pConnection==NULL)
	{	CString strConnect=	_T("m_pConnection 對象為空");
		this->SendMessage( WM_STATUSMSG, (WPARAM)&strConnect );	
		SetDoc(strConnect);
		return  FALSE;
	}	
	m_pRecordset.CreateInstance(_T("ADODB.Recordset"));
	try
	{   
		CString  strDel=_T("DELETE  FROM AutotollBPAccount  "); 
		_variant_t varSQLDel(strDel);	
		m_pRecordset->Open(varSQLDel,_variant_t((IDispatch *)m_pConnection,true),adOpenDynamic,adLockPessimistic,adCmdText);									
		CString strSQL=_T("SELECT * FROM AutotollBPAccount  "); 
		_variant_t varSQL(strSQL);	
		m_pRecordset->Open(varSQL,_variant_t((IDispatch *)m_pConnection,true),adOpenDynamic,adLockPessimistic,adCmdText);									
	}
	catch(_com_error *e)
	{ 
		CString errormessage;
		errormessage.Format(_T("數據庫操作失敗,原因%s"),e->ErrorMessage());
		this->SendMessage( WM_STATUSMSG, (WPARAM)&errormessage );		
		SetDoc(errormessage);
		return  FALSE;
	}
	m_nTimes=0;
	return    TRUE;
	   

}
BOOL   CMainFrame::ReadCSV()
{   

	int nNumber=1;
	CString  strString;
	BOOL  bReturn=FALSE;
	CString  strDataTarget;
	mFile.Open( path_search, CFile::modeRead, &mExcept);
	bReturn=mFile.ReadString(strString);
    strDataTarget=DataOperate(strString );
	while(bReturn)
	{   
		Sleep(1);
		if (bReadHeader==TRUE)
		{   
		     OnWriterow(strDataTarget);
		}	
		if (bReadHeader==FALSE)
		{    
			
			if (nNumber!=1)
			{
				OnWriterow(strDataTarget);
			}
		}
		bReturn=FALSE;
		bReturn=mFile.ReadString(strString);
		if (bReturn)
		{
			strDataTarget=DataOperate(strString );
		}		
		nNumber++;
	}
	mFile.Close();  
	return  TRUE;
}

CString   CMainFrame::DataOperate(CString strData )
{  
	int  nPosdot=0;
	int  nPosbeg=0;
	int  nPosEnd=0;
	int  nJudge=-1;
	m_nTimes++;
	int nLength=strData.GetLength();
	nJudge=strData.Find(',',nLength);
	if (nJudge=-1)
	{
		strData+='*';
	}
	nPosdot=strData.Find(',',nPosdot);
	
	do
	{    			
		nPosbeg=strData.Find('"',nPosdot-1);
		nPosEnd=strData.Find('"',nPosdot);		
		if (nPosbeg == (nPosEnd-2)  )
		{
			strData.SetAt(nPosdot,'*');
		}
		else
		{
			nPosdot=strData.Find(',',nPosdot+1);
		}
		nPosdot=strData.Find(',',nPosdot);
		
		
	}while(nPosdot>0);
	strData.Remove('"');
	return  strData;
}
void CMainFrame::OnWriterow(CString  strRowData) 
{  	
	int    RecordCount=0;
	try
	{   
		int FieldNum=m_pRecordset->GetFields()->Count;	
		while(!m_pRecordset->adoEOF)	
		{
			m_pRecordset->MoveNext();///移到下一条记录
			RecordCount++;
		}
		if(!m_pRecordset->Supports(adAddNew))
		{
			CString  errormessage;
            errormessage="數據庫不允許添加數據!";
			this->SendMessage( WM_STATUSMSG, (WPARAM)&errormessage );
			SetDoc(errormessage);
			//AfxMessageBox(_T("error"));
		}
		
		CString   strData=strRowData;
		m_pRecordset->AddNew();
		int  iPos=0;
		CString  strNowData=_T("");
		CString  strdd[100];
		for(int i=0;i<FieldNum;i++) 
		{   
			CString  strNowData;
			iPos=strData.Find(_T("*"));      
			strNowData=strData.Left(iPos);
			strData=strData.Right(strData.GetLength()-iPos-1);
			if (i==0)
			{
               	strdd[1]=strNowData;
			}else
				if (i==1)
				{
					strdd[0]=strNowData;
				}
				else
				{
					strdd[i]=strNowData;
				}				
		}
		for(int j=0;j<FieldNum;j++) 
		{
			m_pRecordset->PutCollect(_variant_t((long)j),_variant_t(strdd[j]));
           
		}
		
		m_pRecordset->Update();	
	}
	catch(_com_error *e)
	{
		//AfxMessageBox(e->ErrorMessage());
		CString errormessage;
		errormessage.Format(_T("數據添加失敗,原因%s"),e->ErrorMessage());
		this->SendMessage( WM_STATUSMSG, (WPARAM)&errormessage );
		SetDoc(errormessage);
	}
	
}

void CMainFrame::OnUpdateConnectSql(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(!bConnected);
}

void CMainFrame::OnUpdateCloseSql(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable( bConnected);
}

void CMainFrame::OnUpdateReadfile(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
 	pCmdUI->Enable( bConnected);
}
void CMainFrame::OnUpdateExport(CCmdUI* pCmdUI)	 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable( bConnected);
	
}
void CMainFrame::OnButtonSearch() 
{   
	  // 字符串 szFilename 表示要查找的文件名    
	  wcscpy(szFilename,m_FileName);
	  wcscpy(szDir,m_LocalDir); 
	  _wchdir(szDir); // 进入要查找的路径(也可为某一具体的目录) 	
	  // 查找文件, 如果查到则显示文件的路径全 
	  Search_Directory(szFilename);	  		 
}
void CMainFrame::Search_Directory( TCHAR *  szFilename)
{
	
	long handle; 
	struct _wfinddata_t filestruct; 
	 			
	//表示查找到的路径结果 		
    // 开始查找工作, 找到当前目录下的第一个实体(文件或子目录), 		
	// "*"表示查找任何的文件或子目录, filestruct为查找结果   		
	handle = _wfindfirst(_T("*"), &filestruct); 
	
	// 如果handle为-1, 表示当前目录为空, 则结束查找而返回 			
	if((handle==-1))
		return  ;
	
	// 检查找到的第一个实体是否是一个目录(filestruct.name为其名称) 				   		
	if(::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_DIRECTORY)		
	{ 		
		// 如果是目录, 则进入该目录并递归调用函数Search_Dirctory进行查找, 			
		// 注意: 如果目录名的首字符为′.′(即为″.″或″..″), 则不用进行查找    			
		/*if( filestruct.name[0] != '.' ) 
		{ 
			_wchdir(filestruct.name); 
			Search_Directory(szFilename); 
			
			// 查找完毕之后, 返回上一级目录    		
			_wchdir(_T(".."));
		}*/
	} 
	else // 如果第一个实体不是目录, 则检查是否是要查找的文件 
	{ 
		// stricmp对两字符串进行小写形式的对比, 返回为0表示完全一致 
		if( !wcsicmp(filestruct.name, szFilename) ) 
		{ 			
			// 先获得当前工作目录的全路径 				
			 _wgetcwd(path_search,_MAX_PATH); 			
			// 再获得文件的完整的路径名(包含文件的名称) 
			wcscat(path_search,_T("\\")); 
			wcscat(path_search,filestruct.name);
			bFileFind=  TRUE;
			return;
		} 
	} 
	// 继续对当前目录中的下一个子目录或文件进行与上面同样的查找 
	while(!(_wfindnext(handle,&filestruct))) 
	{ 
		if( ::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_DIRECTORY )
		{ 
			/*if(*filestruct.name != '.') 
			{
				_wchdir(filestruct.name); 
				Search_Directory(szFilename); 
				_wchdir(_T("..")); 
			} */
		} 
		else 
		{ 
			if(!wcsicmp(filestruct.name,szFilename)) 
			{ 
				_wgetcwd(path_search,_MAX_PATH); 
				wcscat(path_search,_T("\\")); 
				wcscat(path_search,filestruct.name);
				bFileFind=  TRUE;
				return;
			} 
		} 
	} 
	_findclose(handle); 	
}

BOOL  CMainFrame::AutoRun( )
{
	HKEY     RegKey;   
	CString     sPath;   
	GetModuleFileName(NULL,sPath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);   
	sPath.ReleaseBuffer();   
	int     nPos;   
	nPos=sPath.ReverseFind('\\');   
	sPath=sPath.Left(nPos);   
	CString     lpszFile=sPath+_T("\\AccountInput.exe");//这里加上你要查找的执行文件名称   
	CFileFind     fFind;   
	BOOL     bSuccess;   
	bSuccess=fFind.FindFile(lpszFile);   
	fFind.Close();   
	if(bSuccess)   
	{   
		CString     fullName;   
		fullName=lpszFile;   
		RegKey=NULL;   
		RegOpenKey(HKEY_LOCAL_MACHINE,_T("Software\\Microsoft\\Windows\\CurrentVersion\\Run"),&RegKey);   
		RegSetValueEx(RegKey,_T("AccountInput"),0,REG_SZ,(const  unsigned  char*)(LPCTSTR)fullName,fullName.GetLength());//这里加上你需要在注册表中注册的内容   
		//this->UpdateData(FALSE);   
	}   
	else   
	{   
		// theApp.SetMainSkin();   
		//::AfxMessageBox("");
		CString errormessage;
		errormessage.Format(_T("没找到执行程序,自动运行失败%s"));
		this->SendMessage( WM_STATUSMSG, (WPARAM)&errormessage );
		SetDoc(errormessage);
		return FALSE;
		//exit(0);   
     } 
		return TRUE;
}

BOOL  CMainFrame::CloseAutoRun( )
{ 
	CRegKey  reg;
	//HKEY     RegKey;
	LPCTSTR  lpszKey=_T("Software\\Microsoft\\Windows\\CurrentVersion\\Run\\");
	//reg=NULL;
	LONG  lResult=0;
	lResult=reg.Open(HKEY_LOCAL_MACHINE,lpszKey);
	//RegOpenKey(HKEY_LOCAL_MACHINE,"",&RegKey);
    lResult= reg.DeleteValue(_T("AccountInput"));
	if (ERROR_SUCCESS!=lResult)
	{
		//AfxMessageBox("failed");
	}
    reg.Close();
	return 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -