1、专业:计算机科学与技术指导老师:李伟班级:计092学号:099074054姓名:陶嘉莉 实验五异常处理实验目的1. 理解异常的产生过程和异常处理的概念2. 掌握C#异常处理的方法实验内容异常的产生是由于代码执行过程中满足了异常的条件而使程序无法正常运行下去,捕获异常实验try-catch语句。还可以通过throw语句无条件抛出异常。 输入1365之间的数字,判断它是一年中的几月几日。增加的代码 Console.Write(Please input the year:); string st = Console.ReadLine(); int yearNum = int.Parse(st); b
2、ool isleapyear = yearNum % 4 = 0 & yearNum % 100 != 0 | yearNum % 400 = 0; int maxDayNum = isleapyear ? 366 : 365; Console.Write(Please input a day number between 1 and 0:, maxDayNum);运行结果 实验四-界面设计与文件存取1实验目的(1)练习界面功能的设计方法。(2)练习文本文件的存取方法。(3)练习对话框的使用方法。(4)练习菜单的设计方法。(5)练习工具条和状态条的设计方法。(6)练习一个窗体调用另外一个窗体以
3、及窗体间参数传递的方法。2实验内容重新设计和处理与个人存取款相关的业务,假定处理业务仅有活期存款一种,而且业务处理规定与实验三的活期存款业务相同。要求功能界面中包括菜单条、工具条和状态条。3.实验步骤(1)创建一个名为MenuBankCustoms 的Windows 应用程序项目。重命名Form1.cs 为FormMain.cs,然后在此窗体上设计个人存款业务处理的功能界面。(2)向项目中添加一个类文件Account.cs,提供以下属性。AccountName:存款人姓名AccountNumber:存款人帐号AccountBalance:帐户余额。同时在该类中提供下面的方法。Deposit方法
4、:负责实现存款功能。Withdraw方法:负责实现取款功能。(3)向项目中添加一个类文件Customers.cs,用于处理所有帐户的信息。类中提供下面的方法。Load 方法:负责从文件bankdata.txt 中将所有存款人的信息读入到Account 类型的数组中,并返回该数组。Save 方法:负责将所有存款人的信息保存到bankdata.txt 文件中。该方法返回一个布尔型的值,表明保存是否成功。CreateAccount 方法:负责创建新帐户,该方法返回新建的帐户。(4)向项目中添加一个Windows 窗体文件FormShowAccountMessage.cs,在该窗体中显示所有帐户信息u
5、sing lab4b1;using lab4b2;namespace lab4b public partial class Form1 : Form public Form1() InitializeComponent(); private void toolStripLabel3_MouseEnter(object sender, EventArgs e) label3.Text = 将内存中所有用户的信息显示出来; private void toolStripLabel3_MouseLeave(object sender, EventArgs e) label3.Text = ; priv
6、ate void toolStripLabel1_MouseEnter(object sender, EventArgs e) label3.Text = 打开内存中所有用户的信息; private void toolStripLabel1_MouseLeave(object sender, EventArgs e) label3.Text = ; private void toolStripLabel2_MouseEnter(object sender, EventArgs e) label3.Text = 保存所有用户信息; private void toolStripLabel2_Mou
7、seLeave(object sender, EventArgs e) label3.Text = ; private void button1_MouseEnter(object sender, EventArgs e) label3.Text = 在已有用户上存款或者新开户并存款; private void button1_MouseLeave(object sender, EventArgs e) label3.Text = ; private void button2_MouseEnter(object sender, EventArgs e) label3.Text = 在已有用户上
8、进行利息结算并取款; private void button2_MouseLeave(object sender, EventArgs e) label3.Text = ; private void listBox1_MouseEnter(object sender, EventArgs e) label3.Text = 所有用户的信息记录; private void listBox1_MouseLeave(object sender, EventArgs e) label3.Text = ; private void toolStripButton1_Click(object sender,
9、 EventArgs e) Form1_Load(sender, e); Customs cs = new Customs(); Account at0 = new Account50; at0 = cs.Load(); for (int i = 0; string.Compare(at0i.AccountName, ) != 0; i+) listBox1.Items.Insert(0, at0i.AccountName + t + at0i.AccountNumber + t + at0i.AccountBalance.ToString(); int j; for (j = 0; j at
10、.Length; j+) if (string.Compare(atj.AccountName, ) = 0) break; for (int i = 0; i at0.Length; i+) ati + j = at0i; private void button1_Click(object sender, EventArgs e) int i, items = -1; double dollars = double.Parse(textBox2.Text); string Name = textBox1.Text; for (i = 0; i at.Length; i+) if (strin
11、g.Compare(ati.AccountName, ) = 0) break; else if (string.Compare(ati.AccountName, Name) = 0) items = i; break; if (items = -1) int j; do int Num = int.Parse(Account.newAccountNum); Num+; if (Num / 10 = 0) Account.newAccountNum = 000 + Num.ToString(); else if (Num / 100 = 0) Account.newAccountNum = 0
12、0 + Num.ToString(); else if (Num / 1000 = 0) Account.newAccountNum = 0 + Num.ToString(); else Account.newAccountNum = Num.ToString(); for (j = 0; string.Compare(atj.AccountNumber, Account.newAccountNum) != 0 & string.Compare(atj.AccountName, ) != 0; j+) ; while (string.Compare(atj.AccountName, ) !=
13、0); ati.AccountName = Name; ati.AccountNumber = Account.newAccountNum; ati.Deposit(dollars); listBox1.Items.Insert(0, 开户: + ati.AccountName + t + ati.AccountNumber + t + ati.AccountBalance.ToString(); else listBox1.Items.Insert(0, 存款前: + ati.AccountName + t + ati.AccountNumber + t + ati.AccountBalan
14、ce.ToString(); ati.Deposit(dollars); listBox1.Items.Insert(0, 存款后: + ati.AccountName + t + ati.AccountNumber + t + ati.AccountBalance.ToString(); private void Form1_Load(object sender, EventArgs e) for (int i = 0; i at.Length; i+) ati = new lab4b1.Account(); textBox2.Text = 0; private void 打开ToolStr
15、ipMenuItem_Click(object sender, EventArgs e) toolStripButton1_Click(sender, e); private void 保存ToolStripMenuItem_Click(object sender, EventArgs e) toolStripButton2_Click(sender, e); private void button2_Click(object sender, EventArgs e) string Name = textBox1.Text; double dollars = double.Parse(text
16、Box2.Text); int items = -1; int i; for (i = 0; i at.Length; i+) if (string.Compare(ati.AccountName, ) = 0) break; else if (string.Compare(ati.AccountName, Name) = 0) items = i; break; if (items = -1) listBox1.Items.Insert(0, Error: + Name + 不存在:如果要开户请选择存款按钮); else if (ati.IsWithdraw(dollars) = 0) li
17、stBox1.Items.Insert(0, Error: + ati.AccountName + t + ati.AccountNumber + t + ati.AccountBalance.ToString() + t余额不足); else listBox1.Items.Insert(0, 存款前: + ati.AccountName + t + ati.AccountNumber + t + ati.AccountBalance.ToString(); ati.Withdraw(dollars); listBox1.Items.Insert(0, 存款后: + ati.AccountNa
18、me + t + ati.AccountNumber + t + ati.AccountBalance.ToString(); private void toolStripButton2_Click(object sender, EventArgs e) if (MessageBox.Show(是否将所有用户信息保存在/bankdata.txt中, 保存, MessageBoxButtons.YesNo) = DialogResult.Yes) Customs ct = new Customs(); ct.Save(at0, false); for (int i = 1; string.Com
19、pare(ati.AccountName, ) != 0; i+) ct.Save(ati); private void toolStripButton3_Click(object sender, EventArgs e) f2.Show(); for (int i = 0; string.Compare(ati.AccountName, ) != 0; i+) f2.listBox1.Items.Insert(0, ati.AccountName + t + ati.AccountNumber + t + ati.AccountBalance.ToString(); private void
20、 显示所有账户信息ToolStripMenuItem_Click(object sender, EventArgs e) toolStripButton3_Click(sender, e); private void listBox1_SelectedIndexChanged(object sender, EventArgs e) string str = listBox1.SelectedItem.ToString(); str = str.Substring(0, 3); str = str.Trim(); textBox1.Text = str; class Account public
21、 static string newAccountNum = 0000; private string accountName = ; private string accountNumber = ; private double accountBalance = 0; public double rate1 = 0.003; public double rate2 = 0.006; public Account() accountName = ; accountNumber = ; accountBalance = 0; public Account(string Name, double
22、Balance) accountName = Name; accountNumber = newAccountNum; int Num = int.Parse(newAccountNum); Num+; if (Num / 10 = 0) newAccountNum = 000 + Num.ToString(); else if (Num / 100 = 0) newAccountNum = 00 + Num.ToString(); else if (Num / 1000 = 0) newAccountNum = 0 + Num.ToString(); else newAccountNum =
23、 Num.ToString(); accountBalance = Balance; public void Deposit(double dollars) accountBalance += dollars; public int IsWithdraw(double dollars) if (accountBalance = dollars) return 1; else return 0; else if (accountBalance * (1 + rate2) = dollars) return 1; else return 0; public void Withdraw(double
24、 dollars) if (accountBalance = 500) accountBalance -= -accountBalance * rate1 + dollars; else accountBalance -= -accountBalance * rate2 + dollars; public string AccountName set accountName = value; get return accountName; public string AccountNumber set accountNumber = value; get return accountNumbe
25、r; public double AccountBalance set accountBalance = value; get return accountBalance; class Customs public Account Load() int i; Account At = new Account30; for (i = 0; i At.Length; i+) Ati = new Account(); string str; i = 0; StreamReader reader = new StreamReader(F:bankdata.txt, System.Text.Encodi
26、ng.Default); while (str = reader.ReadLine() != null) string strline = new string3; strline = str.Split( ); Ati.AccountName = strline0; Ati.AccountNumber = strline1; Ati.AccountBalance = double.Parse(strline2); i+; reader.Close(); return At; public bool Save(Account at0, bool append) StreamWriter wri
27、ter = new StreamWriter(F:bankdata.txt, append, System.Text.Encoding.Default); writer.WriteLine(at0.AccountName + + at0.AccountNumber + + at0.AccountBalance.ToString(); writer.Close(); return true; public bool Save(Account at0) StreamWriter writer = new StreamWriter(F:bankdata.txt, true, System.Text.
28、Encoding.Default); writer.WriteLine(at0.AccountName + + at0.AccountNumber + + at0.AccountBalance.ToString(); writer.Close(); return true; 运行结果 ADO.NET 与数据操作1实验目的(1)练习SQL Server 数据库的创建与使用方法。(2)练习存储过程的创建与调用方法。(3)练习简单的统计查询方法。(4)练习绑定DataGridView到数据源的方法。(5)练习绑定其他控件到数据源的方法。(6)练习数据库中图片的显示和存取方法。2实验内容设计一个Win
29、dows应用程序项目,实现简单的银行工作人员基本信息管理,以及利用存储过程实现统计和查询功能。具体要求如下。(1)程序开始运行出现图4-4 所示的主功能以及登录界面,系统功能仅有两种,一种是基本信息管理,另一种是统计男女人数。已知所有操作员和对应的密码已经保存在数据库中的某个表中,只有操作员密码正确时才允许使用系统功能。(2)当操作员选中【基本信息管理】功能时,弹出如图4-5 所示的窗体,可以对基本信息进行记录导航、添加、删除、修改、保存,以及对照片的导入和移除处理。(3)当操作员选中【统计男女人数】功能时,直接弹出一个对话框显示统计结果。3实验步骤(1)创建一个名为BankEmployee
30、的Windows 应用程序项目。重命名Form1.cs 为FormMain.cs,然后在此窗体上设计如图4-4所示的设计界面。(2)在项目中创建一个名为Employee.mdf的数据库,新建两个表和3 个存储过程。BasicTable:该表用于保存职员基本信息。Operator:该表用于保存操作员名称及密码。SelectAllUserNameFromOperator:该存储过程用于填充下拉列表框。SelectOperator:该存储过程用于判断选择的操作员及密码。SimpleQuery:该存储过程用于统计男女人数。(3)完成FormMain.cs 窗体的【进入】、【退出】以及【统计男女人数】功
31、能。(4)向项目中添加一个新窗体FormEmployee.cs,通过简单的拖放操作设计图4-5所示的设计界面,同时将各控件与数据库的对应字段绑定,然后在导航条的下面,添加一个居中显示的Label 控件,用于提示异常以及操作提示等信息,最后修改Program.cs 让其直接运行该窗体,以便快速调试该窗体提供的功能。(5)将FormEmployee.cs 窗体导航条中的【第一个记录】、【上一个记录】、【下一个记录】、【最后一个记录】、【新添】和【删除】按钮的对应属性全部改为“(无)”,例如【第一个记录】对应【MoveFirstItem】属性,【下一个记录】对应【MoveNextItem】属性等,以
32、便自己用代码实现对应功能。(6)实现导航条上对应按钮的功能。(7)实现照片的【导入】和【移除】功能。(8)完成从FormMain.cs 调用FormEmployee.cs 的功能,并修改Program.cs 中的代码让其首先调用FormMain窗体。4.代码实现public partial class FormMain : Form public FormMain() InitializeComponent(); private void FormMain_Load(object sender, EventArgs e) this.selectAllUserNameFromOperatorTa
33、bleAdapter.Fill(this.employeeDataSet1.SelectAllUserNameFromOperator); comboBoxOperator.DataSource = employeeDataSet1.SelectAllUserNameFromOperator; comboBoxOperator.DisplayMember = employeeDataSet1.SelectAllUserNameFromOperator.usernameColumn.ColumnName; private void buttonLogin_Click(object sender,
34、 EventArgs e) selectOperatorTableAdapter1.Fill(employeeDataSet1.SelectOperator, comboBoxOperator.Text, textBoxPassword.Text); if (textBoxPassword .Text != 123456) MessageBox.Show(操作员或密码不正确, , MessageBoxButtons.OK, MessageBoxIcon.Error); groupBox1.Enabled = false; else groupBox1.Enabled = true; private void buttonExit_Click(object sender, EventArgs e) this.Close(); private void radioButtonManage_CheckedChanged(object sender, EventArgs e) if (radioButtonManage.Checked = true) this.Hide(); FormEmployee fm = new FormEmployee(); fm.ShowDialog(); this.Show();