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

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

有效期: 个月

章节介绍: 共有个章节

收藏
搜索
题库预览
使用Command对象删除数据。 (1)在CommDeleteDemo.aspx页面中添加一个TextBox控件和一个Button控件,其中Button控件作为“删除”按钮,页面主体部分代码如下: <form id="form1" runat="server"> <div> 输入要删除学生的学号:<br /> <asp:TextBox ID="txtStuNo" runat="server"></asp:TextBox> <asp:Button ID="btnDel" runat="server" Text="删除" OnClick="btnDel_Click" /> </div> </form> (2)实现页面功能:CommDeleteDemo.aspx页面连接数据库Student中的表StuInfo,在该页面的TextBox控件中输入学号“14”,单击“删除”按钮,将删除数据表StuInfo中学号是14号学生的所有记录,若删除成功,页面会显示“删除成功!”,若该学号不存在,页面会显示“该记录不存在”。程序运行效果如图2所示,14号学生不存在。 编写页面中“删除”按钮的Click事件过程代码,请完善该事件代码。 protected void btnDel_Click(object sender, EventArgs e) { int intDeleteCount; //建立Connection对象 string ConStr = "Server=.;Database=Student;Integrated Security=true"; SqlConnection sqlconn = new SqlConnection(ConStr); //建立Command对象 SqlCommand cmd = new SqlCommand(); //给Command对象的Connection和CommandText属性赋值 cmd.Connection = sqlconn; cmd.CommandText = "delete from StuInfo where StuNo=@no"; //在执行之前告诉Command对象@no将来用谁来代替 SqlParameter p1 = new SqlParameter("@No", txtStuNo.Text); cmd.Parameters.Add(p1); try { sqlconn.Open(); intDeleteCount = cmd.ExecuteNonQuery(); if (intDeleteCount > 0) Response.Write("删除成功!"); else Response.Write("该记录不存在!"); } catch (Exception ex) { Response.Write("删除失败,错误原因:" + ex.Message); } finally { cmd = null; sqlconn.Close(); sqlconn = null; } } (含图)
使用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; } } (含图)