企业应用程序开发通讯录示例

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

企业应用程序开发示例通讯录企业应用程序开发几乎都离不开数据库。目前,企业应用程序的典型结构是三层体系结构,即数据库层、业务层和表现层。其中数据层一般选用Aceess、SqlServer或Oracle等数据库,业务层包括业务逻辑的类库(即一组DLL),表现层可以是窗体应用程序或网站的形式,如图所示。现通过编写一个通讯录程序,虽然简单,但反映了企业应用程序程序编写的一般思路。主界面:选用Aceess数据库编写一个Person.mdb数据库,里面有一个表Person。表结构如下:填写测试数据:所谓三层结构,是在客户端与数据库之间加了一个中间层。三层结构有效地解决了代码混写在一起时产生的问题,三层结构是今后编程人员开成强大编程思想的基础。三层项目结构的搭建搭建表示层:1.打开VS2008,“新建”—“项目”。2.在“新建项目”对话框中填写名为“ViewPro”的Windows程序。搭建业务层:1.在“文件”菜单项中,选择“添加”—“新建项目”。2.在“添加新建项目”对话框中选择项目模板,选择“类库”,填写名为“BLL”。搭建数据库访问层:1.在“文件”菜单项中,选择“添加”—“新建项目”。2.在“添加新建项目”对话框中选择项目模板,选择“类库”,填写名为“DAL”。添加各层之间的依赖关系1.在ViewPro项目右击“引用”,在弹出的菜单中选择“添加引用”。2.在“添加引用”对话框中选择项目选项卡,选中BLL和DAL。3.实现业务层对数据库层的依赖。在BLL项目右击“引用”,在弹出的菜单中选择“添加引用”,选中DAL。三层结构的编码:在数据访问层DAL添加一个名为SqlHelper.cs的类文件。usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Data;usingSystem.Data.OleDb;usingSystem.Text;namespaceDAL{publicclassSqlHelper{publicOleDbConnectionGetConnection()//获得连接对象{stringconnstr=@Provider=Microsoft.Jet.OLEDB.4.0;DataSource=Person.mdb;OleDbConnectionconn=newOleDbConnection(connstr);returnconn;}publicDataTableGetAll()//获取所有的记录{OleDbConnectioncn=GetConnection();stringSQLstr=select*fromPerson;OleDbDataAdapterdap=newOleDbDataAdapter(SQLstr,cn);OleDbCommandBuildermydbu=newOleDbCommandBuilder(dap);DataTabledt=newDataTable();dap.Fill(dt);returndt;}publicDataTableGetPartial(stringstrName)//按姓名查找,获取部分的记录{OleDbConnectioncn=GetConnection();stringSQLstr=string.Format(select*fromPersonwhereNamelike'%{0}%',strName);OleDbDataAdapterdap=newOleDbDataAdapter(SQLstr,cn);//OleDbCommandBuildermydbu=newOleDbCommandBuilder(dap);DataTabledt=newDataTable();dap.Fill(dt);returndt;}}}在数据业务层BLL添加一个名为Person.cs的类文件。usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingDAL;usingSystem.Data.OleDb;namespaceBLL{publicclassPerson{privatestring_id;//编号publicstringId{get{return_id;}set{_id=value;}}privatestring_name;//姓名publicstringName{get{return_name;}set{_name=value;}}privatestring_phone;//电话publicstringPhone{get{return_phone;}set{_phone=value;}}privatestring_mobile;//手机publicstringMobile{get{return_mobile;}set{_mobile=value;}}privatestring_address;//地址publicstringAddress{get{return_address;}set{_address=value;}}publicvoidAdd()//建立添加记录的方法{DAL.SqlHelperhelper1=newSqlHelper();OleDbConnectioncn=helper1.GetConnection();try{cn.Open();stringSqlstr=InsertintoPerson(Name,Phone,Mobile,Address)Values(@name,@phone,@mobile,@address);OleDbCommandcmd=newOleDbCommand(Sqlstr,cn);cmd.Parameters.AddWithValue(@name,Name);cmd.Parameters.AddWithValue(@phone,Phone);cmd.Parameters.AddWithValue(@mobile,Mobile);cmd.Parameters.AddWithValue(@address,Address);cmd.ExecuteNonQuery();}catch(Exceptionex){//throwex;}finally{cn.Close();}}publicvoidUpdate()//建立更新记录的方法{DAL.SqlHelperhelper1=newSqlHelper();OleDbConnectioncn=helper1.GetConnection();try{cn.Open();stringSqlstr=UpdatePersonsetName=@name,Phone=@phone,Mobile=@mobile,Address=@addresswhereId=+Id.ToString();//只能实现全部的更新,可以先搜索全部放进如textbox中。在进行修改OleDbCommandcmd=newOleDbCommand(Sqlstr,cn);cmd.Parameters.AddWithValue(@name,Name);cmd.Parameters.AddWithValue(@phone,Phone);cmd.Parameters.AddWithValue(@mobile,Mobile);cmd.Parameters.AddWithValue(@address,Address);cmd.ExecuteNonQuery();}catch(Exceptionex){//throwex;}finally{cn.Close();}}publicvoidDelete(stringstrid)//建立删除记录的方法{DAL.SqlHelperhelper1=newSqlHelper();OleDbConnectioncn=helper1.GetConnection();try{cn.Open();stringSqlstr=string.Format(DeletefromPersonwhereid={0},strid);OleDbCommandcmd=newOleDbCommand(Sqlstr,cn);cmd.ExecuteNonQuery();}catch(Exceptionex){//throwex;}finally{cn.Close();}}publicvoidGetPerson(stringstrid)//获取指定人员{DAL.SqlHelperhelper1=newSqlHelper();OleDbConnectioncn=helper1.GetConnection();try{cn.Open();stringSqlstr=string.Format(select*fromPersonwhereid={0},strid);OleDbCommandcmd=newOleDbCommand(Sqlstr,cn);OleDbDataReaderdr=cmd.ExecuteReader();if(dr.Read()){this.Id=dr[Id].ToString();this.Name=dr[Name].ToString();this.Phone=dr[Phone].ToString();this.Mobile=dr[Mobile].ToString();this.Address=dr[Address].ToString();}}catch(Exceptionex){//throwex;}finally{cn.Close();}}}}在表示层ViewPro添加一个名为Form1.cs的窗体窗体。usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingDAL;usingBLL;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceViewPro{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}stringmode=add;//当前模式:add-新增edit-修改DAL.SqlHelpershp;BLL.Personpsn;privatevoidForm1_Load(objectsender,EventArgse){dataGridView1.SelectionMode=DataGridViewSelectionMode.FullRowSelect;shp=newSqlHelper();psn=newPerson();//dataGridView1.DataSource=shp.GetAll();bindingSource1.DataSource=shp.GetAll();bindingNavigator1.BindingSource=bindingSource1;dataGridView1.DataSource=bindingSource1;}//查询按扭privatevoidbutton1_Click(objectsender,EventArgse){//dataGridView1.DataSource=shp.GetPartial(textBox1.Text);bindingSource1.DataSource=shp.GetPartial(textBox1.Text);}//选择数据表格的行privatevoiddataGridView1_SelectionChanged(objectsender,EventArgse){if(dataGridView1.SelectedRows.Co

1 / 10
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功