收藏 分销(赏)

实验六使用NET进行数据库访问.doc

上传人:二*** 文档编号:4520019 上传时间:2024-09-26 格式:DOC 页数:7 大小:84.50KB 下载积分:5 金币
下载 相关 举报
实验六使用NET进行数据库访问.doc_第1页
第1页 / 共7页
本文档共7页,全文阅读请下载到手机保存,查看更方便
资源描述
实验报告 课程名称 Web程序设计 实验名称 实验六、ADO.NET进行数据库访问 日期 2014.4.24 学生学号 姓名 班级 实验目的: 1.熟悉ADO.NET数据库访问技术。 2.掌握Connection、Command对象的使用。 3.掌握DataReader、DataAdapter对象操作数据库数据的方法。 4.掌握VS2008中创建数据库的方法。 实验条件: 电脑一台、能上网查阅资料。 实验内容与步骤: 1. 新建名字为“Accessdatabase_ Exercise”的网站。 2. 在网站的App_Data文件夹中,建立数据库“MyDatabase_ Exercise.mdf”。 3. 在该数据库中建立一张职工表,并且添加一些模拟的职工记录。其关系模式如下: Employees(ID,NAME,SEX,AGE,Date of work, of Photo) 4. 在web.config配置文件中,修改“<connectionStrings/>”标记如下。 <connectionStrings> <add name="ConnectionString" connectionString="DataSource=.\SQLEXPRESS; AttachDb\ MyDatabase_ Exercise.mdf;Integrated Security=True;User Instance=True"/> </connectionStrings> 5. 添加一个网页,利用Command对象实现新职工的录入。 6. 添加一个网页,利用Command对象实现删除指定编号的职工记录。 7. 添加一个网页,利用Command对象实现修改指定编号的职工信息。 8. 添加一个网页,利用DataAdapter对象实现查询职工信息,并显示到网页的Label控件上。 源代码: 定义dal类: public class DAL { string con = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; public DAL() { // //TODO: 在此处添加构造函数逻辑 // } // 执行sql操作 public int Edit(string sql) { SqlConnection conn = new SqlConnection(con); SqlCommand cmd = new SqlCommand(sql,conn); SqlDataAdapter da = new SqlDataAdapter(cmd); conn.Open(); int i = cmd.ExecuteNonQuery(); conn.Close(); return i; } public DataTable Select(string sql) { SqlConnection conn = new SqlConnection(con); SqlCommand cmd = new SqlCommand(sql, conn); SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); return dt; } } 3在数据库里建立职工表: 图1 职工表 4添加一个网页,利用Command对象实现新职工的录入: 图2 录入 代码如下: protected void Button1_Click(object sender, EventArgs e) { DAL dal = new DAL(); int id; string name; string sex; int age; string dow; string phtot; FuZhi(out id, out name, out sex, out age, out dow, out phtot); string cmdMake = string.Format("insert into Employees values({0},{1},{2},{3},{4},{5})", id, name, sex, age, dow, phtot); try { if (dal.Edit(cmdMake) > 0) { msg.Text = "添加成功"; } else msg.Text = "添加失败"; } catch (Exception ex) { msg.Text = "错误信息:" + ex.Message; } } private void FuZhi(out int id, out string name, out string sex, out int age, out string dow, out string phtot) { id = Convert.ToInt32(txtId.Text); name = txtName.Text; sex = txtSex.Text; age = Convert.ToInt32(txtAge.Text); dow = txtDow.Text; phtot = txtPht.Text; } 5利用Command对象实现删除指定编号的职工记录。 图3 删除 源代码: protected void btndel_Click(object sender, EventArgs e) { DAL dal = new DAL(); int id = Convert.ToInt32(TextBox1.Text); string cmdMake = string.Format("delete from Employees where ID={0}", id); try { if (dal.Edit(cmdMake) > 0) { msg.Text = "删除成功"; } else msg.Text = "删除失败"; } catch (Exception ex) { msg.Text = "错误信息:" + ex.Message; } } 6利用Command对象实现修改指定编号的职工信息。 图4 修改 源代码: protected void Button1_Click(object sender, EventArgs e) { DAL dal = new DAL(); int id = Convert.ToInt32(txtId.Text); string name = txtName.Text; string sex = txtSex.Text; int age = Convert.ToInt32(txtAge.Text); string dow = txtDow.Text; string phtot = txtPht.Text; string cmdMake = string.Format("update Employees set NAME={0},SEX={1},AGE={2},[Date of work]={3},[ of Photo]={4}", name, sex, age, dow, phtot, id); try { if (dal.Edit(cmdMake) > 0) { msg.Text = "修改成功"; } else msg.Text = "修改失败"; } catch (Exception ex) { msg.Text = "错误信息:" + ex.Message; } } 7利用DataAdapter对象实现查询职工信息,并显示到网页的Label控件上。 图6 查询 源代码: protected void Button1_Click(object sender, EventArgs e) { DAL dal = new DAL(); string cmdMake = string.Format("select * from Employees where ID={0}", Convert.ToInt32(TextBox1.Text)); try { DataTable dt = dal.Select(cmdMake); foreach (DataRow row in dt.Rows) { msg.Text = "ID: " + row[0].ToString() + " NAME: " + row[1].ToString() + " SEX: " + row[2].ToString() + " AGE: " + row[3].ToString() + " DOW: " + row[4].ToString() + " PHOTO: " + row[4].ToString(); } } catch (Exception ex) { msg.Text = "错误信息:" + ex.Message; } 实验总结(结论或问题分析): 通过本次上机实验,我基本掌握了有关在vs中调用sql语句的方法和熟悉了关于数据库在vs中的使用。 实验成绩 任课教师签名 郭俊恩 7 / 7
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 初中其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服