logo资料库

网络程序设计实验报告.docx

第1页 / 共9页
第2页 / 共9页
第3页 / 共9页
第4页 / 共9页
第5页 / 共9页
第6页 / 共9页
第7页 / 共9页
第8页 / 共9页
资料共9页,剩余部分请下载后查看
2017年10 月
一、实验目的
二、实验环境
三、实验内容
四、原理实验步骤
五、关键问题及解决方法
六、实验结果
七、实验体会
通过这次实验熟悉了HTML服务器控件,掌握了基本动态页面的制作,熟悉掌握了服务器的一些内置对象的使用
附录:源代码(仅仅附上核心代码)
1.1网页login.aspx.cs或其他
2.1网页myhome.aspx.cs或其他
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class myhome : System.Web.UI.Page
{
protected void Page_Load(object sender, EventA
{
if (!IsPostBack)
{
ListBMyClass.Items.Add(new ListItem("我
if (Session["S"] == null)
{
Response.Redirect("login.aspx");
}
else
{
Label3.Text = Session["S_U"].ToStr
Label5.Text = System.DateTime.Now.
}
}
}
protected void btnLogout_Click(object sender,
{
Response.Redirect("login.aspx");
}
protected void btnSeachMyclass_Click(object se
{
SqlConnection conn;
conn = new SqlConnection("Data Source=JF50
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("SEL
DataSet ds=new DataSet();
da.Fill(ds, "[NetSchool].[dbo].[STUDENT_CL
DataTable dt = ds.Tables["[NetSchool].[dbo
foreach (DataRow dr in dt.Rows)
{
ListBMyClass.Items.Add(new ListItem(dr
}
}
}
2.3 数据库生成SQL语句[包括建库、建表语句;记录不需要]
《网络程序设计》实验 综合报告 设计题目:AOD.Net 用户登录认证 学生姓名:刘春勤 学 号:2015216764 专业班级:13 计科 02 班 2017 年 10 月
一、 实验目的 二、实验环境 Vs2010 sql server 2008 三、实验内容 获取与表单用户名、密码后,在注册数据库中验证成功后,创建会话变 量后,在 myhome 页面显示当前用户的私有数据;否则,提示请输入正确的用 户名、密码。 四、原理实验步骤 原理:通过服务器控件获取用户输入后,合法用户则创建相应的 Session 变量,并进行合适 的网页重定向,并在 myHome.aspx 网页中显示相应的个人相关记录,如显示当前登录用户的选修课程。 判定合法用户依据:未使用数据库/表时,启用静态用户名/口令验证原则:使用数据库,则通过当前 数据库 VS 2008 或 Access 数据库中,相应的表中是否有该用户和密码。 步骤如下:首先获取 Web.config 文件中的数据库连接字符串,再创建 Conn 对象,并赋予该对象的 ConnectionString 属性后,执行 Conn.Open 方法连接数据库后,再创建 Command 对象,执行相应的 查询语句,根据返回的 DataReader 集合是否有记录,从而获知当前登录的用户/密码是否合法,进而 进行相应的重定向。在用户主页,再查询当前用户的选修课程,并在相应的控件中显示出来。 五、关键问题及解决方法 与数据库创建连接问题,连接字符串书写问题。解决方法:可在 vs2010 中的服务器资源管理器中, 添加连接,查看其属性,直接复制。 SqlCommand 命令中 SQL 语句书写问题。解决方法:可在数据库中先写,看是否能找出,然后为其 添加引号,需设置的变量,化成字符串,需仔细。 重定向问题,需正确输入页面名,且后缀为”.Aspx”。可直接在解决资源管理器中拖入。 2
六、实验结果 3
七、实验体会 通过这次实验熟悉了 HTML 服务器控件,掌握了基本动态页面的制作,熟悉掌握了服务器的一些内 置对象的使用如 Request、Response、Session 等,也熟悉了一些基本的数据库操作,能使用 SQL 语句 进行表的操作(SELECT、INSERT、UPDATE、DELETE),学会了 ADO.NET 提供的连接式数据访问方式等。 4
附录:源代码(仅仅附上核心代码) 1.1 网页 login.aspx.cs 或其他 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; public partial class login : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnLogin_Click(object sender, EventArgs e) { string username = txtUsername.Text; string password = txtPwd.Text; if (username != "" && password != "") { SqlConnection conn; SqlCommand cmd; SqlDataReader rd; Catalog=NetSchool;Integrated Security=True"; string connectString = "Data Source=JF508-047;Initial string cmdText = "SELECT * FROM STUDENT"; conn = new SqlConnection(connectString); conn.Open(); cmd = new SqlCommand(cmdText, conn); rd = cmd.ExecuteReader(); while (rd.Read()) { if (rd["USERNAME"].ToString() == username) { if (rd["PASSWORD"].ToString() == password) { Session["username"] = Server.HtmlEncode(username.Trim()); Session["password"] = Server.HtmlEncode(password.Trim()); Session["userid"] = rd["USERID"].ToString(); Response.Redirect("myhome.aspx"); 1
} else { Label3.Text = "密码错误!"; } } else { Label3.Text = "该用户名不存在!"; } } rd.Close(); conn.Close(); } else { Label3.Text = "请输入正确的用户名和密码!"; } } protected void btnReset_Click(object sender, EventArgs e) { txtUsername.Text = ""; txtPwd.Text = ""; } } 2
2.1 网页 myhome.aspx.cs 或其他 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data; public partial class myhome : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ListBMyClass.Items.Add(new ListItem("我所选修的课程")); if (Session["S"] == null) { } Response.Redirect("login.aspx"); else 3
Label3.Text = Session["S_U"].ToString(); Label5.Text = System.DateTime.Now.ToString(); { } } } protected void btnLogout_Click(object sender, EventArgs e) { } Response.Redirect("login.aspx"); protected void btnSeachMyclass_Click(object sender, EventArgs e) { SqlConnection conn; conn = new SqlConnection("Data Source=JF508-005;Initial Catalog=NetSchool;Integrated Security=True"); conn.Open(); SqlDataAdapter da = new SqlDataAdapter("SELECT *FROM [NetSchool].[dbo].[STUDENT_CLASS]where USERID=" + Session["P"], conn); DataSet ds=new DataSet(); da.Fill(ds, "[NetSchool].[dbo].[STUDENT_CLASS]"); DataTable dt = ds.Tables["[NetSchool].[dbo].[STUDENT_CLASS]"]; foreach (DataRow dr in dt.Rows) 4
分享到:
收藏