cpp
复制代码
#pragma warning(disable : 4996)
#include <windows.h>
#include "resource.h"
#include <commdlg.h>
#include "CSqlServer.h"
#include "FileSql.h"
#pragma comment(lib, "Comdlg32.lib")
using namespace std;
DWORD WINAPI WorkThread(LPVOID lpParameter);
CSqlServer* cSql;
FileSql cFilesql;
int g_nCount = 0;
char g_str[16];
HANDLE g_hThread;
//main 按钮功能 开始
bool BConNect(HWND hwndDlg, CSqlServer* cSql);//连接数据库按钮
void BGetAdmin(HWND hwndDlg, CSqlServer* cSql);//获取用户管理员密码
void SetShowId(HWND hwndDlg, BOOL EDT, BOOL BTN);//设置按钮状态
void SetShowIdQs(HWND hwndDlg, BOOL EDT, BOOL BTN);//设置按钮状态
bool BUpdate(HWND hwndDlg);
//main 按钮功能 结束
//EnableWindow(GetDlgItem(hwndDlg, EDT_NAME), EDT);
BOOL CALLBACK DlgProc(HWND hwndDlg,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
{
cFilesql.m_hwndDlg = hwndDlg;
SendDlgItemMessage(hwndDlg, EDIT_SERVERIP, WM_SETTEXT, 0, (LPARAM)".");
SendDlgItemMessage(hwndDlg, EDT_DATEBASE, WM_SETTEXT, 0, (LPARAM)"printerp");
return true;
}
case WM_CLOSE:
{
EndDialog(hwndDlg, 0);
return TRUE;
}
case WM_DESTROY:
{
//MessageBox(NULL, "WM_DESTROY", "TEST", 0);
return true;
}
case WM_COMMAND:
{
WORD wId = LOWORD(wParam);//控件id
WORD wMsg = HIWORD(wParam); //消息id(通告码)
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;
}
//处理一下字符串
// Cstr.clear();
delete cSql;
cSql = nullptr;
MessageBox(hwndDlg, "服务器连接断开成功","DisConnect",MB_OK);
SetShowId(hwndDlg, true, true);
return true;
}
case BTN_GETPATH:
{
OPENFILENAME ofn;
CHAR szFileName[MAX_PATH];
memset(szFileName, 0, 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);
}
//初始化文件类 窗口句柄和文件路径
cFilesql.m_hwndDlg = hwndDlg;
cFilesql.SetFile(szFileName);
return true;
}
case BTN_UPDATE:
{
g_hThread = CreateThread(
NULL, // no security attributes
0, // use default stack size
WorkThread, // thread function
hwndDlg, // argument to thread function
0, // use default creation flags
NULL);
return true;
}
case BTN_GETEDT:
{
if (!cFilesql.Open())
{
MessageBox(hwndDlg,"文件打开失败","测试", MB_ICONEXCLAMATION);
return true;
}
cFilesql.GetUpHeaderSql();
cFilesql.GetUpMiddleSql();
//显示拼成的SQL语句
MyStr cUpdateSql;
cUpdateSql = cUpdateSql + cFilesql.m_sHeaderSql.m_str + cFilesql.m_sMiddleSql.m_str +
cFilesql.m_sTailSql.m_str;
cFilesql.m_sMiddleSql.FreeSpace();
SendDlgItemMessage(hwndDlg, EDT_SHOWSQL, WM_SETTEXT, 0, (LPARAM)cUpdateSql.m_str);
cFilesql.Close();
/*SQLRETURN ret = cSql->Exec(cUpdateSql.m_str);
if (!ret)
{
MessageBox(hwndDlg, "最后执行 失败", "ERROR", MB_ICONEXCLAMATION);
return true;
}*/
//g_nCount++;
//sprintf(g_str, "%d", g_nCount);
//SendDlgItemMessage(hwndDlg, EDIT_COUNT, WM_SETTEXT, 0, (LPARAM)g_str);
释放空间
//memset(g_str, 0, 16);
//cSql->FreeHstmt();
return true;
}
case BTN_STOP:
{
SuspendThread(g_hThread); //挂起线程
EnableWindow(GetDlgItem(hwndDlg, BTN_CONTINIU), TRUE);
EnableWindow(GetDlgItem(hwndDlg, BTN_STOP), FALSE);
return true;
}
case BTN_CONTINIU:
{
ResumeThread(g_hThread); //恢复执行线程
EnableWindow(GetDlgItem(hwndDlg, BTN_CONTINIU), FALSE);
EnableWindow(GetDlgItem(hwndDlg, BTN_STOP), TRUE);
return true;
}
}
}
//测试一下 是否都会再这个地方停
}
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)
{
//执行SQL语句查询
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;
}
//获取列数据 取出的数据存储在类的Sqldata里面
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;";
//SQLCHAR connStr[] = "DRIVER={SQL Server};SERVER=.;DATABASE=printerp;UID=print;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);
}
bool BUpdate(HWND hwndDlg)
{
//按钮变成禁用状态
cFilesql.GetUpHeaderSql();
while (cFilesql.GetUpMiddleSql())
{
//显示拼成的SQL语句
MyStr cUpdateSql;
cUpdateSql = cUpdateSql + cFilesql.m_sHeaderSql.m_str + cFilesql.m_sMiddleSql.m_str +
cFilesql.m_sTailSql.m_str;
SQLRETURN ret = cSql->Exec(cUpdateSql.m_str);
if (!ret)
{
MessageBox(hwndDlg, g_str, "ERROR", MB_ICONEXCLAMATION);
return false;
}
cFilesql.m_sMiddleSql.FreeSpace();//手动释放m_sMiddleSql空间
g_nCount++;
//释放空间
sprintf(g_str, "%d", g_nCount);
SendDlgItemMessage(hwndDlg, EDIT_COUNT, WM_SETTEXT, 0, (LPARAM)g_str);
//释放空间
memset(g_str, 0, 16);
cSql->FreeHstmt();
Sleep(60);
}
return true;
}
DWORD WINAPI WorkThread(LPVOID lpParameter)
{
//取窗口句柄 hwnd
HWND hwndDlg = HWND(lpParameter);
SetShowIdQs(hwndDlg, false, false);
if (!cFilesql.Open())
{
MessageBox(hwndDlg, "文件打开失败", "测试", MB_ICONEXCLAMATION);
return false;
}
//MessageBox(hwndDlg,"多线处理","测试",MB_OK);
if (!BUpdate(hwndDlg))
{
MessageBox(hwndDlg, "处理失败", "测试", MB_OK);
SetShowIdQs(hwndDlg, true, true);
return false;
}
MessageBox(hwndDlg, "全部处理完了", "测试", MB_OK);
SetShowIdQs(hwndDlg, true, true);
return true;
}
void SetShowIdQs(HWND hwndDlg, BOOL EDT, BOOL BTN)
{
int nEditNumber = cFilesql.m_nEditNumber;
for (int i = 0;i < 10;i++)
{
EnableWindow(GetDlgItem(hwndDlg, nEditNumber), EDT);
nEditNumber++;
}
EnableWindow(GetDlgItem(hwndDlg, EDT_UPTABLE), EDT);
EnableWindow(GetDlgItem(hwndDlg, BTN_UPDATE), BTN);
EnableWindow(GetDlgItem(hwndDlg, BTN_CONTINIU), BTN);
}
cpp
复制代码
#include "FileSql.h"
FileSql::FileSql()
{
m_hwndDlg = 0;
memset(m_FilePath,0,1024);
}
FileSql::FileSql(const char* FilePath,HWND handle)
{
m_hwndDlg = handle;
m_file = ifstream(FilePath);
}
bool FileSql::Open()
{
m_file.open(m_FilePath);
return IsOpen();
}
bool FileSql::IsOpen()
{
return m_file.is_open();
}
void FileSql::Close()
{
m_file.close();
}
void FileSql::GetUpHeaderSql()
{
GetDlgItemText(m_hwndDlg, 1025, (LPSTR)m_szBuff.m_str, 256);
m_sHeaderSql = "UPDATE ";
m_sHeaderSql.Add(m_szBuff.m_str).Add(" set ");
}
bool FileSql::GetUpMiddleSql()
{
//取的个数等于
int nNumber = 0;
int nCurrent = 0;
int pos = 0;
int nEditNumber = m_nEditNumber;
//列数
int nCount = 9;
string line;
if (!std::getline(m_file, line))
{
//MessageBox(m_hwndDlg, "getline 取完了", "ERROR", MB_ICONEXCLAMATION);
return false;
}
pos = line.find("\t", pos);
nNumber = pos - nCurrent;
GetDlgItemText(m_hwndDlg, nEditNumber, (LPSTR)m_szBuff.m_str, 256);
// m_sTailSql = m_sTailSql + " where " + szBuff + " = " + "\'" + line.substr(nCurrent, nNumber).c_str() + "\'";
m_sTailSql = " where ";
m_sTailSql.Add(m_szBuff.m_str).Add(" = ").Add("\'").Add(line.substr(nCurrent, nNumber).c_str()).Add("\'");
nEditNumber++;
for (int i = 0; i < nCount; i++)
{
GetDlgItemText(m_hwndDlg, nEditNumber, (LPSTR)m_szBuff.m_str, 256);
if (m_szBuff.m_str[0] == 0)
{
break;
}
// m_sMiddleSql.FreeSpace();
if (i != 0)
{
m_sMiddleSql += ",";
}
nCurrent = pos;
pos = line.find("\t", pos + 1);
nNumber = pos - nCurrent - 1;
//添加中部设置条件
//sMiddleSql += "CategoryId =";//这里取编辑框内容
m_sMiddleSql += m_szBuff.m_str;
m_sMiddleSql.Add(" = ").Add("\'").Add(line.substr(nCurrent + 1, nNumber).c_str()).Add("\'");
if (m_szBuff.Find('('))
{
m_sMiddleSql += ")";
}
nEditNumber++;
}
return true;
}
bool FileSql::GetUpTailSql()
{
GetDlgItemText(m_hwndDlg, 1011, (LPSTR)m_szBuff.m_str, 256);
m_sTailSql = m_sTailSql + " where " + m_szBuff.m_str;
return true;
}
void FileSql::SetFile(const char* FilePath)
{
// m_file = ifstream(FilePath);
int nLen = strlen(FilePath);
nLen++;
memcpy_s(m_FilePath, nLen, FilePath, nLen);
}
FileSql::~FileSql()
{
m_file.close();
}
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;
}
// 设置环境属性以使用ODBC 3.x
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;
}
// 分配语句句柄并执行SQL脚本
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))//执行SQL语句
{
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;
}
//处理m_SQLdata数据,取的长度是SQL定义的长度,是已""为结尾
SetSqlData();
return true;
}
//bool CSqlServer::GetData(int nCols)
//
//{
// SQLRETURN ret;
// bool bTmp = false;
// if (!GetOneRows())
// {
// return bTmp;
// }
// ret = SQLGetData(m_hstmt, nCols, SQL_C_CHAR, m_SQLdata, sizeof(m_SQLdata), &m_dataLen);
// return false;
//}
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';
}
}
}