-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDialog_search_user.cpp
155 lines (142 loc) · 5.57 KB
/
CDialog_search_user.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// CDialog_search_user.cpp: 实现文件
//
#include "pch.h"
#include "bill_ticket_system.h"
#include "CDialog_search_user.h"
#include "afxdialogex.h"
// CDialog_search_user 对话框
IMPLEMENT_DYNAMIC(CDialog_search_user, CDialogEx)
CDialog_search_user::CDialog_search_user(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_DIALOG7, pParent)
, phone(_T(""))
, name(_T(""))
, id_(_T(""))
, real_name(_T(""))
, status(_T(""))
, email(_T(""))
{
}
CDialog_search_user::~CDialog_search_user()
{
}
void CDialog_search_user::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, m_list);
DDX_Text(pDX, IDC_EDIT1, phone);
DDV_MaxChars(pDX, phone, 11);
DDX_Text(pDX, IDC_EDIT4, name);
DDV_MaxChars(pDX, name, 32);
DDX_Text(pDX, IDC_EDIT5, id_);
DDV_MaxChars(pDX, id_, 18);
DDX_Text(pDX, IDC_EDIT6, real_name);
DDV_MaxChars(pDX, real_name, 32);
DDX_CBString(pDX, IDC_COMBO2, status);
DDV_MaxChars(pDX, status, 4);
DDX_Text(pDX, IDC_EDIT7, email);
DDV_MaxChars(pDX, email, 32);
}
BEGIN_MESSAGE_MAP(CDialog_search_user, CDialogEx)
ON_BN_CLICKED(IDOK, &CDialog_search_user::OnBnClickedOk)
END_MESSAGE_MAP()
// CDialog_search_user 消息处理程序
BOOL CDialog_search_user::OnInitDialog()
{
// TODO: 在此添加额外的初始化代码
CDialogEx::OnInitDialog();
m_list.InsertColumn(0, TEXT("手机号"), LVCFMT_LEFT, 90);
m_list.InsertColumn(1, TEXT("用户名"), LVCFMT_LEFT, 100);
m_list.InsertColumn(2, TEXT("身份证号"), LVCFMT_LEFT, 120);
m_list.InsertColumn(3, TEXT("姓名"), LVCFMT_LEFT, 100);
m_list.InsertColumn(4, TEXT("身份"), LVCFMT_LEFT, 50);
m_list.InsertColumn(5, TEXT("电子邮件"), LVCFMT_LEFT, 150);
m_list.SetExtendedStyle(m_list.GetExtendedStyle() | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
MYSQL m_mysql; //数据库结构
mysql_init(&m_mysql);//初始化数据库对象
mysql_real_connect(&m_mysql, "localhost", "root", "19991114", "bill_ticket", 3306, NULL, 0);//连接数据库
//////////////////////////////////////////////////////
MYSQL_RES* m_res; //查询的返回结果集
MYSQL_ROW m_row; //获取每一条记录
std::vector<std::string> m_data[100]; //存放数据库记录,最大为100条
char query[] = "select user_phone,user_name,`id`,real_name,status,email from user";
mysql_query(&m_mysql, "set names 'gb2312'");
mysql_real_query(&m_mysql, query, strlen(query));//查询函数,语句用字符串表示
m_res = mysql_store_result(&m_mysql);//得到查询结果
//从数据库获取数据
int i = 0;
while (m_row = mysql_fetch_row(m_res))
{
m_data[i].push_back(m_row[0]);
m_data[i].push_back(m_row[1]);
m_data[i].push_back(m_row[2]);
m_data[i].push_back(m_row[3]);
m_data[i].push_back(m_row[4]);
m_data[i++].push_back(m_row[5]);
}
m_list.DeleteAllItems();
for (int i = 0; i < m_res->row_count; i++)
{
int iCount = m_list.GetItemCount();
m_list.InsertItem(iCount, m_data[i][0].c_str());
for (int j = 0; j < 6; j++)
m_list.SetItemText(iCount, j, m_data[i][j].c_str());
}
//释放结果集
mysql_free_result(m_res);
//关闭数据库
mysql_close(&m_mysql);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CDialog_search_user::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
GetDlgItem(IDC_EDIT1)->GetWindowText(phone);
GetDlgItem(IDC_EDIT4)->GetWindowText(name);
GetDlgItem(IDC_EDIT5)->GetWindowText(id_);
GetDlgItem(IDC_EDIT6)->GetWindowText(real_name);
GetDlgItem(IDC_COMBO2)->GetWindowText(status);
GetDlgItem(IDC_EDIT7)->GetWindowText(email);
MYSQL m_mysql; //数据库结构
mysql_init(&m_mysql);//初始化数据库对象
mysql_real_connect(&m_mysql, "localhost", "root", "19991114", "bill_ticket", 3306, NULL, 0);//连接数据库
//////////////////////////////////////////////////////
char* in_phone = (char*)phone.GetBuffer();
char* in_name = (char*)name.GetBuffer();
char* in_id = (char*)id_.GetBuffer();
char* in_real_name = (char*)real_name.GetBuffer();
char* in_status = (char*)status.GetBuffer();
char* in_email = (char*)email.GetBuffer();
char query[1000];
sprintf_s(query, "select user_phone,user_name,`id`,real_name,status,email from user where user_phone like '%s%%' and user_name like '%s%%' and `id` like '%s%%' and real_name like '%s%%' and status like '%s%%' and email like '%s%%'", in_phone, in_name, in_id, in_real_name, in_status, in_email);
mysql_query(&m_mysql, "set names 'gb2312'");
mysql_real_query(&m_mysql, query, strlen(query));//查询函数,语句用字符串表示
MessageBox(_T("查询成功!"), _T("提示"));
MYSQL_RES* m_res; //查询的返回结果集
MYSQL_ROW m_row; //获取每一条记录
std::vector<std::string> m_data[100]; //存放数据库记录,最大为100条
m_res = mysql_store_result(&m_mysql);//得到查询结果
//从数据库获取数据
int i = 0;
while (m_row = mysql_fetch_row(m_res))
{
m_data[i].push_back(m_row[0]);
m_data[i].push_back(m_row[1]);
m_data[i].push_back(m_row[2]);
m_data[i].push_back(m_row[3]);
m_data[i].push_back(m_row[4]);
m_data[i++].push_back(m_row[5]);
}
m_list.DeleteAllItems();
for (int i = 0; i < m_res->row_count; i++)
{
int iCount = m_list.GetItemCount();
m_list.InsertItem(iCount, m_data[i][0].c_str());
for (int j = 0; j < 6; j++)
m_list.SetItemText(iCount, j, m_data[i][j].c_str());
}
//释放结果集
mysql_free_result(m_res);
//关闭数据库
mysql_close(&m_mysql);
}