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

📄 sybtooldlg.cpp

📁 一个执行SYBASE SQL语言的工具示例源代码 功能特色 1 语法颜色显示 2 执行DDL语句 3 在执行SQL Select 语句时使用异步多线程处理方式
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	char srv[32] ,use[32],pwd[32];
	m_srv.GetWindowText(srv,32);
	m_use.GetWindowText(use,32);
	m_pwd.GetWindowText(pwd,32);
 
	AfxGetApp()->WriteProfileString("SYBTOOL", "srv", srv);
	AfxGetApp()->WriteProfileString("SYBTOOL", "use", use);
	AfxGetApp()->WriteProfileString("SYBTOOL", "pwd", pwd);
 

}

void CSYBToolDlg::OnFileopenSql() 
{
	if (!DoPromptFileName(m_strSQlName, AFX_IDS_OPENFILE,
	  OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, TRUE))
		return;

	ASSERT(this);

	CFile* pInputFile = NULL;
	try
	{
		pInputFile = new CFile(m_strSQlName, CFile::modeRead | CFile::shareDenyNone);
		
		if (pInputFile ->GetLength() >1048576)  //1M
		{
			AfxMessageBox("文件太大!");
			pInputFile->Close();
			delete pInputFile;
			return ;
		}

		EDITSTREAM strm;
		strm.dwCookie = (DWORD) pInputFile;
		strm.pfnCallback = EditStreamCallbackReadFromFile;
		
		long lResult = m_wndScriptEdit.StreamIn(SF_TEXT, strm);
		
		pInputFile->Close();
		delete pInputFile;
		//修改格式
		CHARFORMAT cfm;
		m_wndScriptEdit.SetSel(0, m_wndScriptEdit.GetTextLength( ) );
		cfm.cbSize = sizeof(cfm);
		m_wndScriptEdit.GetSelectionCharFormat(cfm);
		cfm.crTextColor = RGB(0,0,0);
		CopyMemory(cfm.szFaceName, "Fixedsys",9);
		cfm.dwMask = CFM_COLOR |CFM_FACE	;
		m_wndScriptEdit.SetSelectionCharFormat(cfm);
		m_wndScriptEdit.SetSel(1, 1 );

	}
	catch (CFileException* pEx)
	{
		pEx->Delete();
	}

	m_wndScriptEdit.SetModify( FALSE ); 

}



bool CSYBToolDlg::DoPromptFileName(CString& fileName, UINT nIDSTitle, DWORD lFlags, BOOL bOpenFileDialog)
{
	CFileDialog dlgFile(bOpenFileDialog);

	CString title;
	VERIFY(title.LoadString(nIDSTitle));


	int structsize=0; 
	
	DWORD dwVersion,dwWindowsMajorVersion,dwWindowsMinorVersion; 
	//检测目前的操作系统,GetVersion具体用法详见MSDN 
	dwVersion = GetVersion(); 
	
	dwWindowsMajorVersion=(DWORD)(LOBYTE(LOWORD(dwVersion)));
	dwWindowsMinorVersion=(DWORD)(HIBYTE(LOWORD(dwVersion)));
	
	// 如果运行的操作系统是Windows NT/2000 
	if (dwVersion < 0x80000000)  
		structsize =88;//显示新的文件对话框 
	else 
	//运行的操作系统Windows 95/98  
		structsize =76;//显示老的文件对话框 
 
	dlgFile.m_ofn.lStructSize=structsize; 

	dlgFile.m_ofn.Flags |= lFlags;

	CString strFilter="";
	CString strDefault="SQL";
	// do for all doc template
	BOOL bFirst = TRUE;

	// append the "*.SQL" all files filter
	strFilter =_T("SQL 文件(*.SQL)");
	strFilter += (TCHAR)'\0';   // next string please
	strFilter += _T("*.SQL");
	strFilter += (TCHAR)'\0';   // last string
	dlgFile.m_ofn.nMaxCustFilter++;
	strFilter +=_T("所有文件(*.*)");
	strFilter += (TCHAR)'\0';   // next string please
	strFilter += _T("*.*");
	strFilter += (TCHAR)'\0'; 
	dlgFile.m_ofn.nMaxCustFilter++;


	dlgFile.m_ofn.lpstrFilter = strFilter;
	dlgFile.m_ofn.lpstrTitle = title;
	//dlgFile.m_ofn.lpstrDefExt=strDefault;
	dlgFile.m_ofn.lpstrFile = fileName.GetBuffer(_MAX_PATH);


	int nResult = dlgFile.DoModal();
	fileName.ReleaseBuffer();
	return nResult == IDOK;
}


void CSYBToolDlg::OnSqlcolor() 
{
	if (IsDlgButtonChecked( IDC_SQLCOLOR)==1)
		m_wndScriptEdit.m_bUseColor=false;
	else
		m_wndScriptEdit.m_bUseColor=true;

	
}

static UINT indicators[] =
	{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
	};

int CSYBToolDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CResizableDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	
	if (!m_wndStatusBar.Create(this) ||!m_wndStatusBar.SetIndicators(indicators,sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	return 0;
}

void CSYBToolDlg::OnSize(UINT nType, int cx, int cy) 
{
	
   	CRect r;
    GetClientRect(&r);
	r.top =r.bottom-20;
    m_wndStatusBar.MoveWindow(&r);

    UINT nID,nStyle;
    int cxWidth;
    m_wndStatusBar.GetPaneInfo(0,nID,nStyle,cxWidth);
    m_wndStatusBar.SetPaneInfo(0,nID,SBPS_STRETCH,cxWidth);
	CResizableDialog::OnSize(nType, cx, cy);

}



void CSYBToolDlg::OnCancelMode() 
{
	CResizableDialog::OnCancelMode();
	

	
}

void CSYBToolDlg::OnCaptureChanged(CWnd *pWnd) 
{
	// TODO: Add your message handler code here
	
	CResizableDialog::OnCaptureChanged(pWnd);
}

void CSYBToolDlg::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	SHORT status;
	CResizableDialog::OnChar(nChar, nRepCnt, nFlags);
	switch (nChar)
	{
	case VK_CAPITAL:
			status=GetKeyState(nChar);
			if (status & 0X0F) 
				m_wndStatusBar.SetPaneText(3,"小写");
			else
				m_wndStatusBar.SetPaneText(3,"大写");
			break;

	}

}

int FindCommitChar( CTWScriptEdit &ScriptEdit,int start,LPSTR CommitChar)
{
	CHARRANGE chrg;
	FINDTEXTEX find;
	
	chrg.cpMin=start;
	find.chrg =chrg;
	chrg.cpMax= ScriptEdit.GetTextLength();
	find.lpstrText="go";
	return ScriptEdit.FindText(FR_WHOLEWORD ,&find);

}

int ThreadExecSql ( LPVOID parm )
{
	char *pbuff;
	int  nBuffSize=0;
	int start=0, end;

	CSYBToolDlg *pObj;
	
	pObj = (CSYBToolDlg *)(parm);
	if (! pObj) return 1;
	pObj->GetDlgItem(IDCEXECSQL)->EnableWindow( false);

	CTWScriptEdit &ScriptEdit =(pObj->m_wndScriptEdit);

	pObj->m_wndStatusBar.SetPaneText(0,"正在执行...",TRUE);
	pObj->GetDlgItem(IDCEXECSQL)->EnableWindow( false );

	
	nBuffSize=10240;
	pbuff =new char[nBuffSize];
 	while (1)
	{
	
		end =FindCommitChar(ScriptEdit, start, "go");
		if (end ==-1 ) break;
		
		if (nBuffSize < ( end -start +1) )
		{
				nBuffSize=end -start +1;
				delete []pbuff;
				pbuff=new char[nBuffSize];
			}
			ScriptEdit.SetSel(start,end);
			if (pbuff){
		
				ScriptEdit.GetSelText( pbuff );
				if (pObj->ExecureSql( pbuff)==true)
				{
					::WaitForSingleObject(	pObj->hEventExeSQL, -1 );
				}
				Sleep(200);
			}
			start=end +3;
		}
	
  	pObj->m_wndStatusBar.SetPaneText(0,"执行结束!" ,TRUE);
	pObj->GetDlgItem(IDCEXECSQL)->EnableWindow( true );
	if (pbuff) delete []pbuff;
	return 1;
}

 

void CSYBToolDlg::OnFileSaveSql() 
{
	if (!DoPromptFileName(m_strSQlName, AFX_IDS_SAVEFILE,
	  OFN_HIDEREADONLY , FALSE))
		return; 

	ASSERT(this);

	CFile* pOutputFile = NULL;
	try
	{
		pOutputFile = new CFile(m_strSQlName, CFile::modeCreate |CFile::modeWrite | CFile::shareDenyNone);
		
		EDITSTREAM strm;
		strm.dwCookie = (DWORD) pOutputFile;
		strm.pfnCallback = EditStreamCallbackWriteFromFile;
		
		long lResult = m_wndScriptEdit.StreamOut(SF_TEXT, strm);
		
		pOutputFile->Close();
		delete pOutputFile;
	}
	catch (CFileException* pEx)
	{
		pEx->Delete();
	}

	m_wndScriptEdit.SetModify( FALSE ); 
}

DWORD CALLBACK  
CSYBToolDlg::EditStreamCallbackReadFromFile
(															
 DWORD dwCookie,
 LPBYTE pbBuff,
 LONG cb,
 LONG *pcb
 )
{
	CFile* pFile = (CFile*) dwCookie;
	ASSERT_KINDOF(CFile, pFile);
	*pcb = pFile->Read(pbBuff, cb);
	return 0;
}

DWORD CALLBACK  
CSYBToolDlg::EditStreamCallbackWriteFromFile
(															
 DWORD dwCookie,
 LPBYTE pbBuff,
 LONG cb,
 LONG *pcb
 )
{
	CFile* pFile = (CFile*) dwCookie;
	ASSERT_KINDOF(CFile, pFile);
	pFile->Write(pbBuff, cb);
	*pcb=cb;
	return 0;
}

 

bool CSYBToolDlg::ExecureSql(LPSTR buff)
{
	CString str;
	str =buff;
	//删除
	str.TrimRight();
	int 	pos ;
	while (1){
		//删除开始的注释
		str.TrimLeft();
		if (str.Left(2) !="/*" ){
			//没有发现注释开始字符结束循环
			break;
		}
		pos =str.Find( "*/", 0); //查找注释结束符号
		if (pos == -1)
		{ //全部是注释没有
			return false ;
		}
		else
		{
			str =str.Mid(pos+2);
		}
	}

	return (raSybaseSqlExecute(nSybaseEngine , (LPSTR)(LPCSTR)str) ==1);

}

void CSYBToolDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	CResizableDialog::OnMouseMove(nFlags, point);
}


⌨️ 快捷键说明

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