实验项目名称:多层C/S结构应用设计 实验学时: 4学时 同组学生姓名: 实验地点: 1216 实验日期: 2015.11.10-2015.11.24 实验成绩: 批改教师: 批改时间:
一、 实验目的和要求
设计并实现一个基于多层C/S结构的数据库应用,熟悉多层C/S体系结构及其基本处理流程,了解多层结构表现层、业务逻辑层(功能层)、数据访问层所完成的功能,掌握多层C/S结构的数据库应用设计方法,对这三层进行明确分割,并在逻辑上使其独立。学生通过本实验的训练能够熟练掌握对小型数据库应用系统三层结构层次划分方法及系统实现技术。
二、 实验仪器和设备
奔腾以上计算机,Windows 10 、Visual Studio 2013、MySQL
三、 实验过程
分别采用二层C/S结构和多层C/S结构实现个人通讯录系统。该系统的设计目标是能够轻松地管理个人的联系人信息,包括添加、修改和删除操作。联系人信息包括姓名、住址、电话。整个系统的功能图如下图所示:
应用程序代码:
using System;
using System.Windows.Forms; using MySql.Data.MySqlClient;
namespace WindowsFormsApplication2 {
public partial class Form1 : Form {
public int action = 0;//0:添加;1:修改 public Form1() {
InitializeComponent(); }
private void groupBox3_Enter(object sender, EventArgs e) {
}
private void button3_Click(object sender, EventArgs e) {
frm1.Enabled = true; frm2.Enabled = false; action = 0;
}
private void add() {
long id=0;
int count=list1.Items.Count; if (count == 0) {
id = 1001; } else {
id = long.Parse(list1.Items[count - 1].Text)+1; }
Dbcon connector = new Dbcon();
MySqlConnection con = connector.dbcon(); con.Open();
MySqlCommand cmd = new MySqlCommand(\(id,name,phoneno,location) values(@id,@name,@phoneno,@location)\ cmd.Parameters.AddWithValue(\ cmd.Parameters.AddWithValue(\ cmd.Parameters.AddWithValue(\ cmd.Parameters.AddWithValue(\ cmd.ExecuteNonQuery();
con.Close(); build(); }
private void modify() {
foreach (ListViewItem item in list1.SelectedItems) {
Dbcon connector = new Dbcon();
MySqlConnection con = connector.dbcon(); con.Open();
MySqlCommand cmd = con.CreateCommand(); cmd.CommandText=\
name=@name,phoneno=@phoneno,location=@location where id=@id\ cmd.Parameters.AddWithValue(\ cmd.Parameters.AddWithValue(\ cmd.Parameters.AddWithValue(\ cmd.Parameters.AddWithValue(\
cmd.ExecuteNonQuery(); con.Close(); } build(); }
private void button1_Click(object sender, EventArgs e) {
if (action == 0) {
add(); }
else if (action == 1) {
modify(); }
name.Clear(); phoneno.Clear(); location.Clear(); frm2.Enabled = true; frm1.Enabled = false; }
private void button4_Click(object sender, EventArgs e) {
foreach (ListViewItem item in list1.SelectedItems) {
frm1.Enabled = true; frm2.Enabled = false;
name.Text = item.SubItems[1].Text; phoneno.Text = item.SubItems[2].Text; location.Text = item.SubItems[3].Text; action = 1; } }
private void Form1_Load(object sender, EventArgs e) {
build();
}
public void build() {
list1.BeginUpdate(); list1.Items.Clear();
Dbcon connector = new Dbcon();
MySqlConnection con = connector.dbcon(); con.Open();
MySqlCommand cmd = new MySqlCommand(\* from person\con); MySqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) {
ListViewItem item = new ListViewItem(); item.Text = Convert.ToString(dr[\
item.SubItems.Add(Convert.ToString(dr[\ item.SubItems.Add(Convert.ToString(dr[\ item.SubItems.Add(Convert.ToString(dr[\ list1.Items.Add(item); }
dr.Close(); con.Close(); list1.EndUpdate(); }
private void button5_Click(object sender, EventArgs e) {