企业应用程序开发 通讯录示例.docx
- 文档编号:6010977
- 上传时间:2023-01-03
- 格式:DOCX
- 页数:14
- 大小:83.04KB
企业应用程序开发 通讯录示例.docx
《企业应用程序开发 通讯录示例.docx》由会员分享,可在线阅读,更多相关《企业应用程序开发 通讯录示例.docx(14页珍藏版)》请在冰豆网上搜索。
企业应用程序开发通讯录示例
企业应用程序开发示例通讯录
企业应用程序开发几乎都离不开数据库。
目前,企业应用程序的典型结构是三层体系结构,即数据库层、业务层和表现层。
其中数据层一般选用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.Count==0)
{
return;
}
mode="edit";
DataGridViewRowrow=dataGridView1.SelectedRows[0];
stringstrid=row.Cells[0].Value.ToString();
psn.GetPerson(strid);
txt_Name.Text=psn.Name;
txt_Phone.Text=psn.Phone;
txt_Mobile.Text=psn.Mobile;
txt_Address.Text=psn.Address;
}
//退出按扭
privatevoidbtnExit_Click(objectsender,EventArgse)
{
Application.Exit();
}
//添加按扭
privatevoidbtnAdd_Click(objectsender,EventArgse)
{
txt_Name.Text="";
txt_Phone.Text="";
txt_Mobile.Text="";
txt_Address.Text="";
mode="add";
}
//保存按扭
privatevoidbtnSave_Click(objectsender,EventArgse)
{
psn.Name=txt_Name.Text.Trim();
psn.Phone=txt_Phone.Text.Trim();
psn.Mobile=txt_Mobile.Text.Trim();
psn.Address=txt_Address.Text.Trim();
if(mode=="add")
{
psn.Add();
}
else
{
psn.Update();
}
//dataGridView1.DataSource=shp.GetAll();
bindingSource1.DataSource=shp.GetAll();
}
//删除按扭
privatevoidbtnDelete_Click(objectsender,EventArgse)
{
if(dataGridView1.SelectedRows.Count==0)
{
return;
}
try
{
DataGridViewRowrow=dataGridView1.SelectedRows[0];
stringstrid=row.Cells[0].Value.ToString();
psn.Delete(strid);
//dataGridView1.DataSource=shp.GetAll();
bindingSource1.DataSource=shp.GetAll();
}
catch(Exceptionex)
{
}
}
}
}
系统打包部署
1.打开要安装的项目,在(自动生成解决方案)中右键“添加”/“新项目”,在“项目类型”/“其他项目类型”/“安装和部署”/“安装项目”在名称输入“TongXunMSSetup”。
2.创建了安装项目后,在“文件系统”的“目标计算机上的文件系统”节点选“应用程序文件夹”,右键选择“添加”/“项目输出”。
在项目下拉列表中选部署的应用程序,选择输出类型为“主输出”。
“配置”选“活动”。
然后按“确定”。
3.在中间部分右键,选“添加”/“文件”,添加要添加的内容文件,如数据库文件。
4.选中“主输出来自活动”,右键,“创建快捷方式”两次。
5.把创建的快捷方式改名为“通讯录管理系统”。
6.把两个快捷方式分别拉到用户桌面和用户的程序菜单上。
7.在安装项目TongXunMSSetup右键,选择“生成”,即可生成系统安装程序。
8.回到程序的开发目录下,Debug下可以找到TongXunMSSetup.exe安装文件。
9.用它到客户机去安装就可以了。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 企业应用程序开发 通讯录示例 企业 应用程序 开发 通讯录 示例