WEB实验7.doc

上传人:scccc 文档编号:13934786 上传时间:2022-01-27 格式:DOC 页数:13 大小:191.50KB
返回 下载 相关 举报
WEB实验7.doc_第1页
第1页 / 共13页
WEB实验7.doc_第2页
第2页 / 共13页
WEB实验7.doc_第3页
第3页 / 共13页
WEB实验7.doc_第4页
第4页 / 共13页
WEB实验7.doc_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《WEB实验7.doc》由会员分享,可在线阅读,更多相关《WEB实验7.doc(13页珍藏版)》请在三一文库上搜索。

1、WEB程序设计实验报告实验名称:数据库编程实验地点:11教407专业班级:信管092学生姓名: 步艳杰学生学号: 2009094001指导教师:王铁成绩:2012年5月31日一、实验目的1、掌握ADO.Net的基本框架2、掌握ASP.Net访问和操作数据库的方法二、实验准备1、Windows 2000或Windows XP操作系统2、VS.Net2005编辑器3、SQL Server 2000数据库三、实验内容及步骤1、连接数据库使用代码中的字符串连接打开SQL Server 2000企业管理器,新建一个名为“mydatabase”的数据库,打开VS.net2005编辑器,新建一个网站,名称任

2、意,打开默认的“default.aspx”,在其代码编写窗口的Page_Load事件中编写下面代码: public partial class _Default : System.Web.UI.Page protected void Page_Load(object sender, EventArgs e) using (SqlConnection conn = new SqlConnection(server=loadhost;Integrated Security=SSPI;Database=mydatabase) ; Context.Response.Write(连接成功); 在浏览器中

3、查看程序运行结果,若不能正常连接,请检查是否是登录名称或者用户密码有错,分析连接字符串的各个项目分别代码什么含义(必须写到实验报告中)。答:Sever表示数据库服务器。Integrated Security=sspi表示windows身份验证。Database表示需要连接的数据库。使用Web.config文件连接点击2005菜单“网站”“添加新项”,在“添加新项”对话框中选择“Web配置文件”,向网站中添加一个名为“web.config”的配置文件,双击并打开web.config文件,修改节点为下列内容: 在网站中添加一个新的Web窗体,名称不限,在窗体的Load事件中添加下列代码(其中注释部

4、分可以添加):public partial class Default2 : System.Web.UI.Page protected void Page_Load(object sender, EventArgs e) string shujuku = ConfigurationManager.ConnectionStringswodeDataBase.ConnectionString; using (SqlConnection conn = new SqlConnection(shujuku) Context.Response.Write(连接成功 + shujuku); 在浏览器中查看程

5、序运行结果,掌握访问web.config文件中连接字符串的方法新建一个窗体,重复上述过程,并说明实验中,各个项目的对应关系。2、添加记录在先前创建的mydatabase数据库中建立一个表,名为student,字段包括学号、姓名、曾用名、性别、出生日期、班级、专业等。创建一个web窗体,样式自行设计,建议“性别”使用单选按钮控件,“出生日期”使用日期型控件完成。根据上述过程,编写相应的程序(具体编写过程可参考上课时的示例)。要求:对数据进行校验,学号、姓名不能为空;学号的长度为10位,前四位为年份,且年份大于2000、小于或者等于当前年份,其余为数字;若用户在“曾用名”没有录入,则在数据表“曾用

6、名”字段中录入“姓名”信息。public partial class _Default : System.Web.UI.Page protected void Page_Load(object sender, EventArgs e) protected void Button1_Click(object sender, EventArgs e) Label1.Text = Calendar1.SelectedDate.ToString(); string strxh, strname, strpname, strsex, strclass, strzy, strbirth; strxh =

7、this.TextBox1.Text; strname = this.TextBox2.Text; strpname = this.TextBox3.Text; strsex = ; if (RadioButton1.Checked = true) strsex = this.RadioButton1.Text; else if (RadioButton2.Checked = true) strsex = this.RadioButton2.Text; strbirth = Label1.Text; strclass = this.TextBox4.Text; strzy = this.Tex

8、tBox5.Text; if (TextBox1.Text = ) Response.Write( alert(学号不能为空,请输入学号!);history.go(-1);); else if (TextBox1.Text.Length != 10) Response.Write( alert(学号应为位!);history.go(-1);); else string strnum = ; strnum = TextBox1.Text.Substring(0, 4); if (Convert.ToInt32(strnum) DateTime.Now.Year) Response.Write(

9、alert(年份应大于2000、小于或者等于当前年份!);history.go(-1);); else if (TextBox2.Text = ) Response.Write( alert(姓名不能为空,请输入姓名!);history.go(-1);); else /连接数据库 string shujuku = ConfigurationManager.ConnectionStringswodemydatabase.ConnectionString; using (SqlConnection conn = new SqlConnection(shujuku) conn.Open(); usi

10、ng (SqlCommand cmd = conn.CreateCommand() if (TextBox3.Text = ) strpname = strname; cmd.CommandText = insert into student values ( + strxh + , + strname + , + strpname + , + strsex + , + strbirth + , + strclass + , + strzy + ); /执行操作 try cmd.ExecuteNonQuery(); Response.Write(alert(提交信息成功!);); catch

11、(SqlException s) Response.Write(alert(提交信息失败!);); finally protected void RadioButton2_CheckedChanged(object sender, EventArgs e) 3、查看记录手动编码方式根据课上所学的view.aspx和viewdetail.aspx文件的设计过程,编写浏览学生表所有信息的代码。public partial class view : System.Web.UI.Page protected void Page_Load(object sender, EventArgs e) stri

12、ng str1 = server=.;database=mydatabase;uid=sa;pwd=; SqlConnection con = new SqlConnection(str1); con.Open(); string str2 = select *from student; SqlCommand cmd = new SqlCommand(str2,con); SqlDataReader dr = cmd.ExecuteReader(); Response.Write(); Response.Write(); for (int i = 0; i dr.FieldCount; i+)

13、 Response.Write(); Response.Write(dr.GetValue(i); Response.Write(); Response.Write(); Response.Write(); 数据绑定方式在页面中添加一个GridView控件,设定数据源为DataSet中的一个DataTable,写出具体的代码。4、删除记录在3中步中,在列表后面添加一个删除的超级链接,链接到Delete.aspx页面中,在此页面中编写删除的代码,当删除成功以后返回“删除成功!”的对话框,否则显示“删除失败!”的对话框。5、修改记录如4所述,添加一个修改界面,完成对指定记录的修改操作。要求同“添加

14、记录”。namespace WEB7.TEST public partial class insert : System.Web.UI.Page protected void Page_Load(object sender, EventArgs e) string strid= Request.QueryStringid; string str = WebConfigurationManager.ConnectionStringsmydatabaseconnection.ConnectionString; SqlConnection con = new SqlConnection(str);

15、con.Open(); string statement = select * from student+strid; SqlCommand cmd = new SqlCommand(statement,con); SqlDataReader dr = cmd.ExecuteReader(); string strnum = null, strname = null, strforename = null, strsex = null, strclass = null, strsubject = null; DateTime strbirth =new DateTime(); while (d

16、r.Read() strnum = dr.GetValue(0).ToString(); strname= dr.GetValue(1).ToString(); strforename = dr.GetValue(2).ToString(); strsex = dr.GetValue(3).ToString(); strbirth = (DateTime)dr.GetValue(4); strclass=dr.GetValue(5).ToString(); strsubject = dr.GetValue(6).ToString(); string statement2 = update st

17、udent set 学号= + strnum + ,姓名= + strname + ,曾用名= + strforename + ,性别= + strsex + ,出生日期=strbirth.ToString().Substring(0, 10);,班级= + strclass + ,专业= + strsubject + ; SqlCommand cmd2 = new SqlCommand(statement, con); 6、结合Session对象说明用户必须登陆以后才能进行数据浏览的方法,在mydatabase数据库中创建一个名称“管理员”的表,编写用户登录及在本实验第2步骤中应用Sessi

18、on进行登陆状态检验的代码。 using System.Data.Sql;using System.Data.SqlClient;using System.Web.Configuration;using System.Data;namespace WEB7.TEST public partial class login : System.Web.UI.Page protected void Page_Load(object sender, EventArgs e) protected void Button1_Click(object sender, EventArgs e) string s

19、tr = WebConfigurationManager.ConnectionStringsmydatabaseconnection.ConnectionString; SqlConnection con = new SqlConnection(str); con.Open(); string statement = select * from login; SqlCommand cmd = new SqlCommand(statement, con); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; Data

20、Set ds = new DataSet(); da.Fill(ds); if (struid.Equals(ds.Tables0.Rows0账号.ToString() & strpwd.Equals(ds.Tables0.Rows0密码.ToString() Response.Redirect(loginsee.aspx); else Response.Write(alert(账号或密码错误!); using System.Data;using System.Data.Sql;using System.Data.SqlClient;using System.Web.Configuration

21、;namespace WEB7.TEST public partial class loginsee : System.Web.UI.Page protected void Page_Load(object sender, EventArgs e) string str = WebConfigurationManager.ConnectionStringsmydatabaseconnection.ConnectionString; SqlConnection con = new SqlConnection(str); con.Open(); string statement = select

22、* from login; SqlCommand cmd = new SqlCommand(statement, con); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; DataSet ds = new DataSet(); da.Fill(ds); GridView1.DataSource = ds.Tables0.DefaultView; GridView1.DataBind(); 四、实验总结写出你知道的ADO.Net中使用的类及其作用(以SqlClient命名空间为例)。Connection 物件

23、Connection 对象主要是开启程序和数据库之间的连结。没有利用连结对象将数据库打开,是无法从数据库中取得数据的。这个物件在ADO.NET 的最底层,我们可以自己产生这个对象,或是由其它的对象自动产生。Command 物件 Command 对象主要可以用来对数据库发出一些指令,例如可以对数据库下达查询、新增、修改、删除数据等指令,以及呼叫存在数据库中的预存程序等。这个对象是架构在Connection 对象上,也就是Command 对象是透过连结到数据源DataAdapter 物件 DataSetCommand 对象主要是在数据源以及DataSet 之间执行数据传输的工作,它可以透过Comm

24、and 对象下达命令后,并将取得的数据放入DataSet 对象中。这个对象是架构在Command对象上,并提供了许多配合DataSet 使用的功能。在Beta 2 版中DataSetCommand 物件会更名为DataAdapter。DataSet 物件 DataSet 这个对象可以视为一个暂存区(Cache),可以把从数据库中所查询到的数据保留起来,甚至可以将整个数据库显示出来。DataSet 的能力不只是可以储存多个Table 而已,还可以透过DataSetCommand 对象取得一些例如主键等的数据表结构,并可以记录数据表间的关联。DataSet 对象可以说是ADO.NET 中重量级的对

25、象,这个对象架构在DataSetCommand 对象上,本身不具备和数据源沟通的能力;也就是说我们是将DataSetCommand 对象当做DataSet 对象以及数据源间传输数据的桥梁。DataReader 物件 当我们只需要循序的读取数据而不需要其它操作时,可以使用DataReader 对象。DataReader对象只是一次一笔向下循序的读取数据源中的数据,而且这些数据是只读的,并不允许作其它的操作。因为DataReader 在读取数据的时候限制了每次只读取一笔,而且只能只读,所以使用起来不但节省资源而且效率很好。使用DataReader 对象除了效率较好之外,因为不用把数据全部传回,故可以降低网络的负载12

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1