可视化编程技术作业二.doc_第1页
可视化编程技术作业二.doc_第2页
可视化编程技术作业二.doc_第3页
可视化编程技术作业二.doc_第4页
可视化编程技术作业二.doc_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

可视化编程技术作业(二)班级:11网络B班 姓名:张耀东 学号:37一、问题描述:对SQL Server中的Northwind数据库创建一数据库应用程序,该应用程序能够实现浏览每一雇员的相关信息。2、 分析与设计该应用程序涉及到对雇员数据进行显示、操作的用户界面,涉及到读取雇员数据库表中的记录。为了避免对数据库进行反复操作,我们声明方法getAll一次将数据库表中的所有数据读出,并用每条雇员记录创建一个雇员对象将雇员对象添加到ArrayList对象中,这样雇员表中的每条记录以雇员对象的形式暂存在ArrayList对象中,雇员表中的一条对应一个ArrayList列表中的一个雇员对象。然后,我们对ArrayList列表中的雇员对象进行操作,浏览每个雇员的数据。根据以上思路,我们必须声明一个雇员Employee类,以存储雇员记录的数据。雇员数据库表有多少字段,雇员类就应该有多少字段,并在雇员类为每个字段声明属性。同时声明一个Employee类,该类有一个ArrayList对象字段,有getAll方法和获取ArrayList列表中雇员对象的方法(包括包括获取第一条、上一条、下一条、最后一条、添加、修改、删除雇员的方法)。图形界面类与Employee类和EmployeeDA类进行交互,而不直接与数据库进行交互。三、解决方案1、 创建项目和编写Employee类及EmployeeDA类。(1) 创建项目名为Northwind的Windows应用程序。(2) 创建Employee类。向Northwind项目添加Employee类:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Northwind public class Employee private int _EmployeeID; public int EmployeeID get return _EmployeeID; set _EmployeeID = value; private String _LastName; public String LastName get return _LastName; set _LastName = value; private String _FirstName; public String FirstName get return _FirstName; set _FirstName = value; private String _Title; public String Title get return _Title; set _Title = value; private String _TitleOfCourtesy; public String TitleOfCourtesy get return _TitleOfCourtesy; set _TitleOfCourtesy = value; private String _BirthDate; public String BirthDate get return _BirthDate; set _BirthDate = value; private String _HireDate; public String HireDate get return _HireDate; set _HireDate = value; private String _Address; public String Address get return _Address; set _Address = value; private String _City; public String City get return _City; set _City = value; private String _Region; public String Region get return _Region; set _Region = value; private String _PostalCode; public String PostalCode get return _PostalCode; set _PostalCode = value; private String _Country; public String Country get return _Country; set _Country = value; private String _HomePhone; public String HomePhone get return _HomePhone; set _HomePhone = value; private String _Extension; public String Extension get return _Extension; set _Extension = value; private String _Notes; public String Notes get return _Notes; set _Notes = value; private int _ReportsTo; public int ReportsTo get return _ReportsTo; set _ReportsTo = value; private String _photoPath; public String PhotoPath get return _photoPath; set _photoPath = value; public Employee() public Employee(int id, String mLastName, String mFirstName, String mTitle, String mTitleOfCourtesy, String mBirthDate, String mHireDate, String mAddress, String mCity, String mRegion, String mPostalCode, String mCountry, String mHomePhone, String mExtension, int mReportsTo) _EmployeeID = id; _LastName = mLastName; _FirstName = mFirstName; _Title = mTitle; _TitleOfCourtesy = mTitleOfCourtesy; _BirthDate = mBirthDate; _HireDate = mHireDate; _Address = mAddress; _City = mCity; _Region = mRegion; _PostalCode = mPostalCode; _Country = mCountry; _HomePhone = mHomePhone; _Extension = mExtension; _ReportsTo = mReportsTo; public override string ToString() return this.FirstName + + this.LastName; (3) 创建EmployeeDA类。向Northwind项目添加如下EmployeeDA类:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;using System.Collections;namespace Northwind public class EmployeeDA private static ArrayList employess = new ArrayList(); private static int i = 0; private static SqlConnection aConnection; /- private static void GetDBConnection() try string strConn = Data Source=(local);User ID=sa;Password=1234;Initial Catalog=Northwind; aConnection = new SqlConnection(strConn); /aConnection.ConnectionString = strConn; catch(Exception oExcept) MessageBox.Show(oExcept.Message); /- public static ArrayList GetAll() employess.Clear(); SqlCommand oCmd; SqlDataReader oDR = null; String strSQL = SELECT * from Employees; try GetDBConnection(); oCmd = new SqlCommand(); oCmd.Connection = aConnection; oCmd.Connection.Open(); oCmd.CommandText = strSQL; oDR = oCmd.ExecuteReader(); while (oDR.Read() Employee aEmployee = new Employee(); aEmployee.EmployeeID = Int32.Parse(oDREmployeeID.ToString(); aEmployee.LastName = oDRLastName.ToString(); aEmployee.FirstName = oDRFirstName.ToString(); aEmployee.BirthDate = oDRBirthDate.ToString(); aEmployee.HireDate = oDRHireDate.ToString(); aEmployee.Title = oDRTitle.ToString(); aEmployee.TitleOfCourtesy = oDRTitleOfCourtesy.ToString(); aEmployee.Address = oDRAddress.ToString(); aEmployee.Country = oDRCountry.ToString(); aEmployee.Extension = oDRExtension.ToString(); aEmployee.HomePhone = oDRHomePhone.ToString(); aEmployee.Notes = oDRNotes.ToString(); aEmployee.PostalCode = oDRPostalCode.ToString(); aEmployee.Region = oDRRegion.ToString(); if (oDR.IsDBNull(16) aEmployee.ReportsTo = 0; else aEmployee.ReportsTo = Int32.Parse(oDRReportsTo.ToString(); if (oDR.IsDBNull(17) aEmployee.PhotoPath = ; else aEmployee.PhotoPath = oDRPhotoPath.ToString(); employess.Add(aEmployee); catch (Exception oExcept) MessageBox.Show(oExcept.Message); finally CloseSqlDataReader(oDR); CloseConnection(); return employess; /- private static void CloseConnection() if(aConnection != null) aConnection.Close(); /- private static void CloseSqlDataReader(SqlDataReader oDR) if (oDR != null) oDR.Close(); /- public static Employee GetCurrentEmployee() if (employess.Count 0) Employee aEmployee = (Employee)employessi; return aEmployee; else return null; /- public static Employee GetFirstEmployee() Employee aEmployee = new Employee(); if (employess.Count 0) i = 0; aEmployee = (Employee)employessi; return aEmployee; /- public static Employee GetNextEmployee() Employee aEmployee = new Employee(); if (i 0) i = i - 1; aEmployee = (Employee)employessi; return aEmployee; return aEmployee = (Employee)employess0; /- public static Employee GetLastEmployee() Employee aEmployee = new Employee(); if (employess.Count 0) i = employess.Count - 1; aEmployee = (Employee)employessi; return aEmployee; /- public static void AddNew(Employee aEmployee) SqlDataReader oDR = null; string strSQL = INSERT INTO Employees; strSQL += (Address,BirthDate,Country,Extension, + FirstName,LastName,HireDate,; strSQL += HomePhone,Notes,PostalCode,Region, + City,ReportsTo,Title,TitleOfCourtesy); strSQL += VALUES(; strSQL += + aEmployee.Address + + ,; strSQL += + aEmployee.BirthDate + + ,; strSQL += + aEmployee.Country + + ,; strSQL += + aEmployee.Extension + + ,; strSQL += + aEmployee.FirstName + + ,; strSQL += + aEmployee.LastName + + ,; strSQL += + aEmployee.HireDate + + ,; strSQL += + aEmployee.HomePhone + + ,; strSQL += + aEmployee.Notes + + ,; strSQL += + aEmployee.PostalCode + + ,; strSQL += + aEmployee.Region + + ,; strSQL += + aEmployee.City + + ,; strSQL += aEmployee.ReportsTo + ,; strSQL += + aEmployee.Title + + ,; /strSQL += + aEmployee.getPhotoPath() + + ,; strSQL += + aEmployee.TitleOfCourtesy + + ); try GetDBConnection(); SqlCommand oCmd = new SqlCommand(strSQL, aConnection); oCmd.Connection.Open(); oCmd.ExecuteNonQuery(); i = employess.Count; catch (Exception oExcept) MessageBox.Show(oExcept.Message); finally CloseSqlDataReader(oDR); CloseConnection(); /- public static void Update(Employee aEmployee) SqlDataReader oDR = null; Employee currentEmployee = GetCurrentEmployee(); int id = currentEmployee.EmployeeID; String strSQL = UPDATE Employees SET; strSQL += Address= + + aEmployee.Address + + ,; strSQL += BirthDate= + + aEmployee.BirthDate + + ,; strSQL += Country= + + aEmployee.Country + + ,; strSQL += Extension= + + aEmployee.Extension + + ,; strSQL += FirstName= + + aEmployee.FirstName + + ,; strSQL += LastName= + + aEmployee.LastName + + ,; strSQL += HireDate= + + aEmployee.HireDate + + ,; strSQL += HomePhone= + + aEmployee.HomePhone + + ,; strSQL += Notes= + + aEmployee.Notes + + ,; /strSQL += + aEmployee.PhotoPath + + ,; strSQL += PostalCode= + + aEmployee.PostalCode + + ,; strSQL += Region= + + aEmployee.Region + + ,; strSQL += City= + + aEmployee.City + + ,; strSQL += ReportsTo= + aEmployee.ReportsTo + ,; strSQL += Title= + + aEmployee.Title + + ,; strSQL += TitleOfCourtesy= + + aEmployee.TitleOfCourtesy + ; strSQL += WHERE EmployeeID= + id; try GetDBConnection(); SqlCommand oCmd = new SqlCommand(strSQL, aConnection); oCmd.Connection.Open(); oCmd.ExecuteNonQuery(); catch (Exception oExcept) MessageBox.Show(oExcept.Message); finally CloseSqlDataReader(oDR); CloseConnection(); /- public static void Delete(Employee aEmployee) SqlDataReader oDR = null; Employee currentEmployee = GetCurrentEmployee(); int id = currentEmployee.EmployeeID; String strSQL = DELETE Employees; strSQL += WHERE EmployeeID= + id; try GetDBConnection(); SqlCommand oCmd = new SqlCommand(strSQL, aConnection); oCmd.Connection.Open(); oCmd.ExecuteNonQuery(); if (i 0) i = i - 1; catch (Exception oExcept) MessageBox.Show(oExcept.Message); finally CloseSqlDataReader(oDR); CloseConnection(); /- public static void UpdatePhoto(String filename) SqlDataReader oDR = null; Employee aEmployee = (Employee)employessi; int id = aEmployee.EmployeeID; String strSQL = UPDATE Employee SET ; strSQL += PhotoPath= + + filename + ; strSQL += WHERE EmployeeID= + id; try GetDBConnection(); SqlCommand oCmd = new SqlCommand(strSQL, aConnection); oCmd.Connection.Open(); oDR = oCmd.ExecuteReader(); catch (Exception oExcept) MessageBox.Show(oExcept.Message); finally CloseSqlDataReader(oDR); CloseConnection(); /- public static void DeletePhoto() SqlDataReader oDR = null; Employee aEmployee = (Employee)employessi; int id = aEmployee.EmployeeID; String strSQL = UPDATE Employee SET ; strSQL += PhotoPath= + + + ; strSQL += WHERE EmployeeID= + id; try GetDBConnection(); SqlCommand oCmd = new SqlCommand(strSQL, aConnection); oCmd.Connection.Open(); oDR = oCmd.ExecuteReader(); catch (Exception oExcept) MessageBox.Show(oExcept.Message); finally CloseSqlDataReader(oDR); CloseConnection(); 2、 编写用户界面事件处理程序创建窗体用户界面(见图10.10)(1) 添加一个名为Form1的【Windows 窗体】(2) 往【Windows 窗体】上添加控件。(3) 设置控件的属性。3、 编写用户界面事件处理程序(1)在From1.cs添加如下代码using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace Northwind public partial class Form1 : Form public Form1() InitializeComponent(); private void Form1_Load(object sender, EventArgs e) RefreshValues(); private void ShowEmployee

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论