asp.net开发技术复习题-最新-2026-6-22

更新时间: 试题数量: 购买人数: 提供作者:

有效期: 个月

章节介绍: 共有个章节

收藏
搜索
题库预览
使用DataSet和DataAdapter对象查询数据库的数据。 (1)网站项目中添加一个名为DataAdapterSelectDemo.aspx的Web页面,查询数据库Student中表StuInfo中的数据,将该表中的数据信息显示到页面中,运行效果如图2所示。 (2)编写DataAdapterSelectDemo.aspx页面的Page_Load事件的过程代码,请完善事件代码。 protected void Page_Load(object sender, EventArgs e) { //建立SqlConnection对象 string ConStr = "Server=.;Database=Student;Integrated Security=true"; SqlConnection sqlconn = new SqlConnection(ConStr); //建立DataSet对象和DataRow数据行对象 DataSet ds = new DataSet(); DataRow dr; try { //打开连接 sqlconn.Open(); //建立DataAdapter对象 SqlDataAdapter sda = new SqlDataAdapter("select * from StuInfo", sqlconn); //用Fill方法返回的数据,填充DataSet,数据表取名为“StuTable” sda.Fill(ds, "StuTable"); //ds.Tables[0]将数据表StuTable的数据复制到DataTable对象 DataTable dtable = ds.Tables["StuTable"]; //用DataRowCollection对象获取StuTable数据表的所有数据行 DataRowCollection drc = dtable.Rows; //逐行遍历,取出各行的数据 for (int i = 0; i < drc.Count; i++) { dr = drc[i]; Response.Write("学号:" + dr["StuNo"] + " 姓名:" + dr["Name"] + " 性别:" + dr["Sex"] + " 出生日期:" + dr["Birth"] + " 照片:" + dr["Photo"] + " 专业:" + dr["MajorId"]); Response.Write("<br />"); } } catch (Exception ex) { Response.Write("数据读取出错!原因:" + ex.Message); } finally { sqlconn.Close(); sqlconn = null; } } (含图)
实现向MyBlog数据库的Users数据表中增加一条记录的功能。 (提示:使用DataSet和DataAdapter对象实现) Step1:页面设计 新建网站项目,添加一个名为AdapterIns.aspx的页面,页面设计如图1所示。 Step2:实现页面的功能 编写AdapterIns.aspx页面中“提交”按钮的Click事件过程代码,请编写程序代码,完善页面的程序功能。 protected void btnAdd_Click(object sender, EventArgs e) { string ConStr = "Server =.; Database = MyBlog;Integrated Security = true"; SqlConnection sqlconn = new SqlConnection(ConStr); //建立DataSet对象 DataSet ds = new DataSet(); try { //建立DataAdapter对象 SqlDataAdapter sda = new SqlDataAdapter("select * from Users", sqlconn); //设置DataAdapter的InsertCommand命令。 SqlCommand command = new SqlCommand("insert into Users(LoginId, LoginPwd,Name,QQ,Mail)values (@LoginId,@LoginPwd,@Name,@QQ,@Mail)", sqlconn); command.Parameters.Add("@LoginId", SqlDbType.NChar, 50, "LoginId"); command.Parameters.Add("@LoginPwd", SqlDbType.NVarChar, 50, "LoginPwd"); command.Parameters.Add("@Name", SqlDbType.NChar, 50, "Name"); command.Parameters.Add("@QQ", SqlDbType.NChar, 20, "QQ"); command.Parameters.Add("@Mail", SqlDbType.NChar, 50, "Mail"); sda.InsertCommand = command; //用Fill方法返回的数据,填充DataSet,数据表取名为"users" sda.Fill(ds, "users"); //将数据表users的数据复制到DataTable对象 DataTable dtable = ds.Tables["users"]; //增加新记录 DataRow dr = ds.Tables["users"].NewRow(); //给该记录赋值 dr["LoginId"] = txtUid.Text.Trim(); dr["LoginPwd"] = txtPwd.Text.Trim(); dr["Name"] = txtName.Text ; dr["QQ"] = txtQQ.Text ; dr["Mail"] = txtMail.Text ; dtable.Rows.Add(dr); //提交更新 sda.Update(ds, "users"); //页面输出“新增成功” Response.Write("新增成功<hr>"); } catch (Exception ex) { Response.Write("记录新增失败,原因:" + ex.Message); } finally { sqlconn = null; } } (含图)