




下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验报告课程名称Web程序设计实验名称实验四、ADO.NET数据访问接口日期2014.11.25学生学号B13053215姓名刘婷婷班级B130532实验目的:1.熟悉ADO.NET数据库访问技术。掌握Connection、Command对象的使用。掌握DataReader、DataAdapter对象操作数据库数据的方法。掌握VS2008中创建数据库的方法。实验条件:电脑一台、能上网查阅资料。实验内容与步骤:1.实验内容一(1)要求新建名字为“Accessdatabase_Exercise”的网站。在网站的App_Data文件夹中,建立数据库"MyDatabase_Exercise.mdf”。在该数据库中建立一张职工表,并且添加一些模拟的职工记录。其关系模式如下:Employees(ID,NAME,SEX,AGE,Dateofwork,FilenameofPhoto)在web.config配置文件中,修改"<connectionStrings/>"标记如下。<connectionStrings><addname="ConnectionString"connectionString="DataSource=LTT;InitialCatalog=MyDatabase_Exercise;IntegratedSecurity=True"providerName="System.Data.SqlClient"/></connectionStrings>添加一个网页,利用Command对象实现新职工的录入。添加一个网页,利用Command对象实现删除指定编号的职工记录。添加一个网页,利用Command对象实现修改指定编号的职工信息。添加一个网页,利用DataAdapter对象实现查询职工信息,并显小到网页的Label控件上。(2)源代码和实验结果1.添加一个Command_insert.aspx网页,利用Command对象实现新职工的录入。代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Data.SqlClient;usingSystem.Data;usingSystem.Web.Configuration;publicpartialclassCommand_insert:System.Web.UI.Page(protectedvoidSubmitBtn_Click(objectsender,EventArgse)(stringsqlconnstr=WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;SqlConnectionsqlconn=newSqlConnection(sqlconnstr);SqlCommandsqlcommand=newSqlCommand();sqlcommand.Connection=sqlconn;sqlcommand.CommandText="insertintoEmployees(ID,NAME,SEX,AGE,Dateofwork,FilenameofPhoto)values(@ID,@NAME,@SEX,@AGE,@Dateofwork,@FilenameofPhoto)”;sqlcommand.Parameters.AddWithValue("@ID”,TextBox1.Text);sqlcommand.Parameters.AddWithValue("@NAME”,TextBox2.Text);sqlcommand.Parameters.AddWithValue("@SEX”,DropDownList1.Text);sqlcommand.Parameters.AddWithValue("@AGE”,TextBox4.Text);sqlcommand.Parameters.AddWithValue("@Dateofwork”,TextBox5.Text);sqlcommand.Parameters.AddWithValue("@FilenameofPhoto”,FileUpload1.FileName);try(sqlconn.Open();sqlcommand.ExecuteNonQuery();if(FileUploadl.HasFile==true)(FileUpload1.SaveAs(Server.MapPath(("~/image/")+FileUploadl.FileName));}Label7.Text="成功添加记录";}catch(Exceptionex)(Label7.Text="错误原因”+ex.Message;}finally(sqlcommand=null;sqlconn.Close();sqlconn=null;}}}实验结果如下::寺htLp://localhost:5&8[]iPTlocalhost乂1牛(丹鼎本〔日登看W)岐藏衰|囚iTH(T)ID:1NAME:liuttSEX:女wAGE21DataofiAork:11C:\1.ipa提莪成功舔加记录
usingusingusingusingusingusingusingusingusingusingusingusingusingusingusingusingusingusingSystem;System.Collections.Generic;System.Linq;System.Web;System.Web.UI;System.Web.UI.WebControls;System.Data.SqlClient;System.Data;System.Web.Configuration;publicpartialclassCommand_delete:System.Web.UI.Page(protectedvoidDelBtn_Click(objectsender,EventArgse)(intintDeleteCount;stringsqlconnstr=WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;SqlConnectionsqlconn=newSqlConnection(sqlconnstr);SqlCommandsqlcommand=newSqlCommand();sqlcommand.Connection=sqlconn;sqlcommand.CommandText="deletefromEmployeeswhereID=@ID”;sqlcommand.Parameters.AddWithValue("@ID”,TextBox1.Text);try(sqlconn.Open();intDeleteCount=sqlcommand.ExecuteNonQuery();if(intDeleteCount>0)Label2.Text="成功删除记录!";elseLabel2.Text="该记录不存在!";}catch(Exceptionex)(Label2.Text="错误原因:"+ex.Message;}finally(sqlcommand=null;sqlconn.Close();sqlconn=null;}}}实验结果如下:添加一个Command_Update.aspx网页,利用Command对象实现修改指定编号的职工信息。代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Web.Configuration;publicpartialclassCommand_Update:System.Web.UI.Page{protectedvoidSubmitBtn_Click(objectsender,EventArgse)(Stringsqlconnstr=WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;SqlConnectionsqlconn=newSqlConnection(sqlconnstr);SqlCommandsqlcommand=newSqlCommand();sqlcommand.Connection=sqlconn;sqlcommand.CommandText="updateEmployeessetNAME=@NAME,SEX=@SEX,Dateofwork=@Dateofwork,FilenameofPhoto=@FilenameofPhotowhereID=@ID”;sqlcommand.Parameters.AddWithValue("@ID”,TextBox1.Text);sqlcommand.Parameters.AddWithValue("@NAME”,TextBox2.Text);sqlcommand.Parameters.AddWithValue("@SEX”,DropDownList1.Text);sqlcommand.Parameters.AddWithValue("@AGE”,TextBox3.Text);sqlcommand.Parameters.AddWithValue("@Dateofwork”,TextBox4.Text);sqlcommand.Parameters.AddWithValue("@FilenameofPhoto”,FileUpload1.FileName);try(sqlconn.Open();sqlcommand.ExecuteNonQuery();Label7.Text="成功修改记录";}catch(Exceptionex)(Labell.Text="错误原因:"+ex.Message;}finally{sqlcommand=null;sqlconn.Close();sqlconn=null;}}}实验结果如下:添加一个DataAdapter_update.aspx网页,利用DataAdapter对象实现查询职工信息,并显示到网页的Label控件上。代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Web.ConfigurationpublicpartialclassDataAdapter_update:System.Web.UI.Page(protectedvoidPage_Load(objectsender,EventArgse)(stringsqlconnstr=WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;SqlConnectionsqlconn=newSqlConnection(sqlconnstr);DataSetds=newDataSet();DataTabledtable;DataRowCollectioncoldrow;DataRowdrow;sqlconn.Open();SqlDataAdaptersqld=newSqlDataAdapter("select*fromEmployees”,sqlconn);sqld.Fill(ds,"Employees");dtable=ds.Tables["Employees"];coldrow=dtable.Rows;for(inti=0;i<coldrow.Count;i++)(drow=coldrow[i];Label1.Text+="ID"+drow[0];Label1.Text+="NAME"+drow[1];Label1.Text+="SEX"+drow[2];Label1.Text+="AGE"
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 云南农业大学《数字软件设计1》2023-2024学年第一学期期末试卷
- 西安培华学院《免疫学及病原生物学》2023-2024学年第二学期期末试卷
- 上海体育大学《土建概论》2023-2024学年第二学期期末试卷
- 六安职业技术学院《快题表现》2023-2024学年第二学期期末试卷
- 吉林工商学院《英语学习策略2(强化)》2023-2024学年第二学期期末试卷
- 嘉兴职业技术学院《工程项目招投标与合同管理》2023-2024学年第二学期期末试卷
- 沈阳体育学院《导游日语》2023-2024学年第二学期期末试卷
- 技术人员职务聘用合同
- 合同协议内容变更协议
- 担保公司抵押借款合同
- GB/T 36547-2024电化学储能电站接入电网技术规定
- 医疗抢救设备仪器培训
- 《民航服务与沟通学》课件-第25讲 值机处旅客的沟通技巧
- 2024中国慢性阻塞性肺疾病基层诊疗与管理指南解读
- 2025年中国电信云网资源管理技能认证考试题库(含各题型)
- 重难点31 阿基米德三角形(举一反三)(新高考专用)(学生版) 2025年高考数学一轮复习专练(新高考专用)
- 国开实验平台《基础写作》形考作业第1-4单元测试答案
- 生猪屠宰兽医卫生检验人员理论考试题库及答案
- 《大自然的语言》课件
- 智能安防监控系统维护手册
- 人教版数学二年级下册全册核心素养目标教学设计
评论
0/150
提交评论