一、取送货管理项目需求
该系统的业务背景如下:客户是一个针织半成品生产加工作坊,有很多生产加工人员从客户工厂那里取走半成品,加工成成品后送回来。客户根据加工每种半成品的加工单价和完成数量,付费用给生产加工人员。系统的需求很简单,要求统计出每个加工人员取走和送回半成品的数量,时间。以及统计加工人员的应付费用和已付费用。
师傅之前开发的是单机版的(所谓单机版就是数据库独立,不能连接庞大的数据库,这是我的理解,不知道准不准确)界面如下,进行增删改查,我演示的是微软C#所带控件的开发过程。
二、取送货WinForm项目开发过程
(一)取送货数据库的构建
此项目的开发最开始的是构建数据库,经分析需要建立六个表和六个视图,分别是产品分类表,产品分类表,取送货人表,取货信息表,送货信息表和加工费支付信息表,视图主要是建立表与表连接的关系临时表,数据库命名为TakeSendMisCSDB,命名要有实际意义,建立表和视图代码如下:
1 --产品分类表 2 IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES 3 WHERE TABLE_NAME = ‘CK_ProductType‘) 4 DROP TABLE CK_ProductType 5 CREATE TABLE dbo.CK_ProductType( 6 CK_ProductTypeID INT IDENTITY NOT NULL PRIMARY KEY NONCLUSTERED, 7 CK_ProductTypeName VARCHAR(64) NOT NULL DEFAULT ‘‘ 8 ); 9 10 --产品信息表 11 IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES 12 WHERE TABLE_NAME = ‘CK_Product‘) 13 DROP TABLE CK_Product 14 CREATE TABLE dbo.CK_Product( 15 CK_ProductID INT IDENTITY NOT NULL PRIMARY KEY NONCLUSTERED, 16 CK_ProductTypeID INT NOT NULL FOREIGN KEY REFERENCES CK_ProductType(CK_ProductTypeID) ON UPDATE CASCADE, 17 CK_ProductName VARCHAR(100) NOT NULL DEFAULT ‘‘, 18 CK_ProductPrice DECIMAL(10,2) NOT NULL DEFAULT 0 19 ); 20 --取送货人表 21 IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES 22 WHERE TABLE_NAME = ‘CK_People‘) 23 DROP TABLE CK_People 24 CREATE TABLE dbo.CK_People( 25 CK_PeopleID INT IDENTITY NOT NULL PRIMARY KEY NONCLUSTERED, 26 CK_PeopleName VARCHAR(32) NOT NULL DEFAULT ‘‘, 27 CK_PhoneNo VARCHAR(32) NOT NULL DEFAULT ‘‘, 28 CK_Comment VARCHAR(100) NOT NULL DEFAULT ‘‘ 29 ); 30 31 --取货信息表 32 IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES 33 WHERE TABLE_NAME = ‘CK_TakeGoods‘) 34 DROP TABLE CK_TakeGoods 35 CREATE TABLE dbo.CK_TakeGoods( 36 CK_TakeGoodsID INT IDENTITY NOT NULL PRIMARY KEY NONCLUSTERED, 37 CK_PeopleID INT NOT NULL FOREIGN KEY REFERENCES CK_People(CK_PeopleID) ON UPDATE CASCADE, 38 CK_ProductID INT NOT NULL FOREIGN KEY REFERENCES CK_Product(CK_ProductID) ON UPDATE CASCADE, 39 CK_TakeGoodsNo VARCHAR(32) NOT NULL DEFAULT ‘‘, 40 CK_TakeGoodsAmount INT NOT NULL DEFAULT 0, 41 CK_TakeGoodsDate DATETIME not NULL DEFAULT ‘‘ 42 ); 43 44 45 --送货信息表 46 IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES 47 WHERE TABLE_NAME = ‘CK_SendGoods‘) 48 DROP TABLE CK_SendGoods 49 CREATE TABLE dbo.CK_SendGoods( 50 CK_SendGoodsID INT IDENTITY NOT NULL PRIMARY KEY NONCLUSTERED, 51 CK_PeopleID INT NOT NULL FOREIGN KEY REFERENCES CK_People(CK_PeopleID) ON UPDATE CASCADE, 52 CK_ProductID INT NOT NULL FOREIGN KEY REFERENCES CK_Product(CK_ProductID) ON UPDATE CASCADE, 53 CK_SendGoodsNo VARCHAR(32) NOT NULL DEFAULT ‘‘, 54 CK_SendGoodsAmount INT NOT NULL DEFAULT 0, 55 CK_SendGoodsDate DATETIME not NULL DEFAULT ‘‘ 56 ); 57 58 --加工费支付信息表 59 IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES 60 WHERE TABLE_NAME = ‘CK_Payment‘) 61 DROP TABLE CK_Payment 62 CREATE TABLE dbo.CK_Payment( 63 CK_PaymentID INT IDENTITY NOT NULL PRIMARY KEY NONCLUSTERED, 64 CK_PeopleID INT NOT NULL FOREIGN KEY REFERENCES CK_People(CK_PeopleID) ON UPDATE CASCADE, 65 CK_PayDate DATETIME NOT NULL DEFAULT ‘‘, 66 CK_PayMoney DECIMAL(10,2) NOT NULL DEFAULT 0, 67 CK_PayComment VARCHAR(100) NOT NULL DEFAULT ‘‘ 68 );
1 CREATE VIEW [dbo].[v_listPayDetailInfo] 2 AS 3 select ck_people.ck_peoplename ,PayMoney as needPayMoney ,coalesce(PayedMoney,0) as PayedMoney,(PayMoney - coalesce(PayedMoney,0)) as unPayedMoney 4 from (select ck_peopleid,sum(CK_Product.ck_ProductPrice*ck_SendgoodsAmount) PayMoney from ck_Sendgoods 5 LEFT join CK_Product on CK_Product.ck_ProductID = CK_SendGoods.ck_ProductID 6 group by ck_peopleid) tab1 7 left join 8 (select ck_peopleid,sum(ck_paymoney) PayedMoney from ck_payment group by ck_peopleid) tab2 9 on tab1.[ck_peopleid] = tab2.ck_peopleid 10 left join ck_people on tab2.ck_peopleid = ck_people.ck_peopleid 11 12 13 CREATE VIEW [dbo].[v_listPaymentInfo] 14 AS 15 select ck_payment.ck_paymentid, ck_people.ck_peoplename, ck_paymoney, ck_paydate, ck_paycomment from ck_payment 16 join ck_people on ck_payment.ck_peopleid = ck_people.ck_peopleid 17 18 19 CREATE VIEW [dbo].[v_listProductInfo] 20 AS 21 select ck_productid,ck_productname, ck_productprice, ck_productType.ck_productTypename from ck_product 22 join ck_productType on ck_product.ck_productTypeid = ck_productType.ck_productTypeid 23 24 25 CREATE VIEW [dbo].[v_listSendGoodsInfo] 26 AS 27 select ck_sendgoods.ck_sendgoodsid 28 , ck_sendgoods.ck_sendgoodsno 29 , ck_sendgoods.ck_sendgoodsdate 30 ,ck_people.ck_peoplename,ck_producttype.ck_producttypename 31 ,ck_product.ck_productname , ck_sendgoods.ck_sendgoodsAmount 32 ,ck_product.ck_productprice 33 ,ck_sendgoods.ck_sendgoodsAmount * ck_product.ck_productprice as totalfee 34 from ck_sendgoods 35 join ck_product on ck_product.ck_productid = ck_sendgoods.ck_productid 36 join ck_producttype on ck_producttype.ck_producttypeid = ck_product.ck_producttypeid 37 join ck_people on ck_people.ck_peopleid = ck_sendgoods.ck_peopleid 38 39 40 CREATE VIEW [dbo].[v_listTakeGoodsInfo] 41 AS 42 select ck_takegoods.ck_takegoodsid, ck_takegoods.ck_takegoodsno, ck_takegoods.ck_takegoodsdate 43 ,ck_people.ck_peoplename,ck_producttype.ck_producttypename 44 , ck_product.ck_productname , ck_takegoods.ck_takegoodsAmount from ck_takegoods 45 join ck_product on ck_product.ck_productid = ck_takegoods.ck_productid 46 join ck_producttype on ck_producttype.ck_producttypeid = ck_product.ck_producttypeid 47 join ck_people on ck_people.ck_peopleid = ck_takegoods.ck_peopleid 48 49 50 CREATE VIEW [dbo].[v_listTakeSendGoodsInfo] 51 AS 52 select CK_People.ck_PeopleName ,CK_ProductType.CK_ProductTypeName 53 ,CK_Product.CK_ProductName,tab1.sum takenum,coalesce(tab2.sum,0) backnum,tab1.sum - coalesce(tab2.sum,0) diffnum from 54 (select CK_PeopleID,CK_ProductID,sum(ck_takegoodsAmount) sum from ck_takegoods group by CK_ProductID,CK_PeopleID) tab1 55 left join 56 (select CK_PeopleID,CK_ProductID,sum(ck_sendgoodsAmount) sum from ck_sendgoods group by CK_ProductID,CK_PeopleID) tab2 57 on tab2.CK_PeopleID =tab1.CK_PeopleID and tab2.CK_ProductID = tab1.CK_ProductID 58 left join CK_Product on CK_Product.CK_ProductID = tab1.CK_ProductID 59 left join CK_ProductType on CK_ProductType.CK_ProductTypeID = CK_Product.CK_ProductTypeID 60 left join CK_People on CK_People.CK_PeopleID = tab1.CK_PeopleID
(二)用动软代码生成器三层导入到WinForm项目开发中
命名空间为TSM,选择要导入的数据库,操作如图:
将生成代码中的BLL,DAL,MODEL,DBUtility文件夹复制到解决方案同层工作目录下。
如何将其包含在项目中?步骤如下:右击“解决方案”——添加——现有项——找到BLL文件夹下BLL.csproj文件——完成,其它三个也一样导入。截图如下:
之后在项目上右击——添加引用——项目——将四个全选(BLL,DAL,MODEL,DBUtility)——确定。
这时不能连接数据库,需要编写一个App.config文件,App.config是通过在项目名称上右键,然后添加新项,在弹出来的对话框中,找到“应用程序配置文件”,选中,确定就会添加进去了,App.config文件中的代码如下:
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <appSettings> 4 <add key="ConStringEncrypt" value="false"/> 5 <!--数据库连接字符串--> 6 <add key="ConnectionString" value="Data Source=WIN7-20140110BZ\SQLEXPRESS;database = TakeSendMisCSDB;Integrated security = true" /> 7 </appSettings> 8 </configuration> 9 10 //Data Source=自己的数据库服务名称 11 //database=数据库名,此项目数据库我命名为TakeSendMisCSDB
(三)基本信息管理的界面设计与代码实现
以基本信息下的产品分类管理为例进行展开,
界面设计如下:(主要包括Button,panel,TextBox和DataGridView等控件)
代码实现,首先定义几个全局变量:
//定义静态变量实例化产品类别数据库逻辑层,进而进行增删改查操作 private static TSM.BLL.CK_ProductType m_bllCK_ProductType = new TSM.BLL.CK_ProductType(); //标记是修改还是添加 public string m_strOperationType = ""; //定义主键的字段 private int m_CK_ProductTypeID;
双击[显示全部]触发buttonShowAll_Click事件,程序代码如下:
1 private void ShowAllRecords(string strWhere) 2 { 3 this.dataGridView1.DataSource = null; 4 DataSet ds = m_bllCK_ProductType.GetList(strWhere);//用到CK_ProductType的BLL业务逻辑层的GetList方法,间接调用DAL层的GetList方法 5 if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) 6 { 7 this.dataGridView1.DataSource = ds.Tables[0]; 8 this.dataGridView1.Columns[0].Visible = false; 9 } 10 } 11 12 private void buttonShowAll_Click(object sender, EventArgs e) 13 { 14 ShowAllRecords(m_strWhere);//调用自己编写的显示全部记录方法 15 }
运行程序则可以查看到产品种类的所有信息,开始由于数据库里没有信息,检查程序运行结果,可以先在数据库的CK_ProductType表中插入几条信息。显示结果如下图:
属性列是英文,看起来不直观,不能满足用户需求。可以跟踪代码的GetList方法,快捷键F12,一层一层的改写DAL层,BLL层的GetList方法,说是改写其实就是Select语句的重命名,就把DAL层代码展示如下:
1 /// <summary> 2 /// 获得数据列表 3 /// </summary> 4 public DataSet GetList(string strWhere) 5 { 6 StringBuilder strSql=new StringBuilder(); 7 strSql.Append("select CK_ProductTypeID as 产品类别编号,CK_ProductTypeName as 产品类别名称"); 8 strSql.Append(" FROM CK_ProductType "); 9 if(strWhere.Trim()!="") 10 { 11 strSql.Append(" where "+strWhere); 12 } 13 return DbHelperSQL.Query(strSql.ToString()); 14 } 15 //就是添加了中文别名
运行结果图如下:
今天就做到这里,后续会添加其它按钮事件的实现和其它设计模块的编写,写的很简单,但是我认为说明挺详细,希望和有志之士共同进步,也希望博友多多提出宝贵意见和多多鼓励。