实验八 数据库应用
一、实验目的:
? ? ? ? ? ? ? ? ?
了解ADO.NET 结构 了解ADO.NET 的组件
使用Command 对象和 Connection 对象 使用ADO.NET 进行事务处理 使用Command和Connection对象。 多表查询的应用。 主从关系表
熟练应用BindingSource和BindingNavigator控件。 熟练使用DataAdapter和DataReader。
二、实验内容:
实现用ADO.NET对数据库的操作。
检索操作数据
三、实训步骤:
1:用command和Connection实现系统登录界面 问题
一般的程序都需要登录后才能使用,登录时应提供用户名、密码和身份类型。 问题说明
本程序采用SqlServer做为数据库。数据库中有一张表名为:recordInfo,用来记录注册用户的信息;表中有3个字段,分别是id(用户名),pwd(密码),type(类型)。 分析
首先使用以下命名空间: using System.Data;
using System.Data. SqlClient;
用以下方式创建Connection和Command对象,以便在.NET中操作SqlServer数据库。 // 创建Connection 对象
string connString = \ID=sa;pwd=sa\
SqlConnection connection = new SqlConnection(connString); // 创建Command 对象
SqlCommand command = new SqlCommand(sql, connection); 由于返回的是记录数,所以使用Command对象的ExecuteScalar ()方法执行该命令。 解决方案:
(1)在SqlServer中创建“MySchool”数据库。添加一个表名为recordInfo的新表,添加3列:id(用户名),pwd(密码),type(类型)。 (2)向新表recordInfo中添加几行示例数据。
(3)打开Visual Studio .NET 2005 IDE并新建一个名为ConnectStr的Windows应用程序项目。 (4)将Form1.cs文件重命名为LoginForm.cs。
1
(5)拖动工具箱中的控件,设计如图所示的窗体。
窗体设计界面
(6)添加两个按钮的Click事件,程序代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace ConnectStr { public partial class LoginForm : Form { string name; string type; public LoginForm() { InitializeComponent(); } private void btnLogIn_Click(object sender, EventArgs e) { bool isValidUser = false; // 标识是否为合法用户 // 如果验证通过,就显示相应的用户窗体,并将当前窗体设为不可见 if (ValidateInput()) { // 调用用户验证方法 isValidUser = ValidateUser( cboLogInType.Text, txtLogInId.Text, txtLogInPwd.Text); // 如果是合法用户,显示相应的窗体 2
if (isValidUser) { // 将输入的用户名保存到全局变量中 name = txtLogInId.Text; // 将选择的登录类型保存到全局变量中 type = cboLogInType.Text; ShowUserForm(); // 显示相应用户的主窗体 } } } // 验证用户是否进行了输入和选择 private bool ValidateInput() { if (txtLogInId.Text.Trim() == \ { MessageBox.Show(\请输入用户名\输入提示\MessageBoxButtons.OK, MessageBoxIcon.Information); txtLogInId.Focus(); return false; } else if (txtLogInPwd.Text.Trim() == \ { MessageBox.Show(\请输入密码\输入提示\MessageBoxButtons.OK, MessageBoxIcon.Information); txtLogInPwd.Focus(); return false; } else if (cboLogInType.Text.Trim() == \ { MessageBox.Show(\请选择登录类型\输入提示\MessageBoxButtons.OK, MessageBoxIcon.Information); cboLogInType.Focus(); return false; } else { return true; } } // 验证用户输入的用户名和密码是否正确 3
// 验证的结果有两种情况:通过和不通过,返回值为布尔型 // 不通过的原因可能有多种,在方法的参数中的message字符串,用以标识不通过的情况 public bool ValidateUser(string loginType, string loginId, string loginPwd) { // 创建Connection 对象 string connString = \ID=sa;pwd=sa\ SqlConnection connection = new SqlConnection(connString); int num = 0; // 选员信息的数量 // 查询用的SQL 语句 string sql = \\ pwd='\+ loginPwd + \and type='\+ loginType + \ bool isExist = false; try { connection.Open();// 打开数据库连接 // 创建Command 对象 SqlCommand command = new SqlCommand(sql, connection); // 执行SQL 查询 num = (int)command.ExecuteScalar(); if (num > 0) { isExist = true; } else { MessageBox.Show(\数据库中无此记录!\提示信息\MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { // 操作出错 MessageBox.Show(\连接数据库出错!\ } finally { // 关闭数据库连接 connection.Close(); } 4
return isExist; } // 根据登录类型,显示相应的窗体 public void ShowUserForm() { MessageBox.Show(\欢迎使用该系统:\ } private void btnCancel_Click(object sender, EventArgs e) { Application.Exit(); } } } (7)保存并构建应用程序。执行应用程序,以验证其工作情况。运行效果如图所示:
登录界面图
2:多表查询的应用 问题
创建一个多表查询应用程序,通过编写代码实现数据库操作功能的一般方法。设已创建了一个名为员工工资的Access数据库,其中包括表employee(存放员工基本信息)和表pay(存放员工工资信息)。 程序设计要求:
1) 要求分别从上述两表中取出一些字段组成多表关联。
2) 要求将“应发工资”字段排列在“奖金”字段之后,“扣税”字段之前。“实发工资”为最后一个字段。
3) 要求程序具有按部门进行筛选记录的功能。 分析
数据库中表的设计如下图所示:
5
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库实验8 数据库应用在线全文阅读。
相关推荐: