(一) 基本界面设计
(二) 数据库的建立和实现维护功能
保存按钮功能代码如下:
private void btnSave_Click(object sender, EventArgs e)
{
this.Validate();
this.schoolBindingSource.EndEdit();
this.schoolStudentBindingSource.EndEdit();
StudentsInfoDataSet.StudentDataTable deletedRows = studentsInfoDataSet.Student.GetChanges(DataRowState.Deleted) as StudentsInfoDataSet.StudentDataTable;
StudentsInfoDataSet.StudentDataTable addedRows = studentsInfoDataSet.Student.GetChanges(DataRowState.Added) as StudentsInfoDataSet.StudentDataTable;
StudentsInfoDataSet.StudentDataTable modifiedRows = studentsInfoDataSet.Student.GetChanges(DataRowState.Modified) as StudentsInfoDataSet.StudentDataTable;
try
{
this.Cursor = Cursors.WaitCursor;
if (deletedRows != null)
studentTableAdapter.Update(deletedRows);
schoolTableAdapter.Update(studentsInfoDataSet.School);
if (addedRows != null)
studentTableAdapter.Update(addedRows);
if (modifiedRows != null)
studentTableAdapter.Update(modifiedRows);
studentsInfoDataSet.AcceptChanges();
}
catch (Exception ex)
{
MessageBox.Show("在数据更新期间出现了错误!" + Environment.NewLine + ex.Message, "系统警告", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
}
finally
{
if (deletedRows != null)
deletedRows.Dispose();
if (addedRows != null)
addedRows.Dispose();
if (modifiedRows != null)
modifiedRows.Dispose();
this.Cursor = Cursors.Default;
}
}
取消按钮功能代码如下:
private void btnCancel_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
this.studentsInfoDataSet.RejectChanges();
this.Cursor = Cursors.Default;
}
(三) 实现报表功能和关闭窗口前的确认功能
报表设计界面如下:
报表显示界面如下:
报表功能代码如下:
private void frmReport_Load(object sender, EventArgs e)
{//为了在不同窗体间传递数据集,可以在生成数据集的窗体类中写一个公共静态的数据集对象,此方法如(public static DataBaseAccessMutiTablesEx.StudentsInfoDataSet sid;)
//此方法中写上从主窗体的传过来的这个公共静态的数据集对象,设为打印窗体绑定数据控件的数据源
SchoolStudentForSearchBindingSource.DataSource = frmMain.sid;
// TODO: 这行代码将数据加载到表“StudentsInfoDataSet.SchoolStudentForSearch”中。您可以根据需要移动或移除它。
//this.SchoolStudentForSearchTableAdapter.Fill(this.StudentsInfoDataSet.SchoolStudentForSearch);
this.rpvSchoolStudent.RefreshReport();
}
窗体关闭功能界面如下:
窗体关闭功能代码如下:
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (studentsInfoDataSet.HasChanges())
{
if (MessageBox.Show("你没有保存已修改过的数据,现在保存吗并退出吗?", "系统警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
btnSave_Click(null, null);
}
else
e.Cancel = true;
}
}
(四) 实现模糊查询功能
XSD中的 设置界面如下:
注意:对于ACCESS,参数要用?表示;而对于SQL参数要用@XXX表示.
XSD中的 设置效果如下:
查询功能代码如下:
//模糊查询
private void btnSearch_Click(object sender, EventArgs e)
{
if (txbSearch.Text == string.Empty) //没有输入查询内容
{
MessageBox.Show("请输入要查询的内容", "系统警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txbSearch.Select();
return;
}
switch (cbbFieldName.SelectedIndex)
{
case -1: //没有选择任何类型
MessageBox.Show("请选择要查询的类型名称","系统警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
break;
case 0: //学校名称 schoolStudentForSearchTableAdapter.FillBySchoolName(studentsInfoDataSet.SchoolStudentForSearch, txbSearch.Text);
break;
case 1: //学生姓名 schoolStudentForSearchTableAdapter.FillByStudentName(studentsInfoDataSet.SchoolStudentForSearch, txbSearch.Text);
break;
case 2: // 专业名称 schoolStudentForSearchTableAdapter.FillByMajorName(studentsInfoDataSet.SchoolStudentForSearch, txbSearch.Text);
break;
}
if(dgvSearch.Rows.Count==0)
MessageBox.Show("没有你要查询的记录!", "系统警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
//显示打印预览界面
private void btnPrint_Click(object sender, EventArgs e)
{//将这个sid公共静态的数据集对象引用一下!
sid = studentsInfoDataSet;
frmReport myFrmReport = new frmReport();
myFrmReport.Show();
}
(五) 课后练习: