-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb003dlg1.cpp
More file actions
executable file
·343 lines (308 loc) · 8.25 KB
/
db003dlg1.cpp
File metadata and controls
executable file
·343 lines (308 loc) · 8.25 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// db003dlg1.cpp : implementation file
//
#include "stdafx.h"
#include "xitong.h"
#include "db003dlg1.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// Cdb003dlg dialog
Cdb003dlg::Cdb003dlg(CWnd* pParent /*=NULL*/)
: CDialog(Cdb003dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(Cdb003dlg)
m_strId = _T("");
m_strName = _T("");
m_nScore = 0;
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void Cdb003dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(Cdb003dlg)
DDX_Control(pDX, IDC_COMBO_METHOD, m_comMethod);
DDX_Control(pDX, IDC_COMBO_COLUMN, m_comColumn);
DDX_Control(pDX, IDC_LIST_STUDENT, m_StudentList);
DDX_Text(pDX, IDC_EDIT_ID, m_strId);
DDX_Text(pDX, IDC_EDIT_NAME, m_strName);
DDX_Text(pDX, IDC_EDIT_SCORE, m_nScore);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(Cdb003dlg, CDialog)
//{{AFX_MSG_MAP(Cdb003dlg)
ON_WM_SYSCOMMAND()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_INSERT, OnButtonInsert)
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_BUTTON_SAVE, OnButtonSave)
ON_BN_CLICKED(IDC_BUTTON_CANCEL, OnButtonCancel)
ON_BN_CLICKED(IDC_BUTTON_MOD, OnButtonMod)
ON_BN_CLICKED(IDC_BUTTON_DEL, OnButtonDel)
ON_NOTIFY(NM_CLICK, IDC_LIST_STUDENT, OnClickListStudent)
ON_BN_CLICKED(IDC_BUTTON_SORT, OnButtonSort)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// Cdb003dlg message handlers
BOOL Cdb003dlg::OnInitDialog()
{
CDialog::OnInitDialog();
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
// Set small icon
// TODO: Add extra initialization here
m_StudentList.SetExtendedStyle(m_StudentList.GetExtendedStyle()|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
m_StudentList.InsertColumn(0,"学号",LVCFMT_LEFT,200);
m_StudentList.InsertColumn(1,"姓名",LVCFMT_LEFT,200);
m_StudentList.InsertColumn(2,"平均绩点",LVCFMT_LEFT,300);
_bstr_t strcnn(_T("Provider=Microsoft.JET.OLEDB.4.0;Data source=student.mdb"));
try
{
m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection->Open(strcnn,_T(""),_T(""),adModeUnknown);
m_pRecordset.CreateInstance(__uuidof(Recordset));
}
catch (_com_error e)
{
AfxMessageBox("连接数据库失败");
}
FreshList();
EnableInput(FALSE);
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void Cdb003dlg::OnButtonInsert() //插入按钮,定义选择后功能为1
{
// TODO: Add your control notification handler code here
m_nOperation=1;
EnableInput(TRUE);
}
void Cdb003dlg::OnClose() //取消操作按钮
{
// TODO: Add your message handler code here and/or call default
m_pConnection->Close();
m_pConnection.Release();
CoUninitialize();
CDialog::OnClose();
}
void Cdb003dlg::OnButtonSave() //保存按钮
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if(m_nOperation==1)//选择添加,功能号为1
{
if (m_strName.IsEmpty())
{
AfxMessageBox("姓名不能为空");
return;
}
if (m_strId.IsEmpty())
{
AfxMessageBox("学号不能为空");
return;
}
CString strSql;
strSql.Format(_T("INSERT INTO StudentInfo VALUES('%s','%s',%d)"),m_strId,m_strName,m_nScore);//SQL语句将信息保存在数据库中
try
{
m_pConnection->Execute(_bstr_t(strSql),0,adCmdText);
}
catch(_com_error e)
{
AfxMessageBox("增加记录失败");
}
}
else if (m_nOperation==2)//修改信息功能号为2
{
if (m_strName.IsEmpty())
{
AfxMessageBox("姓名不能为空");
return;
}
if (m_strId.IsEmpty())
{
AfxMessageBox("学号不能为空");
return;
}
CString strSql;
strSql.Format(_T("UPDATE StudentInfo SET student_name=\'%s\',score=%d WHERE student_id=\'%s\'"),m_strName,m_nScore,m_strId);//SQL语句将信息保存在数据库中
try
{
m_pConnection->Execute(_bstr_t(strSql),0,adCmdText);
}
catch(_com_error e)
{
AfxMessageBox("修改记录失败");
}
}
FreshList();
EnableInput(FALSE);
}
void Cdb003dlg::OnButtonCancel()
{
// TODO: Add your control notification handler code here
EnableInput(FALSE);
}
void Cdb003dlg::FreshList()
{
try
{
m_pRecordset->Open(_T("SELECT * FROM StudentInfo"),(IDispatch*)m_pConnection,\
adOpenDynamic,adLockOptimistic,adCmdText);
int nItem;
m_StudentList.DeleteAllItems();
while(!m_pRecordset->adoEOF)
{
nItem=m_StudentList.GetItemCount();
CString str;
str=(LPCTSTR)_bstr_t(m_pRecordset->GetCollect("student_id"));
m_StudentList.InsertItem(nItem,str);
str=(LPCTSTR)_bstr_t(m_pRecordset->GetCollect("student_name"));
m_StudentList.SetItemText(nItem,1,str);
str=(LPCTSTR)_bstr_t(m_pRecordset->GetCollect("score"));
m_StudentList.SetItemText(nItem,2,str);
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
}
catch (_com_error e)
{
AfxMessageBox("刷新列表失败");
}
}
void Cdb003dlg::EnableInput(BOOL bEnable)//实现允许输入
{
CEdit *pEdit;
CButton *pButton;
pEdit = (CEdit *)GetDlgItem(IDC_EDIT_ID);
pEdit->SetReadOnly(!bEnable);
pEdit = (CEdit *)GetDlgItem(IDC_EDIT_NAME);
pEdit->SetReadOnly(!bEnable);
pEdit = (CEdit *)GetDlgItem(IDC_EDIT_SCORE);
pEdit->SetReadOnly(!bEnable);
pButton=(CButton *)GetDlgItem(IDC_BUTTON_INSERT);
pButton->EnableWindow(!bEnable);
pButton=(CButton *)GetDlgItem(IDC_BUTTON_DEL);
pButton->EnableWindow(!bEnable);
pButton=(CButton *)GetDlgItem(IDC_BUTTON_MOD);
pButton->EnableWindow(!bEnable);
pButton=(CButton *)GetDlgItem(IDC_BUTTON_SAVE);
pButton->EnableWindow(bEnable);
pButton=(CButton *)GetDlgItem(IDC_BUTTON_CANCEL);
pButton->EnableWindow(bEnable);
}
void Cdb003dlg::OnButtonMod() //定义修改按钮
{
// TODO: Add your control notification handler code here
m_nOperation=2;
EnableInput(TRUE);
CEdit *pEdit=(CEdit*)GetDlgItem(IDC_EDIT_ID);
pEdit->SetReadOnly(TRUE);
}
void Cdb003dlg::OnButtonDel()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if (m_strId.IsEmpty())
{
AfxMessageBox("学生号不能为空");
return;
}
CString strSql;
strSql.Format(_T("DELETE FROM StudentInfo WHERE student_id=\'%s\'"),m_strId);
try
{
m_pConnection->Execute((_bstr_t)strSql,0,adCmdText);
}
catch(_com_error e)
{
AfxMessageBox("删除记录失败");
}
FreshList();
}
void Cdb003dlg::OnClickListStudent(NMHDR* pNMHDR, LRESULT* pResult) //实现点击学生列表中信息显示到编辑框
{
// TODO: Add your control notification handler code here
int n=m_StudentList.GetSelectionMark();
m_strId=m_StudentList.GetItemText(n,0);
m_strName=m_StudentList.GetItemText(n,1);
CString str1=m_StudentList.GetItemText(n,2);
m_nScore=atoi(str1.GetBuffer(10));
str1.ReleaseBuffer();
UpdateData(FALSE);
*pResult = 0;
}
void Cdb003dlg::GetSortData(LPCTSTR lpszKey, BOOL bAsc)//排序功能的实现
{
CString strOrder=bAsc?"ASC":"DESC";
CString strSql;
strSql.Format(_T("SELECT * FROM StudentInfo ORDER BY %s %s"),lpszKey,strOrder);
try
{
m_pRecordset->Open((_bstr_t)strSql,(IDispatch*)m_pConnection,adOpenDynamic,adLockOptimistic,adCmdText);
int nItem;
m_StudentList.DeleteAllItems();
while(!m_pRecordset->adoEOF)
{
nItem=m_StudentList.GetItemCount();
CString str;
str=(LPCTSTR)_bstr_t(m_pRecordset->GetCollect("student_id"));
m_StudentList.InsertItem(nItem,str);
str=(LPCTSTR)_bstr_t(m_pRecordset->GetCollect("student_name"));
m_StudentList.SetItemText(nItem,1,str);
str=(LPCTSTR)_bstr_t(m_pRecordset->GetCollect("score"));
m_StudentList.SetItemText(nItem,2,str);
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
}
catch(_com_error e)
{
AfxMessageBox("刷新列表失败");
}
}
void Cdb003dlg::OnButtonSort()
{
// TODO: Add your control notification handler code here
int nIndex=m_comMethod.GetCurSel();
if (nIndex==-1)
{
return;
}
BOOL bAsc=FALSE;
if (nIndex==0)
{
bAsc=TRUE;
}
nIndex=m_comColumn.GetCurSel();
if (nIndex==-1)
{
return;
}
CString strKey;
if (nIndex==0)
{
strKey="student_id";
}
if (nIndex==1)
{
strKey="score";
}
GetSortData(strKey,bAsc);
}