VC-MFC 登陆界面 + 数据库账号+密码
1 // DlgUser.cpp : 实现文件 2 // 3 4 #include "stdafx.h" 5 #include "Login.h" 6 #include "DlgUser.h" 7 #include "afxdialogex.h" 8 9 10 // CDlgUser 对话框 11 12 IMPLEMENT_DYNAMIC(CDlgUser, CDialogEx) 13 14 15 CDlgUser::CDlgUser(CWnd* pParent /*=NULL*/) 16 : CDialogEx(CDlgUser::IDD, pParent) 17 { 18 19 } 20 21 CDlgUser::~CDlgUser() 22 { 23 24 } 25 26 BOOL CDlgUser::OnInitDialog() 27 { 28 CDialogEx::OnInitDialog(); 29 30 // 将“关于...”菜单项添加到系统菜单中。 31 32 // IDM_ABOUTBOX 必须在系统命令范围内。 33 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 34 ASSERT(IDM_ABOUTBOX < 0xF000); 35 36 CMenu* pSysMenu = GetSystemMenu(FALSE); 37 if (pSysMenu != NULL) 38 { 39 BOOL bNameValid; 40 CString strAboutMenu; 41 bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX); 42 ASSERT(bNameValid); 43 if (!strAboutMenu.IsEmpty()) 44 { 45 pSysMenu->AppendMenu(MF_SEPARATOR); 46 pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 47 } 48 } 49 50 // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动 51 // 执行此操作 52 // SetIcon(m_hIcon, TRUE); // 设置大图标 53 // SetIcon(m_hIcon, FALSE); // 设置小图标 54 55 //ShowWindow(SW_MINIMIZE); 56 57 // TODO: 在此添加额外的初始化代码 58 clear_all(); 59 60 return TRUE; // 除非将焦点设置到控件,否则返回 TRUE 61 } 62 63 64 65 66 void CDlgUser::DoDataExchange(CDataExchange* pDX) 67 { 68 69 70 CDialogEx::DoDataExchange(pDX); 71 DDX_Control(pDX, IDC_COMBO_DAY, m_Combo_Day); 72 DDX_Control(pDX, IDC_COMBO_HOUR, m_Combo_Hour); 73 DDX_Control(pDX, IDC_COMBO_MINUTE, m_Combo_Minute); 74 DDX_Control(pDX, IDC_COMBO_MONTH, m_Combo_Month); 75 DDX_Control(pDX, IDC_COMBO_SECOND, m_Combo_Second); 76 DDX_Control(pDX, IDC_COMBO_YEAR, m_Combo_Year); 77 DDX_Control(pDX, IDC_COMBO_MID, m_Combo_Mid); 78 79 DDX_Control(pDX, IDC_LIST_MIDHERO, m_List_Midhero); 80 DDX_Control(pDX, IDC_BUTTON_CHECKNAME, m_Combo_Checkname); 81 DDX_Control(pDX, IDC_COMBO_CHECKNAMED, m_Combo_Checknamed); 82 DDX_Control(pDX, IDC_COMBO_ZDDEPARTMENT, m_Combo_Zddepartment); 83 DDX_Control(pDX, IDC_COMBO_ZDSKILL, m_Combo_Zdskill); 84 DDX_Control(pDX, IDC_COMBO_ATTRIBUTE, m_Combo_Attribute); 85 DDX_Control(pDX, IDC_BUTTON_MIDSAVE, m_Combo_Midsave); 86 DDX_Control(pDX, IDC_BUTTON_ZDEL, m_Combo_Middel); 87 DDX_Control(pDX, IDC_COMBO_ZDID, m_Combo_Zdid); 88 } 89 90 91 BEGIN_MESSAGE_MAP(CDlgUser, CDialogEx) 92 ON_CBN_SELCHANGE(IDC_COMBO_MID, &CDlgUser::OnCbnSelchangeComboMid) 93 ON_BN_CLICKED(IDC_BUTTON1, &CDlgUser::OnBnClickedButton1) 94 95 ON_BN_CLICKED(IDC_BUTTON_CHECKNAME, &CDlgUser::OnBnClickedButtonCheckname) 96 97 98 99 ON_BN_CLICKED(IDC_BUTTON_CHECKNAME2, &CDlgUser::OnBnClickedButtonCheckname2) 100 ON_BN_CLICKED(IDC_BUTTON_ZDEL, &CDlgUser::OnBnClickedButtonZdel) 101 ON_NOTIFY(NM_DBLCLK, IDC_LIST_MIDHERO, &CDlgUser::OnNMDblclkListMidhero) 102 ON_CBN_SELENDOK(IDC_COMBO_MID, &CDlgUser::OnCbnSelendokComboMid) 103 ON_BN_CLICKED(IDC_BUTTON_MIDSAVE, &CDlgUser::OnBnClickedButtonMidsave) 104 ON_BN_CLICKED(IDC_BUTTON_MIDDEL, &CDlgUser::OnBnClickedButtonMiddel) 105 END_MESSAGE_MAP() 106 107 // CDlgUser 消息处理程序 108 109 110 111 BOOL CDlgUser::PreTranslateMessage(MSG* pMsg) 112 { 113 // TODO: 在此添加专用代码和/或调用基类 114 115 116 if(pMsg->message == WM_KEYDOWN) 117 { 118 switch(pMsg->wParam) 119 { 120 case VK_RETURN: // 屏蔽回车 121 { 122 123 124 125 126 //显示时间 127 CTime t = CTime::GetCurrentTime(); 128 int y=t.GetYear(); 129 int m=t.GetMonth(); 130 int d=t.GetDay(); 131 int hh=t.GetHour(); 132 int mm=t.GetMinute(); 133 int ss=t.GetSecond(); 134 135 year_str.Format("%d",y); 136 137 if(m<10) 138 { 139 month_str.Format("0%d",m); 140 } 141 else 142 { 143 month_str.Format("%d",m); 144 } 145 if(d<10) 146 { 147 day_str.Format("0%d",d); 148 } 149 else 150 { 151 day_str.Format("%d",d); 152 } 153 if(hh<10) 154 { 155 hour_str.Format("0%d",hh); 156 } 157 else 158 { 159 hour_str.Format("%d",hh); 160 } 161 if(mm<10) 162 { 163 minute_str.Format("0%d",mm); 164 } 165 else 166 { 167 minute_str.Format("%d",mm); 168 } 169 if(ss<10) 170 { 171 second_str.Format("0%d",ss); 172 } 173 else 174 { 175 second_str.Format("%d",ss); 176 } 177 178 m_Combo_Year.SetWindowTextA(year_str); 179 m_Combo_Month.SetWindowTextA(month_str); 180 m_Combo_Day.SetWindowTextA(day_str); 181 m_Combo_Hour.SetWindowTextA(hour_str); 182 m_Combo_Minute.SetWindowTextA(minute_str); 183 m_Combo_Second.SetWindowTextA(second_str); 184 185 186 187 } 188 return true; 189 } 190 } 191 192 return CDialogEx::PreTranslateMessage(pMsg); 193 194 } 195 //初始化数据 196 void CDlgUser::clear_all() 197 { 198 199 //显示数据 200 zdskill_str=""; 201 zdattribute_str=""; 202 zddepartment_str=""; 203 204 zdid_str=""; 205 206 m_Combo_Zdskill.SetWindowTextA(zdskill_str); 207 m_Combo_Attribute.SetWindowTextA(zdattribute_str); 208 209 m_Combo_Zdid.SetWindowTextA(zdid_str); 210 211 212 //表格数据 213 m_List_Midhero.ModifyStyle( 0L, LVS_SHOWSELALWAYS ); 214 m_List_Midhero.SetExtendedStyle( LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_CHECKBOXES );//item前生成checkbox控件 215 216 217 218 CRect linshi_rect; 219 int linshi_nWidth; 220 221 m_List_Midhero.GetClientRect(linshi_rect); 222 linshi_nWidth = linshi_rect.Width(); 223 m_List_Midhero.InsertColumn(1,"Id",LVCFMT_CENTER,linshi_nWidth*1/7,0); 224 m_List_Midhero.InsertColumn(2,"Name",LVCFMT_CENTER,linshi_nWidth*1/7,0); 225 m_List_Midhero.InsertColumn(3,"Department",LVCFMT_CENTER,linshi_nWidth*1/7,0); 226 m_List_Midhero.InsertColumn(4,"Skill",LVCFMT_CENTER,linshi_nWidth*1/7,0); 227 m_List_Midhero.InsertColumn(5,"Attribute",LVCFMT_CENTER,linshi_nWidth*3/7,0); 228 229 230 231 //显示时间 232 CTime t = CTime::GetCurrentTime(); 233 int y=t.GetYear(); 234 int m=t.GetMonth(); 235 int d=t.GetDay(); 236 int hh=t.GetHour(); 237 int mm=t.GetMinute(); 238 int ss=t.GetSecond(); 239 240 year_str.Format("%d",y); 241 242 if(m<10) 243 { 244 month_str.Format("0%d",m); 245 } 246 else 247 { 248 month_str.Format("%d",m); 249 } 250 if(d<10) 251 { 252 day_str.Format("0%d",d); 253 } 254 else 255 { 256 day_str.Format("%d",d); 257 } 258 if(hh<10) 259 { 260 hour_str.Format("0%d",hh); 261 } 262 else 263 { 264 hour_str.Format("%d",hh); 265 } 266 if(mm<10) 267 { 268 minute_str.Format("0%d",mm); 269 } 270 else 271 { 272 minute_str.Format("%d",mm); 273 } 274 if(ss<10) 275 { 276 second_str.Format("0%d",ss); 277 } 278 else 279 { 280 second_str.Format("%d",ss); 281 } 282 283 m_Combo_Year.SetWindowTextA(year_str); 284 m_Combo_Month.SetWindowTextA(month_str); 285 m_Combo_Day.SetWindowTextA(day_str); 286 m_Combo_Hour.SetWindowTextA(hour_str); 287 m_Combo_Minute.SetWindowTextA(minute_str); 288 m_Combo_Second.SetWindowTextA(second_str); 289 290 291 m_Combo_Mid.SetCurSel(0); 292 disp_department(); 293 294 } 295 //在下拉框了选择有提示 296 void CDlgUser::OnCbnSelchangeComboMid() 297 { 298 // TODO: 在此添加控件通知处理程序代码 299 // TODO: 在此添加控件通知处理程序代码 300 301 CComboBox* combo1= ( CComboBox*)GetDlgItem(IDC_COMBO_MID); 302 int nIndex = combo1->GetCurSel(); 303 if(nIndex>=0) 304 { 305 306 combo1->GetLBText( nIndex, mid_str); 307 308 MessageBox(mid_str,"选择框提示:",1); 309 310 311 } 312 313 // SetDlgItemText(IDC_EDIT_TIP,"您已经选择了一个部门!!!"); 314 //设置 文本编辑框 IDC_EDIT_PROBLEM1==》"str" 315 316 //((CComboBox*)GetDlgItem(IDC_EDIT_TIP))->GetWindowText(tip_str); 317 //获得 文本编辑框 IDC_EDIT_PROBLEM1==》"str" 318 319 } 320 321 //查询中单区(添加到combox) 322 bool CDlgUser::load_hero(void) 323 { 324 325 //数据库的初始化============================================ 326 327 unsigned short Port = 3306; 328 char *IPAddress = "localhost"; 329 char *UserName = "root"; 330 char *Password = "love00"; //数据库密码 331 char *DBName = "myself_db"; //数据库名称 332 333 MYSQL *ssock; 334 char execsql[1500]; 335 ssock = (MYSQL *)malloc(sizeof(MYSQL)); 336 337 mysql_init(ssock); 338 if(ssock == NULL) 339 { 340 printf_s("ERROR: MySQL ssock init error. "); 341 return FALSE; 342 } 343 344 //连接到指定的数据库 345 346 mysql_options(ssock, MYSQL_SET_CHARSET_NAME, "gb2312"); 347 348 ssock = mysql_real_connect(ssock, IPAddress, UserName, Password, NULL, Port, NULL, 0); 349 if(!ssock) 350 { 351 printf_s("conn fail... "); 352 unsigned int mtint = mysql_errno(ssock); 353 return FALSE; 354 } 355 356 if(mysql_select_db(ssock, DBName) != 0) 357 { 358 printf_s("select db error. "); 359 return FALSE; 360 } 361 362 //数据库初始化========================================================== 363 364 365 CString execsql_str; 366 367 //execsql_str="SELECT name FROM login_tab WHERE department='中单区';"; 368 execsql_str="SELECT name FROM login_tab ;"; 369 strcpy_s(execsql,execsql_str.GetBuffer(execsql_str.GetLength())); 370 if(mysql_real_query(ssock,execsql,strlen(execsql))!=0) 371 { 372 AfxMessageBox("查询 login_tab 数据库中表格 department 出错1!"); 373 } 374 MYSQL_RES *result2; 375 MYSQL_ROW row2; 376 if(!(result2=mysql_use_result(ssock))) 377 { 378 AfxMessageBox("读取 login_tab 数据集 department 失败1!"); 379 } 380 else 381 { 382 CString str2; 383 ((CComboBox*)GetDlgItem(IDC_COMBO_MID))->ResetContent(); 384 while(row2=mysql_fetch_row(result2)) 385 { 386 str2.Format("%s",row2[0]); 387 m_Combo_Mid.AddString(str2); 388 389 } 390 } 391 392 /* 393 if("select attribute from login_tab where name ='亚索';") 394 { 395 CString yasuo_str; 396 yasuo_str="200"; 397 398 m_Edit_Shuxing.SetWindowTextA(yasuo_str); 399 400 } 401 if("select attribute from login_tab where name ='泰隆';") 402 { 403 CString tailong_str; 404 tailong_str="42343"; 405 406 m_Edit_Shuxing.SetWindowTextA(tailong_str); 407 408 } 409 else 410 { 411 AfxMessageBox("查询 login_tab 数据库中表格 department 出错1!"); 412 413 } 414 415 */ 416 mysql_free_result(result2); 417 418 mysql_close(ssock); //关闭数据库 419 420 return true; 421 422 423 424 } 425 426 //查询中单区到表格里 427 bool CDlgUser::load_mid(void) 428 { 429 430 //数据库的初始化============================================ 431 432 unsigned short Port = 3306; 433 char *IPAddress = "localhost"; 434 char *UserName = "root"; 435 char *Password = "love00"; //数据库密码 436 char *DBName = "myself_db"; //数据库名称 437 438 MYSQL *ssock; 439 char execsql[1500]; 440 ssock = (MYSQL *)malloc(sizeof(MYSQL)); 441 442 mysql_init(ssock); 443 if(ssock == NULL) 444 { 445 printf_s("ERROR: MySQL ssock init error. "); 446 return FALSE; 447 } 448 449 //连接到指定的数据库 450 451 mysql_options(ssock, MYSQL_SET_CHARSET_NAME, "gb2312"); 452 453 ssock = mysql_real_connect(ssock, IPAddress, UserName, Password, NULL, Port, NULL, 0); 454 if(!ssock) 455 { 456 printf_s("conn fail... "); 457 unsigned int mtint = mysql_errno(ssock); 458 return FALSE; 459 } 460 461 if(mysql_select_db(ssock, DBName) != 0) 462 { 463 printf_s("select db error. "); 464 return FALSE; 465 } 466 467 //数据库初始化========================================================== 468 469 CString execsql_str; 470 471 //execsql_str="SELECT id,number,name,department FROM login_tab;"; 472 473 //execsql_str="SELECT id,name, department,attribute FROM login_tab WHERE department='中单区';"; 474 execsql_str="SELECT * FROM login_tab;"; 475 476 strcpy_s(execsql,execsql_str.GetBuffer(execsql_str.GetLength())); 477 if(mysql_real_query(ssock,execsql,strlen(execsql))!=0) 478 { 479 AfxMessageBox("查询 login_tab 数据库中表格 name 出错1!"); 480 } 481 MYSQL_RES *result; 482 MYSQL_ROW row; 483 if(!(result=mysql_use_result(ssock))) 484 { 485 AfxMessageBox("读取 employee_tab 数据集 name 失败1!"); 486 } 487 else 488 { 489 /*CString str; 490 m_List_Midhero.DeleteAllItems(); 491 while(row=mysql_fetch_row(result)) 492 { 493 str.Format("%s",row[0]); 494 m_List_Midhero.InsertItem(0,str); 495 496 str.Format("%s",row[1]); 497 m_List_Midhero.SetItemText(0,1,str); 498 499 str.Format("%s",row[2]); 500 m_List_Midhero.SetItemText(0,2,str); 501 str.Format("%s",row[3]); 502 m_List_Midhero.SetItemText(0,3,str); 503 str.Format("%s",row[4]); 504 m_List_Midhero.SetItemText(0,4,str); 505 }*/ 506 507 CString str; 508 unsigned int i=0; 509 m_List_Midhero.DeleteAllItems(); 510 while(row=mysql_fetch_row(result)) 511 { 512 513 /*str.Format("%s",row[1]); 514 m_List_Midhero.InsertItem(i,str); 515 516 str.Format("%s",row[2]); 517 m_List_Midhero.SetItemText(i,1,str); 518 519 str.Format("%s",row[3]); 520 m_List_Midhero.SetItemText(i,2,str); 521 522 str.Format("%s",row[4]); 523 m_List_Midhero.SetItemText(i,3,str);*/ 524 525 str.Format("%s",row[0]); 526 m_List_Midhero.InsertItem(i,str); 527 528 str.Format("%s",row[1]); 529 m_List_Midhero.SetItemText(i,1,str); 530 531 str.Format("%s",row[2]); 532 m_List_Midhero.SetItemText(i,2,str); 533 534 str.Format("%s",row[3]); 535 m_List_Midhero.SetItemText(i,3,str); 536 537 str.Format("%s",row[4]); 538 m_List_Midhero.SetItemText(i,4,str); 539 540 541 i++; 542 } 543 } 544 mysql_free_result(result); 545 546 547 mysql_close(ssock); //关闭数据库 548 549 return true; 550 551 } 552 553 //查询英雄按钮 554 void CDlgUser::OnBnClickedButton1() 555 { 556 // TODO: 在此添加控件通知处理程序代码 557 load_mid(); 558 load_hero(); 559 560 561 } 562 563 //查询是否有名字 564 bool CDlgUser::check_name(void) 565 { 566 567 unsigned short Port = 3306; 568 char *IPAddress = "localhost"; 569 char *UserName = "root"; 570 char *Password = "love00"; //数据库密码 571 char *DBName = "myself_db"; //数据库名称 572 573 MYSQL *ssock; 574 char execsql[1500]; 575 ssock = (MYSQL *)malloc(sizeof(MYSQL)); 576 577 mysql_init(ssock); 578 if(ssock == NULL) 579 { 580 printf_s("ERROR: MySQL ssock init error. "); 581 return FALSE; 582 } 583 584 //连接到指定的数据库 585 586 mysql_options(ssock, MYSQL_SET_CHARSET_NAME, "gb2312"); 587 588 ssock = mysql_real_connect(ssock, IPAddress, UserName, Password, NULL, Port, NULL, 0); 589 if(!ssock) 590 { 591 printf_s("conn fail... "); 592 unsigned int mtint = mysql_errno(ssock); 593 return FALSE; 594 } 595 596 if(mysql_select_db(ssock, DBName) != 0) 597 { 598 printf_s("select db error. "); 599 return FALSE; 600 } 601 //数据库初始化========================================================== 602 603 CString execsql_str5; 604 execsql_str5="SELECT name FROM login_tab WHERE name ='"+name_str+"';"; 605 606 strcpy_s(execsql,execsql_str5.GetBuffer(execsql_str5.GetLength())); 607 608 if(mysql_real_query(ssock,execsql,strlen(execsql))!=0) 609 { 610 AfxMessageBox("查询 login_tab 数据库中 name 是否存在 出错5!"); 611 } 612 MYSQL_RES *result0; 613 MYSQL_ROW row0; 614 if(!(result0=mysql_use_result(ssock))) 615 { 616 AfxMessageBox("检查 login_tab 数据集 name 是否存在 失败5!"); 617 } 618 if(row0=mysql_fetch_row(result0)) 619 { 620 //数据已经存在 621 622 name_flag=true; 623 AfxMessageBox("中单区里有这名英雄!!!"); 624 } 625 else 626 { 627 //数据不存在 628 629 name_flag=false; 630 631 AfxMessageBox("抱歉,没有查到该英雄!!!"); 632 } 633 634 635 mysql_free_result(result0); 636 637 mysql_close(ssock); //关闭数据库 638 639 return true; 640 } 641 642 //查询是否有名字按钮 643 void CDlgUser::OnBnClickedButtonCheckname() 644 { 645 646 // TODO: 在此添加控件通知处理程序代码 647 //查询名字 648 ((CComboBox*)GetDlgItem(IDC_COMBO_CHECKNAMED))->GetWindowText(name_str); 649 check_name(); 650 } 651 652 //英雄新增按钮 653 void CDlgUser::OnBnClickedButtonCheckname2() 654 { 655 // TODO: 在此添加控件通知处理程序代码 656 657 ((CComboBox*)GetDlgItem(IDC_COMBO_CHECKNAMED))->GetWindowText(addname_str); 658 659 if(addname_str!="") 660 { 661 add_name(); 662 663 addname_str=""; 664 m_Combo_Checknamed.SetWindowTextA(addname_str); 665 666 load_mid(); 667 load_hero(); 668 669 } 670 else 671 { 672 AfxMessageBox("亲爱的召唤师:英雄名称不可以为空,无法新增!!"); 673 } 674 675 } 676 677 //添加名字 678 bool CDlgUser::add_name(void) 679 { 680 unsigned short Port = 3306; 681 char *IPAddress = "localhost"; 682 char *UserName = "root"; 683 char *Password = "love00"; //数据库密码 684 char *DBName = "myself_db"; //数据库名称 685 686 MYSQL *ssock; 687 char execsql[1500]; 688 ssock = (MYSQL *)malloc(sizeof(MYSQL)); 689 690 mysql_init(ssock); 691 if(ssock == NULL) 692 { 693 printf_s("ERROR: MySQL ssock init error. "); 694 return FALSE; 695 } 696 697 //连接到指定的数据库 698 699 mysql_options(ssock, MYSQL_SET_CHARSET_NAME, "gb2312"); 700 701 ssock = mysql_real_connect(ssock, IPAddress, UserName, Password, NULL, Port, NULL, 0); 702 if(!ssock) 703 { 704 printf_s("conn fail... "); 705 unsigned int mtint = mysql_errno(ssock); 706 return FALSE; 707 } 708 709 if(mysql_select_db(ssock, DBName) != 0) 710 { 711 printf_s("select db error. "); 712 return FALSE; 713 } 714 715 //数据库初始化========================================================== 716 bool name_flag; 717 718 CString execsql_str; 719 execsql_str="SELECT name FROM login_tab WHERE name='"+addname_str+"';"; 720 strcpy_s(execsql,execsql_str.GetBuffer(execsql_str.GetLength())); 721 if(mysql_real_query(ssock,execsql,strlen(execsql))!=0) 722 { 723 AfxMessageBox("查询 login_tab 数据库中 内容 是否存在 出错"); 724 } 725 MYSQL_RES *result0; 726 MYSQL_ROW row0; 727 if(!(result0=mysql_use_result(ssock))) 728 { 729 AfxMessageBox("检查 login_tab 数据集 是否存在 失败!"); 730 } 731 if(row0=mysql_fetch_row(result0)) 732 { 733 //数据已经存在 734 name_flag=true; 735 } 736 else 737 { 738 //数据不存在 739 name_flag=false; 740 } 741 mysql_free_result(result0); 742 743 if(!name_flag) 744 { 745 execsql_str="INSERT INTO login_tab (name)VALUE('"+addname_str+"');"; 746 strcpy_s(execsql,execsql_str.GetBuffer(execsql_str.GetLength())); 747 if(mysql_real_query(ssock,execsql,strlen(execsql))!=0) 748 { 749 AfxMessageBox("新增 login_tab 数据库中 name 内容出错!"); 750 } 751 } 752 else 753 { 754 AfxMessageBox("亲爱的召唤师:这名英雄名称已经存在,不可以新增!!!"); 755 } 756 757 mysql_close(ssock); //关闭数据库 758 759 760 return true; 761 } 762 763 //删除按钮 764 void CDlgUser::OnBnClickedButtonZdel() 765 { 766 // TODO: 在此添加控件通知处理程序代码 767 ((CComboBox*)GetDlgItem(IDC_COMBO_CHECKNAMED))->GetWindowText(addname_str); 768 769 del_name(); 770 771 addname_str=""; 772 m_Combo_Checknamed.SetWindowTextA(addname_str); 773 774 load_mid(); 775 load_hero(); 776 777 } 778 779 //删除名称 780 bool CDlgUser::del_name(void) 781 { 782 unsigned short Port = 3306; 783 char *IPAddress = "localhost"; 784 char *UserName = "root"; 785 char *Password = "love00"; //数据库密码 786 char *DBName = "myself_db"; //数据库名称 787 788 MYSQL *ssock; 789 char execsql[1500]; 790 ssock = (MYSQL *)malloc(sizeof(MYSQL)); 791 792 mysql_init(ssock); 793 if(ssock == NULL) 794 { 795 printf_s("ERROR: MySQL ssock init error. "); 796 return FALSE; 797 } 798 799 //连接到指定的数据库 800 801 mysql_options(ssock, MYSQL_SET_CHARSET_NAME, "gb2312"); 802 803 ssock = mysql_real_connect(ssock, IPAddress, UserName, Password, NULL, Port, NULL, 0); 804 if(!ssock) 805 { 806 printf_s("conn fail... "); 807 unsigned int mtint = mysql_errno(ssock); 808 return FALSE; 809 } 810 811 if(mysql_select_db(ssock, DBName) != 0) 812 { 813 printf_s("select db error. "); 814 return FALSE; 815 } 816 817 //数据库初始化========================================================== 818 CString execsql_str; 819 execsql_str="DELETE FROM login_tab WHERE name='"+addname_str+"';"; 820 strcpy_s(execsql,execsql_str.GetBuffer(execsql_str.GetLength())); 821 if(mysql_real_query(ssock,execsql,strlen(execsql))!=0) 822 { 823 AfxMessageBox("删除 login_tab 数据库中 product_name 内容出错!"); 824 } 825 826 827 mysql_close(ssock); //关闭数据库 828 829 return true; 830 } 831 832 //双击将表格里的数据显示到控件中 833 void CDlgUser::OnNMDblclkListMidhero(NMHDR *pNMHDR, LRESULT *pResult) 834 { 835 LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR); 836 // TODO: 在此添加控件通知处理程序代码 837 int Row=m_List_Midhero.GetSelectionMark(); 838 839 840 841 if(Row>=0) 842 { 843 zdid_str=m_List_Midhero.GetItemText(Row,0); 844 m_Combo_Zdid.SetWindowTextA(zdid_str); 845 846 addname_str=m_List_Midhero.GetItemText(Row,1); 847 m_Combo_Checknamed.SetWindowTextA(addname_str); 848 849 zddepartment_str=m_List_Midhero.GetItemText(Row,2); 850 m_Combo_Zddepartment.SetWindowTextA(zddepartment_str); 851 852 853 zdskill_str=m_List_Midhero.GetItemText(Row,3); 854 m_Combo_Zdskill.SetWindowTextA(zdskill_str); 855 856 zdattribute_str=m_List_Midhero.GetItemText(Row,4); 857 m_Combo_Attribute.SetWindowTextA(zdattribute_str); 858 859 860 *pResult = 0; 861 862 863 } 864 } 865 866 867 //combox里添加初始化数据 868 bool CDlgUser::disp_department(void) 869 { 870 unsigned short Port = 3306; 871 char *IPAddress = "localhost"; 872 char *UserName = "root"; 873 char *Password = "love00"; //数据库密码 874 char *DBName = "myself_db"; //数据库名称 875 876 MYSQL *ssock; 877 char execsql[1500]; 878 ssock = (MYSQL *)malloc(sizeof(MYSQL)); 879 880 mysql_init(ssock); 881 if(ssock == NULL) 882 { 883 printf_s("ERROR: MySQL ssock init error. "); 884 return FALSE; 885 } 886 887 //连接到指定的数据库 888 889 mysql_options(ssock, MYSQL_SET_CHARSET_NAME, "gb2312"); 890 891 ssock = mysql_real_connect(ssock, IPAddress, UserName, Password, NULL, Port, NULL, 0); 892 if(!ssock) 893 { 894 printf_s("conn fail... "); 895 unsigned int mtint = mysql_errno(ssock); 896 return FALSE; 897 } 898 899 if(mysql_select_db(ssock, DBName) != 0) 900 { 901 printf_s("select db error. "); 902 return FALSE; 903 } 904 905 CString execsql_str; 906 //execsql_str="SELECT DISTINCT xu_name FROM kc_xu_tab WHERE type='日期(日)序列';"; 907 //execsql_str="SELECT department FROM login_tab ;"; 908 execsql_str="SELECT DISTINCT department FROM login_tab WHERE department='中单区';"; 909 strcpy_s(execsql,execsql_str.GetBuffer(execsql_str.GetLength())); 910 if(mysql_real_query(ssock,execsql,strlen(execsql))!=0) 911 { 912 AfxMessageBox("查询 login_tab 数据库中 department 内容出错!"); 913 } 914 915 MYSQL_RES *result; 916 MYSQL_ROW row; 917 if(!(result=mysql_use_result(ssock))) 918 { 919 AfxMessageBox("读取 login_tab 中 department 失败!"); 920 return false; 921 } 922 else 923 { 924 CString str; 925 ((CComboBox*)GetDlgItem(IDC_COMBO_ZDDEPARTMENT))->ResetContent(); 926 while(row=mysql_fetch_row(result)) 927 { 928 str.Format("%s",row[0]); 929 m_Combo_Zddepartment.AddString(str); 930 } 931 } 932 mysql_free_result(result); 933 934 mysql_close(ssock); //关闭数据库 935 936 return true; 937 } 938 939 940 //得到的值 941 void CDlgUser::get_value(void) 942 { 943 ((CComboBox*)GetDlgItem(IDC_COMBO_ZDID))->GetWindowText(zdid_str); 944 ((CComboBox*)GetDlgItem(IDC_COMBO_ZDSKILL))->GetWindowText(zdskill_str); 945 ((CComboBox*)GetDlgItem(IDC_COMBO_ATTRIBUTE))->GetWindowText(zdattribute_str); 946 //((CComboBox*)GetDlgItem(IDC_COMBO_VERSION))->GetWindowText(version_str); 947 //((CComboBox*)GetDlgItem(IDC_COMBO_CC))->GetWindowText(cc_str); 948 //((CComboBox*)GetDlgItem(IDC_COMBO_RESERVE1))->GetWindowText(reserve1_str); 949 //((CComboBox*)GetDlgItem(IDC_COMBO_RESERVE2))->GetWindowText(reserve2_str); 950 //((CComboBox*)GetDlgItem(IDC_COMBO_RESERVE3))->GetWindowText(reserve3_str); 951 952 CComboBox* combo= ( CComboBox*)GetDlgItem(IDC_COMBO_ZDDEPARTMENT); 953 int nIndex = combo->GetCurSel(); 954 if(nIndex>=0) 955 { 956 combo->GetLBText( nIndex, zddepartment_str); 957 } 958 959 /* 960 CComboBox* combo2= ( CComboBox*)GetDlgItem(IDC_COMBO_COMBINE_SN); 961 int nIndex2 = combo2->GetCurSel(); 962 if(nIndex2>=0) 963 { 964 combo2->GetLBText( nIndex2, combine_sn_str); 965 } 966 967 CComboBox* combo3= ( CComboBox*)GetDlgItem(IDC_COMBO_COMBINE); 968 int nIndex3 = combo3->GetCurSel(); 969 if(nIndex3>=0) 970 { 971 combo3->GetLBText( nIndex3, combine_str); 972 } 973 */ 974 } 975 976 977 //Combo里选择数据会在表格显示 978 void CDlgUser::OnCbnSelendokComboMid() 979 { 980 // TODO: 在此添加控件通知处理程序代码 981 CComboBox* combo= ( CComboBox*)GetDlgItem(IDC_COMBO_MID); 982 int nIndex = combo->GetCurSel(); 983 if(nIndex>=0) 984 { 985 combo->GetLBText( nIndex, addname_str); 986 987 disp_mid_list2(); 988 } 989 } 990 991 //Combo里选择数据会在表格显示(被调用) 992 bool CDlgUser::disp_mid_list2(void) 993 { 994 unsigned short Port = 3306; 995 char *IPAddress = "localhost"; 996 char *UserName = "root"; 997 char *Password = "love00"; //数据库密码 998 char *DBName = "myself_db"; //数据库名称 999 1000 MYSQL *ssock; 1001 char execsql[1500]; 1002 ssock = (MYSQL *)malloc(sizeof(MYSQL)); 1003 1004 mysql_init(ssock); 1005 if(ssock == NULL) 1006 { 1007 printf_s("ERROR: MySQL ssock init error. "); 1008 return FALSE; 1009 } 1010 1011 //连接到指定的数据库 1012 1013 mysql_options(ssock, MYSQL_SET_CHARSET_NAME, "gb2312"); 1014 1015 ssock = mysql_real_connect(ssock, IPAddress, UserName, Password, NULL, Port, NULL, 0); 1016 if(!ssock) 1017 { 1018 printf_s("conn fail... "); 1019 unsigned int mtint = mysql_errno(ssock); 1020 return FALSE; 1021 } 1022 1023 if(mysql_select_db(ssock, DBName) != 0) 1024 { 1025 printf_s("select db error. "); 1026 return FALSE; 1027 } 1028 1029 //初始化数据库 1030 CString execsql_str; 1031 execsql_str="SELECT * FROM login_tab WHERE name='"+addname_str+"';"; 1032 strcpy_s(execsql,execsql_str.GetBuffer(execsql_str.GetLength())); 1033 if(mysql_real_query(ssock,execsql,strlen(execsql))!=0) 1034 { 1035 AfxMessageBox("查询 login_tab 数据库中 内容出错!"); 1036 } 1037 1038 MYSQL_RES *result; 1039 MYSQL_ROW row; 1040 if(!(result=mysql_use_result(ssock))) 1041 { 1042 AfxMessageBox("读取 login_tab 中 内容 失败!"); 1043 return false; 1044 } 1045 else 1046 { 1047 CString str; 1048 unsigned int i=0; 1049 m_List_Midhero.DeleteAllItems(); 1050 while(row=mysql_fetch_row(result)) 1051 { 1052 1053 /*str.Format("%s",row[1]); 1054 m_List_Midhero.InsertItem(i,str); 1055 1056 str.Format("%s",row[2]); 1057 m_List_Midhero.SetItemText(i,1,str); 1058 1059 str.Format("%s",row[3]); 1060 m_List_Midhero.SetItemText(i,2,str); 1061 1062 str.Format("%s",row[4]); 1063 m_List_Midhero.SetItemText(i,3,str);*/ 1064 1065 str.Format("%s",row[0]); 1066 m_List_Midhero.InsertItem(i,str); 1067 1068 str.Format("%s",row[1]); 1069 m_List_Midhero.SetItemText(i,1,str); 1070 1071 str.Format("%s",row[2]); 1072 m_List_Midhero.SetItemText(i,2,str); 1073 1074 str.Format("%s",row[3]); 1075 m_List_Midhero.SetItemText(i,3,str); 1076 1077 str.Format("%s",row[4]); 1078 m_List_Midhero.SetItemText(i,4,str); 1079 1080 1081 1082 1083 i++; 1084 } 1085 } 1086 mysql_free_result(result); 1087 1088 mysql_close(ssock); //关闭数据库 1089 1090 return true; 1091 } 1092 1093 1094 //保存信息 1095 void CDlgUser::OnBnClickedButtonMidsave() 1096 { 1097 // TODO: 在此添加控件通知处理程序代码 1098 1099 CComboBox* combo= ( CComboBox*)GetDlgItem(IDC_COMBO_MID); 1100 int nIndex = combo->GetCurSel(); 1101 if(nIndex>=0) 1102 { 1103 combo->GetLBText( nIndex,addname2_str); 1104 1105 get_value(); 1106 1107 save_mid(); 1108 1109 load_mid(); 1110 } 1111 else 1112 { 1113 MessageBox("请先选择合适的英雄名称再做保存操作!","提示:"); 1114 } 1115 } 1116 1117 1118 //信息保存 1119 bool CDlgUser::save_mid(void) 1120 { 1121 unsigned short Port = 3306; 1122 char *IPAddress = "localhost"; 1123 char *UserName = "root"; 1124 char *Password = "love00"; //数据库密码 1125 char *DBName = "myself_db"; //数据库名称 1126 1127 MYSQL *ssock; 1128 char execsql[1500]; 1129 ssock = (MYSQL *)malloc(sizeof(MYSQL)); 1130 1131 mysql_init(ssock); 1132 if(ssock == NULL) 1133 { 1134 printf_s("ERROR: MySQL ssock init error. "); 1135 return FALSE; 1136 } 1137 1138 //连接到指定的数据库 1139 1140 mysql_options(ssock, MYSQL_SET_CHARSET_NAME, "gb2312"); 1141 1142 ssock = mysql_real_connect(ssock, IPAddress, UserName, Password, NULL, Port, NULL, 0); 1143 if(!ssock) 1144 { 1145 printf_s("conn fail... "); 1146 unsigned int mtint = mysql_errno(ssock); 1147 return FALSE; 1148 } 1149 1150 if(mysql_select_db(ssock, DBName) != 0) 1151 { 1152 printf_s("select db error. "); 1153 return FALSE; 1154 } 1155 1156 //初始化数据库 1157 1158 1159 bool name_flag; 1160 1161 CString execsql_str; 1162 execsql_str="SELECT name FROM login_tab WHERE name='"+addname_str+"';"; 1163 strcpy_s(execsql,execsql_str.GetBuffer(execsql_str.GetLength())); 1164 if(mysql_real_query(ssock,execsql,strlen(execsql))!=0) 1165 { 1166 AfxMessageBox("查询 login_tab 数据库中 内容是否存在 出错"); 1167 } 1168 MYSQL_RES *result0; 1169 MYSQL_ROW row0; 1170 if(!(result0=mysql_use_result(ssock))) 1171 { 1172 AfxMessageBox("检查 login_tab 数据集 是否存在 失败!"); 1173 } 1174 if(row0=mysql_fetch_row(result0)) 1175 { 1176 //数据已经存在 1177 name_flag=true; 1178 } 1179 else 1180 { 1181 //数据不存在 1182 name_flag=false; 1183 } 1184 mysql_free_result(result0); 1185 1186 if(name_flag) 1187 { 1188 //execsql_str="UPDATE login_tab SET skill='"+zdskill_str+"',department='"+zddepartment_str+"',attribute='"+zdattribute_str+"',name='"+addname_str+"' WHERE name='"+addname_str+"';"; 1189 execsql_str="UPDATE login_tab SET id='"+zdid_str+"',skill='"+zdskill_str+"',department='"+zddepartment_str+"',attribute='"+zdattribute_str+"',name='"+addname_str+"' WHERE name='"+addname_str+"';"; 1190 1191 strcpy_s(execsql,execsql_str.GetBuffer(execsql_str.GetLength())); 1192 if(mysql_real_query(ssock,execsql,strlen(execsql))!=0) 1193 { 1194 AfxMessageBox("保存 login_tab 数据库中 内容 出错!"); 1195 } 1196 1197 } 1198 else 1199 { 1200 1201 execsql_str="INSERT INTO login_tab (id,name,department,skill,attribute)VALUE('"+zdid_str+"','"+addname_str+"','"+zddepartment_str+"','"+zdskill_str+"','"+zdattribute_str+"');"; 1202 //execsql_str="INSERT INTO login_tab (name,department,skill,attribute)VALUE('"+addname_str+"','"+zddepartment_str+"','"+zdskill_str+"','"+zdattribute_str+"');"; 1203 1204 strcpy_s(execsql,execsql_str.GetBuffer(execsql_str.GetLength())); 1205 if(mysql_real_query(ssock,execsql,strlen(execsql))!=0) 1206 { 1207 AfxMessageBox("新增 login_tab 数据库中 内容 出错!"); 1208 } 1209 } 1210 1211 mysql_close(ssock); //关闭数据库 1212 1213 1214 return true; 1215 } 1216 1217 //删除整体 1218 void CDlgUser::OnBnClickedButtonMiddel() 1219 { 1220 // TODO: 在此添加控件通知处理程序代码 1221 CComboBox* combo= ( CComboBox*)GetDlgItem(IDC_COMBO_MID); 1222 int nIndex = combo->GetCurSel(); 1223 if(nIndex>=0) 1224 { 1225 combo->GetLBText( nIndex,addname_str); 1226 1227 del_mid(); 1228 load_mid(); 1229 1230 } 1231 } 1232 1233 //删除函数 1234 bool CDlgUser::del_mid(void) 1235 { 1236 unsigned short Port = 3306; 1237 char *IPAddress = "localhost"; 1238 char *UserName = "root"; 1239 char *Password = "love00"; //数据库密码 1240 char *DBName = "myself_db"; //数据库名称 1241 1242 MYSQL *ssock; 1243 char execsql[1500]; 1244 ssock = (MYSQL *)malloc(sizeof(MYSQL)); 1245 1246 mysql_init(ssock); 1247 if(ssock == NULL) 1248 { 1249 printf_s("ERROR: MySQL ssock init error. "); 1250 return FALSE; 1251 } 1252 1253 //连接到指定的数据库 1254 1255 mysql_options(ssock, MYSQL_SET_CHARSET_NAME, "gb2312"); 1256 1257 ssock = mysql_real_connect(ssock, IPAddress, UserName, Password, NULL, Port, NULL, 0); 1258 if(!ssock) 1259 { 1260 printf_s("conn fail... "); 1261 unsigned int mtint = mysql_errno(ssock); 1262 return FALSE; 1263 } 1264 1265 if(mysql_select_db(ssock, DBName) != 0) 1266 { 1267 printf_s("select db error. "); 1268 return FALSE; 1269 } 1270 1271 //初始化数据库 1272 1273 CString execsql_str; 1274 execsql_str="DELETE FROM login_tab WHERE name='"+addname_str+"';"; 1275 strcpy_s(execsql,execsql_str.GetBuffer(execsql_str.GetLength())); 1276 if(mysql_real_query(ssock,execsql,strlen(execsql))!=0) 1277 { 1278 AfxMessageBox("删除 login_tab 数据库中 内容 出错!"); 1279 } 1280 1281 mysql_close(ssock); //关闭数据库 1282 1283 return true; 1284 }
1 // LoginDlg.cpp : 实现文件 2 // 3 4 #include "stdafx.h" 5 #include "Login.h" 6 #include "LoginDlg.h" 7 #include "afxdialogex.h" 8 #include "DlgUser.h" 9 #include "Chose.h" 10 11 #ifdef _DEBUG 12 #define new DEBUG_NEW 13 #endif 14 15 16 // 用于应用程序“关于”菜单项的 CAboutDlg 对话框 17 18 class CAboutDlg : public CDialogEx 19 { 20 public: 21 CAboutDlg(); 22 23 // 对话框数据 24 enum { IDD = IDD_ABOUTBOX }; 25 26 protected: 27 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 28 29 // 实现 30 protected: 31 DECLARE_MESSAGE_MAP() 32 }; 33 34 CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD) 35 { 36 } 37 38 void CAboutDlg::DoDataExchange(CDataExchange* pDX) 39 { 40 CDialogEx::DoDataExchange(pDX); 41 } 42 43 BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx) 44 END_MESSAGE_MAP() 45 46 47 // CLoginDlg 对话框 48 49 50 51 52 CLoginDlg::CLoginDlg(CWnd* pParent /*=NULL*/) 53 : CDialogEx(CLoginDlg::IDD, pParent) 54 { 55 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 56 } 57 58 void CLoginDlg::DoDataExchange(CDataExchange* pDX) 59 { 60 CDialogEx::DoDataExchange(pDX); 61 DDX_Control(pDX, IDC_COMBO_OPRATER_NAME, m_Combo_Oprater_Name); 62 DDX_Control(pDX, IDC_EDIT_PASSWORD, m_Edit_Password); 63 DDX_Control(pDX, IDC_COMBO_OPRATER_NAME, m_Combo_Oprater_Name); 64 } 65 66 BEGIN_MESSAGE_MAP(CLoginDlg, CDialogEx) 67 ON_WM_SYSCOMMAND() 68 ON_WM_PAINT() 69 ON_WM_QUERYDRAGICON() 70 ON_BN_CLICKED(IDC_BUTTON_LOGIN, &CLoginDlg::OnBnClickedButtonLogin) 71 72 ON_BN_CLICKED(IDC_BUTTON_CANCEL, &CLoginDlg::OnBnClickedButtonCancel) 73 END_MESSAGE_MAP() 74 75 76 // CLoginDlg 消息处理程序 77 78 BOOL CLoginDlg::OnInitDialog() 79 { 80 CDialogEx::OnInitDialog(); 81 82 // 将“关于...”菜单项添加到系统菜单中。 83 84 // IDM_ABOUTBOX 必须在系统命令范围内。 85 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 86 ASSERT(IDM_ABOUTBOX < 0xF000); 87 88 CMenu* pSysMenu = GetSystemMenu(FALSE); 89 if (pSysMenu != NULL) 90 { 91 BOOL bNameValid; 92 CString strAboutMenu; 93 bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX); 94 ASSERT(bNameValid); 95 if (!strAboutMenu.IsEmpty()) 96 { 97 pSysMenu->AppendMenu(MF_SEPARATOR); 98 pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 99 } 100 } 101 102 // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动 103 // 执行此操作 104 SetIcon(m_hIcon, TRUE); // 设置大图标 105 SetIcon(m_hIcon, FALSE); // 设置小图标 106 107 //ShowWindow(SW_MINIMIZE); 108 109 // TODO: 在此添加额外的初始化代码 110 clear_all(); 111 112 return TRUE; // 除非将焦点设置到控件,否则返回 TRUE 113 } 114 115 void CLoginDlg::OnSysCommand(UINT nID, LPARAM lParam) 116 { 117 if ((nID & 0xFFF0) == IDM_ABOUTBOX) 118 { 119 CAboutDlg dlgAbout; 120 dlgAbout.DoModal(); 121 } 122 else 123 { 124 CDialogEx::OnSysCommand(nID, lParam); 125 } 126 } 127 128 // 如果向对话框添加最小化按钮,则需要下面的代码 129 // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序, 130 // 这将由框架自动完成。 131 132 void CLoginDlg::OnPaint() 133 { 134 if (IsIconic()) 135 { 136 CPaintDC dc(this); // 用于绘制的设备上下文 137 138 SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0); 139 140 // 使图标在工作区矩形中居中 141 int cxIcon = GetSystemMetrics(SM_CXICON); 142 int cyIcon = GetSystemMetrics(SM_CYICON); 143 CRect rect; 144 GetClientRect(&rect); 145 int x = (rect.Width() - cxIcon + 1) / 2; 146 int y = (rect.Height() - cyIcon + 1) / 2; 147 148 // 绘制图标 149 dc.DrawIcon(x, y, m_hIcon); 150 } 151 else 152 { 153 CDialogEx::OnPaint(); 154 } 155 } 156 157 //当用户拖动最小化窗口时系统调用此函数取得光标 158 //显示。 159 HCURSOR CLoginDlg::OnQueryDragIcon() 160 { 161 return static_cast<HCURSOR>(m_hIcon); 162 } 163 164 void CLoginDlg::clear_all() 165 { 166 disp_oprater_name(); 167 168 } 169 //___________________关于数据库__________________________________ 170 void CLoginDlg::OnBnClickedButtonLogin() 171 { 172 173 174 // TODO: 在此添加控件通知处理程序代码 175 176 check_password(); 177 178 if(name_flag) 179 { 180 //GetDlgItem(IDC_BUTTON_EXPORT)->EnableWindow(TRUE); 181 GetDlgItem(IDC_COMBO_OPRATER_NAME)->EnableWindow(FALSE); 182 AfxMessageBox("恭喜您,您已经成功登录该系统!!!!"); 183 Chose dlg; 184 //CDlgUser dlg; 185 dlg.DoModal(); 186 187 } 188 else 189 { 190 //GetDlgItem(IDC_BUTTON_EXPORT)->EnableWindow(FALSE); 191 192 AfxMessageBox("对不起,您输入密码有误,请您重新输入........."); 193 } 194 } 195 196 197 bool CLoginDlg::check_password(void) 198 { 199 200 //数据库的初始化============================================ 201 202 unsigned short Port = 3306; 203 char *IPAddress = "localhost"; 204 char *UserName = "root"; 205 char *Password = "love00"; //数据库密码 206 char *DBName = "myself_db"; //数据库名称 207 208 MYSQL *ssock; 209 char execsql[1500]; 210 ssock = (MYSQL *)malloc(sizeof(MYSQL)); 211 212 mysql_init(ssock); 213 if(ssock == NULL) 214 { 215 printf_s("ERROR: MySQL ssock init error. "); 216 return FALSE; 217 } 218 219 //连接到指定的数据库 220 221 mysql_options(ssock, MYSQL_SET_CHARSET_NAME, "gb2312"); 222 223 ssock = mysql_real_connect(ssock, IPAddress, UserName, Password, NULL, Port, NULL, 0); 224 if(!ssock) 225 { 226 printf_s("conn fail... "); 227 unsigned int mtint = mysql_errno(ssock); 228 return FALSE; 229 } 230 231 if(mysql_select_db(ssock, DBName) != 0) 232 { 233 printf_s("select db error. "); 234 return FALSE; 235 } 236 //数据库初始化========================================================== 237 238 ((CComboBox*)GetDlgItem(IDC_EDIT_PASSWORD))->GetWindowText(password_str); 239 240 CComboBox* combo1= ( CComboBox*)GetDlgItem(IDC_COMBO_OPRATER_NAME); 241 242 int nIndex1 = combo1->GetCurSel(); 243 if(nIndex1>=0) 244 { 245 combo1->GetLBText( nIndex1, oprater_name_str); 246 } 247 248 CString execsql_str1; 249 250 execsql_str1="SELECT * FROM login_password_tab WHERE department='"+oprater_name_str+"' AND password='"+password_str+"' ;"; 251 strcpy_s(execsql,execsql_str1.GetBuffer(execsql_str1.GetLength())); 252 if(mysql_real_query(ssock,execsql,strlen(execsql))!=0) 253 { 254 AfxMessageBox("查询 login_password_tab 数据库中 内容是否存在 出错!"); 255 } 256 MYSQL_RES *result0; 257 MYSQL_ROW row0; 258 if(!(result0=mysql_use_result(ssock))) 259 { 260 AfxMessageBox("检查 login_password_tab 数据集 是否存在 失败!"); 261 } 262 if(row0=mysql_fetch_row(result0)) 263 { 264 //数据已经存在 265 266 name_flag=true; 267 } 268 else 269 { 270 //数据不存在 271 272 name_flag=false; 273 } 274 mysql_free_result(result0); 275 276 mysql_close(ssock); //关闭数据库 277 278 return true; 279 280 } 281 282 283 bool CLoginDlg::disp_oprater_name(void) 284 { 285 //数据库的初始化============================================ 286 287 unsigned short Port = 3306; 288 char *IPAddress = "localhost"; 289 char *UserName = "root"; 290 char *Password = "love00"; //数据库密码 291 char *DBName = "myself_db"; //数据库名称 292 293 MYSQL *ssock; 294 char execsql[1500]; 295 ssock = (MYSQL *)malloc(sizeof(MYSQL)); 296 297 mysql_init(ssock); 298 if(ssock == NULL) 299 { 300 printf_s("ERROR: MySQL ssock init error. "); 301 return FALSE; 302 } 303 304 //连接到指定的数据库 305 306 mysql_options(ssock, MYSQL_SET_CHARSET_NAME, "gb2312"); 307 308 ssock = mysql_real_connect(ssock, IPAddress, UserName, Password, NULL, Port, NULL, 0); 309 if(!ssock) 310 { 311 printf_s("conn fail... "); 312 unsigned int mtint = mysql_errno(ssock); 313 return FALSE; 314 } 315 316 if(mysql_select_db(ssock, DBName) != 0) 317 { 318 printf_s("select db error. "); 319 return FALSE; 320 } 321 //数据库初始化========================================================== 322 323 CString execsql_str; 324 execsql_str="SELECT department FROM login_password_tab ;"; 325 strcpy_s(execsql,execsql_str.GetBuffer(execsql_str.GetLength())); 326 if(mysql_real_query(ssock,execsql,strlen(execsql))!=0) 327 { 328 AfxMessageBox("查询 login_tab 数据库中 name 出错!"); 329 } 330 MYSQL_RES *result; 331 MYSQL_ROW row; 332 if(!(result=mysql_use_result(ssock))) 333 { 334 AfxMessageBox("读取 login_tab 数据集 name 失败!"); 335 } 336 else 337 { 338 CString str; 339 ((CComboBox*)GetDlgItem(IDC_COMBO_OPRATER_NAME))->ResetContent(); 340 while(row=mysql_fetch_row(result)) 341 { 342 str.Format("%s",row[0]); 343 m_Combo_Oprater_Name.AddString(str); 344 } 345 } 346 mysql_free_result(result); 347 348 mysql_close(ssock); //关闭数据库 349 350 return true; 351 } 352 353 354 void CLoginDlg::OnBnClickedButtonCancel() 355 { 356 // TODO: 在此添加控件通知处理程序代码 357 CDialog::OnCancel(); 358 }
本文来自博客园,作者:Bytezero!,转载请注明原文链接:https://www.cnblogs.com/Bytezero/p/15221813.html