当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

河南中医药大学:《ASP.NET应用开发》实验指导书(使用DataSet访问数据库)2

资源类别:文库,文档格式:DOC,文档页数:3,文件大小:39.5KB,团购合买
点击下载完整版文档(DOC)

实验八使用 Dataset访问数据库 实验目的 1、掌握 DataSet Tabels子对象及 DataRow对象在程序设计中的具 体使用方法 实验内容 (1)使用 Data Set向数据库中添加新记录。单击“添加记录”按钮 时执行的事件代码如下 protected void AddRow Click(object sender, EventArgs e) Sqlconnection conn= new SqlConnectiono),∥创建 SQLServer连接对象 conn Connection String=@" Data source=服务器名 Initial Catalog= StudentDB Integrated Security=True ∥把“服务器名”更改为自己的 SQLServer服务器实例名 SqlDataAdapter da= new SqlDataAdapter(;∥创建 DataAdapter对象 string Selectsql="select* from StudentInfo da SelectCommand=new SqlCommand(selectSql, conn) SqlcommandBuilder scb =new Sqlcommand Builder(da); DataSet ds= new DataSet,∥创建一个空 DataSet对象 da Fill(ds) DataRow StudentNewRow =ds. Tables[O]. NewRowO Student Rowl" StudentID"]="200902602028";∥为新行的各字段赋值 Student Newrow" Studentname"]="黄蓉 StudentNewRow["Sex"="* StudentNewRowl"DateOfBirth]=1991-09-28' Student NewRow" Specialty'"]="网络技术"; Studentnewrowl"email=hY9109@126.com ds. Tables[0].Rows. Add(StudentNewRow) Grid Viewl DataSource=ds. Tables[0,∥在 Grid view中显示添加记录后的结果 Grid Viewl. DataBindo; da Update(ds);∥将 DataSet中数据变化提交到数据库(更新数据库 (2)使用 DataSet向更新数据表记录。向Web窗体中添加“更新记录

实验八 使用 DataSet 访问数据库 实验目的 1、 掌握 DataSet.Tabels 子对象及DataRow 对象在程序设计中的具 体使用方法。 实验内容 (1) 使用 DataSet 向数据库中添加新记录。单击“添加记录”按钮 时执行的事件代码如下: protected void AddRow_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(); //创建 SQL Server 连接对象 conn.ConnectionString=@"Data Source=服务器名;Initial Catalog=StudentDB; Integrated Security=True"; //把“服务器名”更改为自己的 SQL Server 服务器实例名 SqlDataAdapter da = new SqlDataAdapter(); //创建 DataAdapter 对象 string SelectSql = "select * from StudentInfo"; da.SelectCommand = new SqlCommand(SelectSql, conn); SqlCommandBuilder scb = new SqlCommandBuilder(da); DataSet ds = new DataSet(); //创建一个空 DataSet 对象 da.Fill(ds); DataRow StudentNewRow = ds.Tables[0].NewRow(); StudentNewRow["StudentID"] = "200902602028"; //为新行的各字段赋值 StudentNewRow["StudentName"] = "黄蓉"; StudentNewRow["Sex"] = "女"; StudentNewRow["DateOfBirth"] = "1991-09-28"; StudentNewRow["Specialty"] = "网络技术"; StudentNewRow["Email"] = "hy9109@126.com"; ds.Tables[0].Rows.Add(StudentNewRow); GridView1.DataSource = ds.Tables[0]; //在 GridView 中显示添加记录后的结果 GridView1.DataBind(); da.Update(ds); //将 DataSet 中数据变化提交到数据库(更新数据库) } (2) 使用DataSet 向更新数据表记录。向Web 窗体中添加“更新记录

按钮,其单击事件代码如下: protected void EditRow Click(object sender, EventArgs e) SqlConnection conn=new Sqlconnectiono conn. Connection String=@" Data source=服务器名; Initial Catalog= Student Integrated Security=True ∥把“服务器名”更改为自己的 SQLServer服务器实例名 SqlDataAdapter da= new SqlDataAdapterO string SelectSql="select* from StudentInfo da. Select Command=new SqlCommand(selectSql, conn) SqlCommand Builder scb= new SqlCommand Builder(da,∥为 Data Adapter自动生成更新 命令 Data Set ds=new DataSet da Fill(ds) ∥创建行对象,从表 Tables(0对象中得到要修改的行,修改第4行的记录 DataRow My Row =ds. Tables[0]. Rows(3 My Row" StudentID]="200902601100;该行各字段赋以新值,如果学号重复,则 出错 My Row" StudentName"]="张无忌" Grid Viewl DataSource= ds. Tables[O,∥在GridⅤiew中显示添加记录后的结果 Grid View l. DataBindo a Update(ds),∥将 DataSet中数据变化提交到数据库(更新数据库)中 (3)通过 Data Set对象删除数据表记录。在Web窗体中添加“删除记 录”按钮,其单击事件代码如下: protected void DelRecord Click(object sender, EventArgs e) SqlConnection conn =new Sqlconnectiono conn ConnectionString:=@" Data Source=服务器名; Initial Catalog= StudentDB Integrated Security=True ∥把“服务器名”更改为自己的 SQLServer服务器实例名 SqlDataAdapter da= new SqlDataAdapterO string SelectSql="select* from StudentInfo da SelectCommand=new SqlCommand( selectSql, conn) SqlCommand Builder scb= new SqlCommand Builder(da)∥为 DataAdapter自动生成更新 命令 DataSet ds=new DataSet da Fill(ds) ∥创建行对象,指定要删除的行索引值(删除第1个记录) Data Row DeleteRow =ds. Tables[0]. Rows[0] Delete Row. Delete,∥调用 DataRow对象的 DeleteD方法,从 DataSet填充的数据表中删

按钮,其单击事件代码如下: protected void EditRow_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(); conn.ConnectionString=@"Data Source=服务器名;Initial Catalog=StudentDB; Integrated Security=True"; //把“服务器名”更改为自己的 SQL Server 服务器实例名 SqlDataAdapter da = new SqlDataAdapter(); string SelectSql = "select * from StudentInfo"; da.SelectCommand = new SqlCommand(SelectSql, conn); SqlCommandBuilder scb = new SqlCommandBuilder(da); //为 DataAdapter 自动生成更新 命令 DataSet ds = new DataSet(); da.Fill(ds); //创建行对象,从表 Tables[0]对象中得到要修改的行,修改第 4 行的记录 DataRow MyRow = ds.Tables[0].Rows[3]; MyRow["StudentID"] = "200902601100"; //为该行各字段赋以新值,如果学号重复,则 出错 MyRow["StudentName"] = "张无忌"; GridView1.DataSource = ds.Tables[0]; //在 GridView 中显示添加记录后的结果 GridView1.DataBind(); da.Update(ds); //将 DataSet 中数据变化提交到数据库(更新数据库)中 } (3) 通过 DataSet 对象删除数据表记录。在 Web 窗体中添加“删除记 录”按钮,其单击事件代码如下: protected void DelRecord_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(); conn.ConnectionString=@"Data Source=服务器名;Initial Catalog=StudentDB; Integrated Security=True"; //把“服务器名”更改为自己的 SQL Server 服务器实例名 SqlDataAdapter da = new SqlDataAdapter(); string SelectSql = "select * from StudentInfo"; da.SelectCommand = new SqlCommand(SelectSql, conn); SqlCommandBuilder scb = new SqlCommandBuilder(da); //为 DataAdapter 自动生成更新 命令 DataSet ds = new DataSet(); da.Fill(ds); //创建行对象,指定要删除的行索引值(删除第 1 个记录) DataRow DeleteRow = ds.Tables[0].Rows[0]; DeleteRow.Delete(); //调用 DataRow 对象的 Delete()方法,从 DataSet 填充的数据表中删

除行 GridⅤ iewl DataSource=ds. Tables{0;∥在 Grid view中显示删除记录后的结果 Grid Viewl Data Bindo da Update(ds);

除行 GridView1.DataSource = ds.Tables[0]; //在 GridView 中显示删除记录后的结果 GridView1.DataBind(); da.Update(ds); }

点击下载完整版文档(DOC)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
已到末页,全文结束
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有