C#实验报告
通讯录管理系统
学院:计算机科学学院 班级:12级网络工程1班
姓名:张 挺
学号:41209040120
日期:20xx年11月11日
一、 试验目的:
熟悉数据库应用程序的开发步骤;学会运用数据库组件结构中的数据集组件、数据源组件和数据控制组件;学会使用数据源ODBC连接;练习使用相应组件及SQL语句完成对数据库的查询。
二、 实验内容:
1. 编写一个通讯录应用程序,该通讯录应用程序应具有信息的查看、编辑功能。
2. 该通讯录应用程序应具有对信息的添加和删除功能。
3. 该通讯录应用程序应具有如下对信息的查找功能:
1) 可以用名字和电话号码进行查询;
2) 支持模糊查询。
三、 功能截图:
(一)
登陆界面:用户输入账号和密码,若是输入不完整则出现提示:
(二) 菜单界面:
真确输入账号和密码后,登陆到菜单界面
(3)
查询功能界面: a. 模糊查询:
b. 查询图示:
c. 修改:
d. 删除:
(四) 查看所有:
(五)
添加联系人:
四、 主要代码:
(1)主界面:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 通讯录
{
public partial class menu : Form
{
string sql = "";
SqlConnection conn = new SqlConnection("server=localhost;database=phone;user=sa;password=994717664");//连接数据库
public menu()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
this.Hide();
view f2 = new view();
f2.Show();
}
private void menu_Load(object sender, EventArgs e)
{
string name;
string number;
try
{
conn.Open();//打开数据库
sql = "select Name,Number from contact";//定义执行语句
SqlCommand cmd = new SqlCommand(sql, conn);//创建实例 SqlDataReader rd2 = cmd.ExecuteReader();//读数据 while (rd2.Read())
{
name = Convert.ToString(rd2["Name"]);
number = Convert.ToString(rd2["Number"]); comboBox1.Items.Add(name);
comboBox2.Items.Add(number);
}
catch (Exception exe)
{
MessageBox.Show(exe.Message);
}
finally
{
if (conn != null) conn.Close();
cmd1.Dispose();//销毁对象,回收垃圾 }
/ /*/
}
private void button3_Click(object sender, EventArgs e)
{
this.Hide();
Form1 f3 = new Form1();
f3.Show();
}
private void button4_Click(object sender, EventArgs e)
{
//string sql="";
if (comboBox1.Text.Length != 0)
{
sql = "select * from contact where Name= '" comboBox1.Text.Trim() + "'";//定义执行语句
SqlCommand cmd = new SqlCommand(sql, conn);
try
{
conn.Open();
SqlDataReader rd = cmd.ExecuteReader();
while (rd.Read())
{
textBox1.Text = Convert.ToString(rd["Name"]);
comboBox3.Text = Convert.ToString(rd["Number"]); textBox3.Text = Convert.ToString(rd["Adresss"]); textBox4.Text = Convert.ToString(rd["Remarks"]); textBox5.Text = Convert.ToString(rd["Sex"]); }
if(textBox1.Text.Length==0)
MessageBox.Show("查无此人!", "提示"); comboBox1.Text="";
comboBox2.Text = "";
rd.Close();
conn.Close();
cmd.Dispose();//销毁对象,回收垃圾
}
catch
{
conn.Close();
}
}
else if (comboBox2.Text.Length != 0)
{ +
sql = "select * from contact where Number= '" +
comboBox2.Text.Trim() + "'";//定义执行语句
SqlCommand cmd = new SqlCommand(sql, conn);//创建实例
try
{
conn.Open();
SqlDataReader rd1 = cmd.ExecuteReader(); while (rd1.Read())
{
textBox1.Text = Convert.ToString(rd1["Name"]);
comboBox3.Text = Convert.ToString(rd1["Number"]);
textBox3.Text = Convert.ToString(rd1["Adresss"]);
textBox4.Text = Convert.ToString(rd1["Remarks"]);
textBox5.Text = Convert.ToString(rd1["Sex"]);
}
if (textBox1.Text.Length == 0)
MessageBox.Show("查无此人!", "提示");
comboBox1.Text = "";
comboBox2.Text = "";
rd1.Close();
conn.Close();
cmd.Dispose();//销毁对象,回收垃圾
}
catch
{
conn.Close();
MessageBox.Show("查无此人!", "提示");
}
}
else
{
MessageBox.Show("请输入!", "提示");
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
add f4=new add();
f4.Show();
}
private void pictureBox4_Click(object sender, EventArgs e)
{
sql = "delete from contact where Name= '" + textBox1.Text.Trim() + "'";//定义执行语句
SqlCommand cmd = new SqlCommand(sql, conn);/
try
{
conn.Open();
int n = cmd.ExecuteNonQuery();
if (n > 0) MessageBox.Show("成功删除联系人!");
}
catch (Exception exe)
{
MessageBox.Show(exe.Message);
}
finally
{
if (conn != null) conn.Close();
cmd.Dispose();//销毁对象,回收垃圾
}
comboBox1.Items.Clear();//置空查询框
comboBox1.Items.Add("");
comboBox2.Items.Clear();
comboBox2.Items.Add("");
comboBox3.Items.Clear();
comboBox3.Text = "";
textBox1.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
menu_Load(null,null); //刷新窗体
}
private void pictureBox5_Click(object sender, EventArgs e)
{
sql = "update contact set Name='" + textBox1.Text + "',Number='" + comboBox3.Text + "',Adresss='" + textBox3.Text + "',Remarks='" + textBox4.Text + "',Sex='" + textBox5.Text + "' where Name='" + textBox1.Text + "'";
SqlCommand cmd = new SqlCommand();
try
{
cmd.Connection = conn;
cmd.CommandText = sql;
conn.Open();
int n = cmd.ExecuteNonQuery();
if (n > 0) MessageBox.Show("成功修改联系人信息!");
}
catch (Exception exe)
{
MessageBox.Show(exe.Message);
}
finally
{
if (conn != null) conn.Close();
cmd.Dispose();//销毁对象,回收垃圾
}
}
}
}
(2)添加联系人:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 通讯录
{
public partial class add : Form
{
string strsql = "";
SqlConnection conn = new SqlConnection("server=localhost;database=phone;user=sa;password=994717664");//连接数据库 public add()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
textBox1.Focus();
}
private void button3_Click(object sender, EventArgs e)
{
this.Hide();
menu f1 = new menu();
f1.Show();
}
private void button1_Click(object sender, EventArgs e)
{
strsql ="insert into contact values('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"','"+textBox4.Text+"','"+textBox5.Text+"')";
SqlCommand cmd1 = new SqlCommand();
try
{
cmd1.Connection = conn;
cmd1.CommandText = strsql;
conn.Open();
int n = cmd1.ExecuteNonQuery();
if (n > 0) MessageBox.Show("成功添加联系人");
}
catch (Exception exe)
{
MessageBox.Show(exe.Message);
}
finally
{
if (conn != null) conn.Close();
cmd1.Dispose();//销毁对象,回收垃圾
}
this.Hide();
menu f3 = new menu();
f3.Show();
}
}
}
(3)查看代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Odbc;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 通讯录
{
public partial class view : Form
{
public view()
{
InitializeComponent();
}
private void view_Load(object sender, EventArgs e)
{
OdbcConnection odbcCon = new OdbcConnection("DSN=phone;uid=sa;pwd=994717664");
try
{
OdbcDataAdapter odbcDat = new OdbcDataAdapter("SELECT * FROM contact", odbcCon);
DataTable dt = new DataTable("phone");
odbcDat.Fill(dt);
this.dataGridView1.DataSource = dt.DefaultView;
}
catch (Exception ey)
{
MessageBox.Show(ey.Message);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = string.Format("现在时间: {0}",DateTime.Now.ToLongTimeString());
}
private void pictureBox2_Click(object sender, EventArgs e)
{
this.Hide();
menu f1 = new menu();
f1.Show();
}
private void pictureBox3_Click(object sender, EventArgs e)
{
this.Hide();
Form1 f2 = new Form1();
f2.Show();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
this.Hide();
add f3 = new add();
f3.Show();
}
}
}
五、 实验思考:
(1)
(2)
(3)
(4) 这个简单的通讯录力求简单,容易操作,让人一看就会。 只是一个通讯录而已,没必要做的多么华丽,所以做的界面比较简洁。 把以前学到的知识,尽量在实验里全部用到,比如时间显示,模糊查询。 考虑到一个人可能有多个号码,所以在显示号码那里,使用的是可以有下拉功能的
ComboBox组件;
(5) 通过自己做这个小实验,自己感觉学到了好多,知道了要考虑各方面的问题。比如
数据库连接后操作了,但却没有 关闭,就会导致执行其他数据库操作出错。模糊查询那里,新增了联系人,同时也要在查询的那里更新……做的过程中遇到了许多问题,但都自己查资料解决,事后很有成就感。