sql2008工作文档2 main CSqlServer.h CSqlServer.cpp
main
# pragma warning ( disable : 4996 )
# include "CSqlServer.h"
# include <string>
# include "resource.h"
# include <commdlg.h>
# pragma comment ( lib, "Comdlg32.lib" )
using namespace std; CSqlServer* cSql;
bool BConNect ( HWND hwndDlg, CSqlServer* cSql) ;
void BGetAdmin ( HWND hwndDlg, CSqlServer* cSql) ;
void SetShowId ( HWND hwndDlg, BOOL EDT, BOOL BTN) ;
void utf8_to_ascii ( const char * utf8_str, char * buff) ;
BOOL CALLBACK DlgProc ( HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
{ switch ( message) { case WM_INITDIALOG: { SendDlgItemMessage ( hwndDlg, EDIT_SERVERIP, WM_SETTEXT, 0 , ( LPARAM) "192.168.0.166" ) ; SendDlgItemMessage ( hwndDlg, EDT_DATEBASE, WM_SETTEXT, 0 , ( LPARAM) "t10erp" ) ; return true ; } case WM_CLOSE: { EndDialog ( hwndDlg, 0 ) ; return TRUE; } case WM_DESTROY: { return true ; } case WM_COMMAND: { WORD wId = LOWORD ( wParam) ; WORD wMsg = HIWORD ( wParam) ; HWND hCtrl = ( HWND) lParam; switch ( wId) { case BTN_CONNECTSERVER: { cSql = new CSqlServer; BConNect ( hwndDlg, cSql) ; return true ; } case BTN_GETADMIN: { BGetAdmin ( hwndDlg, cSql) ; return true ; } case BTN_DISCONNECTSERVER: { if ( ! cSql-> DisConnect ( ) ) { MessageBox ( hwndDlg, "服务器连接断开失败" , "ERROR DisConnect" , MB_ICONEXCLAMATION) ; return true ; } delete cSql; cSql = nullptr ; MessageBox ( hwndDlg, "服务器连接断开成功" , "DisConnect" , MB_OK) ; SetShowId ( hwndDlg, true , true ) ; return true ; } case BTN_GETPATH: { OPENFILENAME ofn; CHAR szFileName[ MAX_PATH] ; CHAR szFilter[ ] = TEXT ( "All Files (*.*)\0*.*\0\0" ) ; ZeroMemory ( szFileName, MAX_PATH) ; ZeroMemory ( & ofn, sizeof ( ofn) ) ; ofn. lpstrFile = szFileName; ofn. nMaxFile = MAX_PATH; ofn. lpstrFilter = szFilter; ofn. lStructSize = sizeof ( OPENFILENAME) ; ofn. hwndOwner = hwndDlg; if ( GetOpenFileName ( & ofn) ) { SendDlgItemMessage ( hwndDlg, EDIT_FILEPATH, WM_SETTEXT, 0 , ( LPARAM) szFileName) ; } FILE* fp = fopen ( szFileName, "r" ) ; if ( fp == NULL ) { printf ( "文件读取失败" ) ; return 0 ; } int a = 0 ; CHAR szFileName1[ MAX_PATH] ; CHAR szFileName2[ MAX_PATH] ; CHAR szFileName3[ MAX_PATH] ; while ( ! feof ( fp) ) { a = fscanf ( fp, "%s %s %s" , & szFileName1, & szFileName2, & szFileName3) ; char buff[ 256 ] ; if ( a < 0 ) break ; } fclose ( fp) ; } case BTN_UPDATE: { string str2 = "成品" ; string strSql = "select SingleId from cmMaterialCategory where CategoryName = " ; strSql += "\'" + str2 + "\'" ; bool ret = cSql-> Exec ( strSql. c_str ( ) ) ; if ( ! cSql-> GetOneRows ( ) ) { MessageBox ( hwndDlg, "GetOneRows 失败" , "ERROR" , MB_ICONEXCLAMATION) ; return true ; } if ( ! cSql-> GetOneCols ( 1 ) ) { MessageBox ( hwndDlg, "GetOneCols 失败" , "ERROR" , MB_ICONEXCLAMATION) ; return true ; } string str3 = ( char * ) cSql-> m_SQLdata; cSql-> FreeHstmt ( ) ; string str5 = "where ProductNo = '一个机'" ; string str4 = "UPDATE cmProduct set CategoryId = " ; str4 += "\'" + str3 + "\'" + str5; ret = cSql-> Exec ( str4. c_str ( ) ) ; cSql-> FreeHstmt ( ) ; } } } } return FALSE;
} int APIENTRY WinMain ( HINSTANCE g_hInstance, HINSTANCE hInstPrev, PSTR cmdline, int cmdshow)
{ DialogBox ( g_hInstance, MAKEINTRESOURCE ( IDD_DIALOG1) , NULL , DlgProc) ; return 0 ;
} void BGetAdmin ( HWND hwndDlg, CSqlServer* cSql)
{ if ( ! cSql-> Exec ( "select Password from sysuser where UserId = 'admin'" ) ) { MessageBox ( hwndDlg, "Exec 失败" , "ERROR" , MB_ICONEXCLAMATION) ; return ; } if ( ! cSql-> GetOneRows ( ) ) { MessageBox ( hwndDlg, "GetOneRows 失败" , "ERROR" , MB_ICONEXCLAMATION) ; return ; } if ( ! cSql-> GetOneCols ( 1 ) ) { MessageBox ( hwndDlg, "GetOneCols 失败" , "ERROR" , MB_ICONEXCLAMATION) ; return ; } SendDlgItemMessage ( hwndDlg, EDIT_admin, WM_SETTEXT, 0 , ( LPARAM) cSql-> m_SQLdata) ; cSql-> FreeHstmt ( ) ;
} bool BConNect ( HWND hwndDlg, CSqlServer* cSql)
{ string Cstr; Cstr = "DRIVER={SQL Server};SERVER=" ; char szBuff[ 128 ] = { 0 } ; GetDlgItemText ( hwndDlg, EDIT_SERVERIP, szBuff, sizeof ( szBuff) ) ; Cstr += szBuff; Cstr += ";DATABASE=" ; memset ( szBuff, 0 , sizeof ( szBuff) ) ; GetDlgItemText ( hwndDlg, EDT_DATEBASE, szBuff, sizeof ( szBuff) ) ; Cstr += szBuff; Cstr += ";UID=print;" ; Cstr += "PWD=print;" ; if ( ! cSql-> ConnectSql ( ( char * ) Cstr. data ( ) ) ) { MessageBox ( hwndDlg, "连接失败" , "测试" , MB_ICONEXCLAMATION) ; return false ; } MessageBox ( hwndDlg, "连接成功" , "测试" , MB_OK) ; SetShowId ( hwndDlg, false , false ) ; return true ;
} void SetShowId ( HWND hwndDlg, BOOL EDT, BOOL BTN)
{ EnableWindow ( GetDlgItem ( hwndDlg, EDIT_SERVERIP) , EDT) ; EnableWindow ( GetDlgItem ( hwndDlg, EDT_DATEBASE) , EDT) ; EnableWindow ( GetDlgItem ( hwndDlg, BTN_CONNECTSERVER) , BTN) ;
} void utf8_to_ascii ( const char * utf8_str, char * buff)
{ int nCount = 0 ; int nLen = strlen ( utf8_str) ; for ( int i = 0 ; i < nLen; i++ ) { if ( utf8_str[ i] < 0x80 ) { buff[ nCount] = utf8_str[ i] ; nCount++ ; } }
}
CSqlServer.h
# pragma once
# include <windows.h>
# include <stdio.h>
# include <sql.h>
# include <sqlext.h>
class CSqlServer
{ public :
CSqlServer ( ) ;
bool InitSql ( ) ; bool ConnectSql ( char * str) ; int GetCols ( ) ; int GetRows ( const char * cTableName) ; bool Exec ( const char * cSql) ; bool FreeHstmt ( ) ; bool GetOneRows ( ) ; bool GetOneCols ( int nCols) ; bool GetData ( int nCols) ; bool DisConnect ( ) ; ~ CSqlServer ( ) ; private :
void SetSqlData ( ) ; public : SQLCHAR m_SQLdata[ 256 ] ; SQLINTEGER m_dataLen;
private : bool m_fDis; SQLHENV m_henv; SQLHDBC m_hdbc; SQLHSTMT m_hstmt;
} ;
CSqlServer.cpp
# include "CSqlServer.h" CSqlServer :: CSqlServer ( )
{ memset ( m_SQLdata, 0 , 256 ) ; InitSql ( ) ; } bool CSqlServer :: InitSql ( )
{ SQLRETURN ret = SQLAllocHandle ( SQL_HANDLE_ENV, SQL_NULL_HANDLE, & m_henv) ; if ( ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) { return false ; } ret = SQLSetEnvAttr ( m_henv, SQL_ATTR_ODBC_VERSION, ( SQLPOINTER) SQL_OV_ODBC3, 0 ) ; if ( ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) { return false ; } ret = SQLAllocHandle ( SQL_HANDLE_DBC, m_henv, & m_hdbc) ; if ( ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) { return false ; } return true ;
} bool CSqlServer :: ConnectSql ( char * str)
{ SQLRETURN ret = SQLDriverConnect ( m_hdbc, NULL , ( SQLCHAR* ) str, SQL_NTS, NULL , 0 , NULL , SQL_DRIVER_NOPROMPT) ; if ( ret != SQL_SUCCESS_WITH_INFO && ret != SQL_SUCCESS) { return false ; } SQLAllocHandle ( SQL_HANDLE_STMT, m_hdbc, & m_hstmt) ; if ( ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) { return false ; } m_fDis = true ; return true ;
} int CSqlServer :: GetCols ( )
{ SQLSMALLINT numCols; SQLRETURN ret = SQLNumResultCols ( m_hstmt, & numCols) ; if ( ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) { return 0 ; } return numCols; } int CSqlServer :: GetRows ( const char * cTableName)
{ char buff[ 256 ] = { 0 } ; sprintf_s ( buff, 256 , "%s%s" , "SELECT COUNT(*) FROM " , cTableName) ; if ( ! Exec ( buff) ) { return - 1 ; } SQLRETURN ret = SQLFetch ( m_hstmt) ; if ( ret != SQL_SUCCESS) { return - 1 ; } ret = SQLGetData ( m_hstmt, 1 , SQL_C_CHAR, m_SQLdata, sizeof ( m_SQLdata) , & m_dataLen) ; if ( ret != SQL_SUCCESS) { return - 1 ; } return true ; } bool CSqlServer :: Exec ( const char * cSql)
{ SQLRETURN ret = SQLExecDirect ( m_hstmt, ( SQLCHAR* ) cSql, SQL_NTS) ; if ( ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) { return false ; } return true ;
} bool CSqlServer :: FreeHstmt ( )
{ SQLRETURN ret = SQLFreeStmt ( m_hstmt, SQL_CLOSE) ; if ( ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) { return false ; } return true ;
} bool CSqlServer :: GetOneRows ( )
{ SQLRETURN ret = SQLFetch ( m_hstmt) ; if ( ret != SQL_SUCCESS) { return false ; } return true ;
} bool CSqlServer :: GetOneCols ( int nCols)
{ SQLRETURN ret = SQLGetData ( m_hstmt, nCols, SQL_C_CHAR, m_SQLdata, sizeof ( m_SQLdata) , & m_dataLen) ; if ( ret != SQL_SUCCESS) { return false ; } SetSqlData ( ) ; return true ;
}
bool CSqlServer :: GetData ( int nCols)
{ if ( ! GetOneCols ( nCols) ) { return false ; } return true ;
} bool CSqlServer :: DisConnect ( )
{ SQLRETURN ret = SQLFreeHandle ( SQL_HANDLE_STMT, m_hstmt) ; if ( ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) { return false ; } ret = SQLDisconnect ( m_hdbc) ; if ( ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) { return false ; } ret = SQLFreeHandle ( SQL_HANDLE_DBC, m_hdbc) ; if ( ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) { return false ; } ret = SQLFreeHandle ( SQL_HANDLE_ENV, m_henv) ; if ( ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) { return false ; } m_fDis = false ; return true ;
} CSqlServer :: ~ CSqlServer ( )
{ if ( m_fDis) { DisConnect ( ) ; } } void CSqlServer :: SetSqlData ( )
{ for ( int i = 0 ; i < m_dataLen; i++ ) { if ( m_SQLdata[ i] == ' ' ) { m_SQLdata[ i] = '\0' ; } } }