数据库课程设计——库存销售管理系统(15页).doc
-数据库课程设计库存销售管理系统-第 12 页 数据库课程设计 实验报告 姓名:黄世增 班级:计算机1403 学号:14116403051. 实验名称 使用和SQL Server 2014开发库存销售管理系统2.实验目的使用Visual Basic 6.0开发工具开发一个库存销售管理系统,该系统采用SQL Server 2014数据库保存库存货物信息,数据库中包括货物出入库信息和商品销售等信息。该系统包括系统登录、货物出入库管理、商品销售管理和商品统计管理等功能,通过这些功能实现对库存中货物信息和销售信息进行管理。系统主要实现以下目标:l 实现系统登录及修改用户密码的功能。l 对库存货物的出入库信息进行管理。l 对商品的销售信息进行管理。l 根据销售日期统计商品的销售数据。3.1 设计数据库建立一个名为“DB_KCGL”的数据库,根据前述的主要功能目标,需要建立3个数据表(关系):(1) 货物的出入库信息:保存货物的出入库信息l id:库存货物信息的编号,主码,建议长度为18的numeric类型。l tb_title:库存货物的名称,建议长度为50的varchar类型。l tb_style:库存货物的类型,建议长度为50的varchar类型。l tb_nums:库存货物的数量,建议为整型int。l tb_values:库存货物的价格,实数类型real。l tb_date:库存货物的入库日期,时间日期类型datetime。l tb_mark:库存货物的备注信息,建议长度为50的varchar类型。(2) 商品的销售信息:保存商品的销售信息l id:商品销售信息的编号,主码,建议长度为18的numeric类型。l tb_title:销售商品的名称,建议长度为50的varchar类型。l tb_nums:建议为整型int。l tb_values:销售商品的总价,浮点类型float。l tb_date:商品销售的日期,时间日期类型datetime。(3) 系统用户的信息:保存系统用户的信息l id:用户信息的编号,主码,建议长度为18的numeric类型。l tb_name:用户名称,建议长度为50的varchar类型。l tb_pas:用户的密码信息,建议长度为50的varchar类型。3.2 设计连接数据库的模块需要首先建立一个连接数据库的模块,这样应用程序才能与数据库中的数据表取得连接,将数据信息从数据表中读出到应用程序中或通过应用程序保存到数据表中。为此,建立一个负责数据库连接的模块如下:(1) 选择“工程”菜单下的“添加模块”命令,在应用程序中添加一个模块Module1。(2) Module1模块的实现如下:'数据连接模块Public MyStrs As StringPublic DB_AdoRs As New ADODB.Recordset '后添加一个记录集对象Public DB_AdoRs1 As New ADODB.Recordset '后添加一个记录集对象Public DB_AdoRs2 As New ADODB.Recordset '后添加一个记录集对象Public Function Cnn() As ADODB.Connection '定义连接字符串函数 Cnn.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DB_KCGL;Data Source=."End FunctionPublic Sub Main() MyStrs = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DB_KCGL;Data Source=." '显示系统登录窗体 Form1.ShowEnd Sub(3) 这里需要将程序的入口设置为模块中的Main()函数。选择“工程”菜单下的“工程属性”命令,在“通用”标签中进行相应设置。3.3 系统登陆模块设计在本库存销售管理系统中,只有授权用户(管理员)可以登录并进行库存和销售信息管理与维护,因此需要一个系统登录界面,防止非法用户登录到系统中,从而保证了应用程序的安全性和可靠性。3.3.1 登录窗体设计设计如下的登录窗体,在窗体中要添加一个ADO控件,命名为Adodc1。(1) 登录窗体启动之后,需要将系统用户的信息数据表中的所有用户名称信息添加到窗体的ComboBox控件的Op_Name属性中,即实现在“操作员名称”下拉列表中可以出现系统当前的操作员信息,供登录用户选择。Private Sub Form_Load()'使用代码连接数据库与数据表 Adodc1.ConnectionString = MyStrs Adodc1.RecordSource = "select * from 系统的用户信息" If Adodc1.Recordset.RecordCount > 0 Then Op_Name.Clear '在添加数据时,首先清空控件中的内容 Do While Adodc1.Recordset.EOF = False '将操作员信息添加到下拉列表框控件当中 Op_Name.AddItem (Adodc1.Recordset.Fields("tb_name") Loop End IfEnd Sub(2) 当用户在“操作员名称”下拉列表中选择操作员的名称,并且在“操作员密码”文本框输入正确的密码之后,单击“确定”按钮将登录到系统中,否则将无法登录系统。具体实现如下:Private Sub Cmd_Ok_Click() Adodc1.RecordSource = "select * from Tb_User where tb_name ='" & Op_Name.Text & "'" If Adodc1.Recordset.RecordCount > 0 Then MPassword = Adodc1.Recordset.Fields("tb_pas") If Txt_Password.Text = MPassword Then '判断数据的密码是否正确 Frm_Main.StatusBar1.Panels.Item(2).Text = "当前操作员: " + Adodc1.Recordset.Fields("tb_name") Frm_Main.Show '通过身份验证则显示主窗体,登录到系统当中 Unload Me Else MsgBox "密码不正确,请您确认后重新输入", , "提示信息" Txt_Password.Text = "" End If Else MsgBox "对不起 没有此用户的信息", , "提示信息" Op_Name.Text = "" Txt_Password.Text = "" End IfEnd Sub3.4 系统主界面的实现3.4.1 主界面设计(1) 在工程中添加一个窗体,将窗体命名为Frm_main,将窗体Caption属性设置为“库存管理系统”。(2) 添加一个Toolbar控件,工具栏由“修改密码”、“库存管理”、“商品销售”、“销售统计”和“退出系统”5个按钮组成。(3) 添加一个StatusBar控件,使得状态栏中能够显示登录操作员姓名和当前系统时间等信息。(4) 在主窗体上添加一个时钟控件,用于显示系统当前日期和时间信息。(5) 在主窗体上添加一个Label标签控件,将其Caption属性设置为“库存管理系统”。3.4.2 代码实现(1) 窗体启动时,在窗体的状态栏中将显示当前系统的日期信息:Private Sub Form_Load() StatusBar1.Panels.Item(3).Text = Format(Now, "yyyy年mm月dd日")End Sub(2)单击窗口工具栏按钮时,将会调用系统的各个子功能模块:Private Sub Toolbar1_ButtonClick(ByVal Button As MSComctlLib.Button) Case 1 Frm_Pas.Show '修改密码 Case 2 Frm_Inout.Show '库存管理 Case 3 Frm_Sale.Show '商品销售 Case 4 Frm_Stat.Show '销售统计 Case 5 End '退出系统 End SelectEnd Sub(3) 在时钟控件的Timer事件中添加如下代码,实现在状态栏中显示当前系统时间的功能:Private Sub Timer1_Timer() StatusBar1.Panels.Item(4).Text = TimeEnd Sub(4) 再向状态栏中添加公司名称,操作员和公司网址信息。系统主窗体的运行界面如下图所示:3.5 出入库管理模块的设计与实现该模块主要是记录和维护库存中的货物信息,其中包括对货物信息的删除、修改和保存等方面的功能。3.5.1 窗体界面设计(1) 在工程中添加一个窗体,命名为Frm_Inout,将窗体Caption设置为“出入库管理”。(2) 在窗体上添加相应的控件,如下图所示。(3) 通过“工程”菜单下的“部件”命令将DataGrid数据表格控件添加到工具箱中,然后在窗体上添加1个数据表格控件DataGrid1。(4) 在窗体上添加一个ADO控件Adodc1,同时将DataGrid1的数据源属性DataSource设置为Adodc1。3.5.2 程序代码实现(1) 窗口载入时,将数据库中商品表数据读出。Private Sub Form_Load() Adodc1.ConnectionString = MyStrs Adodc1.RecordSource = "select * from 货物的出入库信息 order by id" AddTitleEnd Sub其中,AddTitle函数用于向DataGrid1添加表头,实现如下:'添加数据库字段标题的事件过程Private Sub AddTitle() DataGrid1.Columns.Item(0).Caption = "编号" DataGrid1.Columns.Item(1).Caption = "名称" DataGrid1.Columns.Item(2).Caption = "类型" DataGrid1.Columns.Item(3).Caption = "数量" DataGrid1.Columns.Item(4).Caption = "单价" DataGrid1.Columns.Item(5).Caption = "入库日期" DataGrid1.Columns.Item(6).Caption = "备注"End Sub(2)点击“添加”按钮,清空编辑框,让用户输入新的待添加内容Private Sub Command1_Click()'清空文本框中的内容 Text1.Text = "" Text2.Text = "" Text3.Text = "" Text4.Text = "" Text5.Text = "" Text6.Text = ""End Sub(3)点击“删除”按钮,删除选中当前记录。Private Sub Command2_Click()'删除库存信息 c = MsgBox("确认要删除该信息吗", 17, "提示信息") If c = vbOK Then '如果有错误则执行错误处理 On Error Resume Next Set DB_AdoRs = Cnn.Execute("Delete from 货物的出入库信息 where id='" + Text1.Text + "'")MsgBox "数据删除成功", 64, "提示信息" '删除后刷新数据信息 Adodc1.RecordSource = "select * from 货物的出入库信息 order by id" AddTitle End If Text1.Text = "" Text2.Text = "" Text3.Text = "" Text4.Text = "" Text5.Text = "" Text6.Text = ""End Sub(4)点击“修改”按钮,修改当前的记录信息。Private Sub Command3_Click()'修改库存信息 c = MsgBox("确认修改信息吗", 33, "提示信息") If c = vbOK Then ' On Error Resume Next Set DB_AdoRs = Cnn.Execute("UPDATE 货物的出入库信息 SET tb_title='" + Text2 + "',tb_style='" + Text3 + "',tb_nums='" + Text4 + "',tb_values='" _ + Text5 + "',tb_mark='" + Text6 + "' where id='" + Text1.Text + "'") MsgBox "数据修改成功", 64, "提示信息" Adodc1.RecordSource = "select * from 货物的出入库信息 order by id" AddTitle End IfEnd Sub(5)点击“保存”按钮,将保存文本框中输入的货物信息。Private Sub Command4_Click()If Text1.Text = "" Or Text2.Text = "" Or Text3.Text = "" Or Text4.Text = "" Or Text5.Text = "" Or Text6.Text = "" Then MsgBox "保存的数据信息不能为空", 48, "提示信息" Else DB_AdoRs.Open "select * from 货物的出入库信息 where tb_title='" + Text2.Text + "'", Cnn, adOpenKeyset If DB_AdoRs.RecordCount > 0 Then '判断要保存的信息是否已经存在 MsgBox "该货物信息信息已经存在", 48, "提示信息" Else c = MsgBox("确认保存信息吗", 33, "提示信息") If c = vbOK Then '如果选择的是“确定”按钮则保存信息 Set DB_AdoRs = Cnn.Execute("insert into 货物的出入库信息 values(" & Text1 & ",'" & Text2 & "','" & Text3 & "'," & Text4 & "," _ & Text5 & ",'" & Date & "','" & Text6 & "')") MsgBox "信息保存成功", 64, "提示信息" Else End If '保存数据后刷新数据信息 Adodc1.RecordSource = "select * from 货物的出入库信息 order by id" AddTitle End If End IfEnd Sub(6)点击退出,销毁当前窗口。Private Sub Command6_Click() Unload MeEnd Sub(7) 单击DataGrid1中的相应记录,会在窗体左侧的各个编辑框中显示相应的内容。Private Sub DataGrid1_Click()'On Error Resume Next If Adodc1.Recordset.RecordCount > 0 Then Text1.Text = Adodc1.Recordset.Fields(0) Text2.Text = Adodc1.Recordset.Fields(1) Text3.Text = Adodc1.Recordset.Fields(2) Text4.Text = Adodc1.Recordset.Fields(3) Text5.Text = Adodc1.Recordset.Fields(4) Text6.Text = Adodc1.Recordset.Fields(6) End IfEnd Sub 窗体的运行界面如下图所示:3.6 商品销售模块的设计与实现在“商品销售管理”窗口中的“销售商品”下拉列表中选择要销售的商品之后,该商品的基本信息将显示在窗体中相应的文本框中,在输入销售数量和实收金额后,单击“确认销售”按钮完成销售商品的操作。3.6.1 窗体界面设计(1) 在工程中添加一个窗体,命名为Frm_Sale,将Caption属性设置为“商品销售管理”。(2)在窗体上添加相应的控件,如下图所示。(3)通过“工程”菜单下的“部件”命令将DataGrid数据表格控件添加到工具箱中,然后在窗体上添加1个数据表格控件DataGrid1。(4)在窗体上添加2个ADO控件Adodc1和Adodc2,同时将DataGrid1的数据源属性DataSource设置为Adodc2。3.6.2 程序代码实现(1) 窗体启动时,将商品库存中的货物名称信息添加到下拉列表中,然后再将商品销售的数据信息显示在DataGrid1控件中。Private Sub Form_Load() Adodc2.ConnectionString = MyStrs Adodc2.RecordSource = "select * from 商品的销售信息 order by id" AddTitle '使用代码连接数据库与数据表 Adodc1.ConnectionString = MyStrs Adodc1.RecordSource = "select * from 货物的出入库信息" If Adodc1.Recordset.RecordCount > 0 Then Combo1.Clear '在添加数据时,首先清空控件中的内容 Do While Adodc1.Recordset.EOF = False '将货物出入库信息添加到下拉列表框控件当中 Combo1.AddItem (Adodc1.Recordset.Fields("tb_title") Loop End IfEnd SubPrivate Sub AddTitle() DataGrid1.Columns.Item(0).Caption = "编号" DataGrid1.Columns.Item(1).Caption = "名称" DataGrid1.Columns.Item(2).Caption = "销售数量" DataGrid1.Columns.Item(3).Caption = "销售总价" DataGrid1.Columns.Item(4).Caption = "销售日期"End Sub(2) 选择下拉列表中的商品之后,该商品的详细信息将显示在窗体中相对应的文本框中。'商品详细信息Private Sub Combo1_Click()Adodc1.RecordSource = "select * from 货物的出入库信息 where tb_title ='" & Combo1.Text & "'" If Adodc1.Recordset.RecordCount > 0 Then Text1.Text = Adodc1.Recordset.Fields("id") Text2.Text = Adodc1.Recordset.Fields("tb_style") Text3.Text = Adodc1.Recordset.Fields("tb_nums") Text4.Text = Adodc1.Recordset.Fields("tb_values") Text5.Text = Adodc1.Recordset.Fields("tb_mark") End If '清空文本框中的内容 Text6.Text = "" Text7.Text = "" Text8.Text = "" Text9.Text = ""End Sub(3) 单击“确认销售”按钮,将完成销售商品的操作。(4) 当用户输入销售数量后并将输入焦点移开后,需要实时更新商品详细信息中的数量信息,即在原有数量的基础上减去用户输入的销售数量。(3)和(4)的代码:Private Sub Command1_Click() Adodc1.ConnectionString = MyStrs Adodc1.RecordSource = "select * from 货物的出入库信息 where tb_title ='" & Combo1.Text & "'" Dim a As Integer Dim b As Integer a = Val(Text7.Text) b = Val(Text4.Text * Text6.Text) If Text6.Text = "" Or Text7.Text = "" Then MsgBox "请补全信息!", 64, "提示信息" Else If a < b Then MsgBox "信息输入有误,请重新输入!", 64, "提示信息" Else Set DB_AdoRs = Cnn.Execute("insert into 商品的销售信息 values('" & Adodc2.Recordset.RecordCount + 1 & "','" & Combo1.Text & "','" & Text6 & "'," & Text8 & ",'" & Date & "')") Set DB_AdoRs = Cnn.Execute("UPDATE 货物的出入库信息 SET tb_nums = '" & Text3 & "' where id= '" & Text1 & "'") MsgBox "销售成功!", 64, "提示信息" Adodc2.RecordSource = "select * from 商品的销售信息 " AddTitle End If End IfEnd Sub 窗体的运行结果如下图所示:3.7 销售统计模块的设计与实现该模块主要实现统计商品销售信息的功能。选择商品的销售日期和销售商品的名称之后,在下面的DataGrid数据表格控件中就会显示查询的数据结果信息,同时统计出销售商品的总数量和总价格。3.7.1 窗体界面设计(1) 添加一个窗体,命名为Frm_Stat,将窗体的Caption属性设置为“销售统计”。(2) 在窗体上添加控件情况如后图。(3) 在窗体上添加一个数据表格控件DataGrid1。(4) 在窗体上添加2个ADO控件Adodc1和Adodc2,同时将DataGrid1的数据源属性DataSource设置为Adodc2。销售统计窗体的设计界面如下图所示。3.7.2 程序代码实现(1) 在启动窗体时,将货物的名称信息添加到下拉列表控件中,然后再将商品销售的数据信息显示在表格控件中。(2) 在日期时间控件DTPicker1的Change事件下添加如下的程序代码,实现根据选择条件统计商品销售信息的功能。(3) 在日期Combo1控件的Click事件下添加如下的程序代码,实现根据选择条件统计商品销售信息的功能。Private Sub Combo1_Click()Adodc2.RecordSource = "select * from 商品的销售信息 where tb_title ='" & Combo1.Text & "' and tb_date ='" & DTPicker1.Value & "' " AddTitle Dim n, m As Integer n = 0 m = 0 n = n + Adodc2.Recordset.Fields(2).Value m = m + Adodc2.Recordset.Fields(3).Value Loop Text1.Text = n Text2.Text = mEnd SubPrivate Sub DTPicker1_Change()Adodc2.RecordSource = "select * from 商品的销售信息 where tb_title ='" & Combo1.Text & "' and tb_date ='" & DTPicker1.Value & "' " AddTitle Dim n, m As Integer n = 0 m = 0 n = n + Adodc2.Recordset.Fields(2).Value m = m + Adodc2.Recordset.Fields(3).Value Loop Text1.Text = n Text2.Text = mEnd SubPrivate Sub Form_Load() Adodc2.ConnectionString = MyStrs Adodc2.RecordSource = "select * from 商品的销售信息 where tb_title ='" & Combo1.Text & "' and tb_date ='" & DTPicker1.Value & "' order by id" AddTitle'使用代码连接数据库与数据表 Adodc1.ConnectionString = MyStrs Adodc1.RecordSource = "select * from 货物的出入库信息 " If Adodc1.Recordset.RecordCount > 0 Then Combo1.Clear '在添加数据时,首先清空控件中的内容 Do While Adodc1.Recordset.EOF = False '将商品销售信息添加到下拉列表框控件当中 Combo1.AddItem (Adodc1.Recordset.Fields("tb_title") Loop End IfEnd SubPrivate Sub AddTitle() DataGrid1.Columns.Item(0).Caption = "编号" DataGrid1.Columns.Item(1).Caption = "名称" DataGrid1.Columns.Item(2).Caption = "销售数量" DataGrid1.Columns.Item(3).Caption = "销售总价" DataGrid1.Columns.Item(4).Caption = "销售日期"End Sub窗体的运行结果如下:3.8 修改密码模块的设计与实现3.8.1 窗体界面设计(1) 添加一个窗体,命名为Frm_Pas,将Caption属性设置为“密码修改”。(2)在窗体上加入一个ADO控件Adodc1。(3)设计窗体的界面如下图。3.8.2 程序代码实现(1) 载入窗口时连接操作员信息数据表Private Sub Form_Load()'使用代码连接数据库与数据表 Adodc1.ConnectionString = MyStrs Adodc1.RecordSource = "select * from 系统用户的信息"End Sub(2) 当点击“确定”按钮时,首先判断用户输入的用户名和原密码信息是否正确,如果正确并且两次输入的新密码一致,就将用户密码修改为新密码。Private Sub Command1_Click()Adodc1.RecordSource = "select * from 系统用户的信息 where tb_name ='" & Text1.Text & "'" If Text1.Text = "" Then MsgBox "请输入用户名!", 48, "提示信息" Else If Text2.Text = "" Then MsgBox "请输入旧密码!", 48, "提示信息" Else If Text3.Text = "" Then MsgBox "请输入新密码!", 48, "提示信息" Else If Text4.Text = "" Then MsgBox "请确认新密码!", 48, "提示信息" Else If Adodc1.Recordset.RecordCount > 0 Then MPassword = Adodc1.Recordset.Fields("tb_pas") If Text2.Text = MPassword And Text3.Text = Text4.Text Then '判断数据的密码是否正确 Set DB_AdoRs = Cnn.Execute("UPDATE 系统用户的信息 SET tb_pas='" + Text3.Text + "' where tb_name='" + Text1.Text + "'") MsgBox "密码已修改!", 48, "提示信息" Unload Me Else If Text2.Text <> MPassword Then MsgBox "密码不正确,请您确认后重新输入!", 64, "提示信息" Text2.Text = "" Text3.Text = "" Text4.Text = "" Else MsgBox "两次输入的新密码不一致,请重新输入!", 64, "提示信息" Text3.Text = "" Text4.Text = "" End