实验八 开发会员管理系统
一. 目的和要求
掌握VS.NET下程序排错与调试的方法,掌握编程规范,掌握Web.config的配置,掌握程序发布的方法.
二.实验课时
2课时。
三.实验内容
1. 编写会员管理系统的下列模块(任意两个):
1) 登录
2) 注册
3) 显示个人信息
4) 修改个人信息
//Default4
3. 上机调试本章的例题。
4. 实验思考题:
1) 编程规范有何作用?变量命名要遵循哪些规范?
按照编程规范写出的代码便于阅读与理解,对与大工程的十分方便。
骆驼命名法或其它的主流命名规范
2) 如何在Web.config中存放和读取数据库连接信息?
一. 目的和要求
掌握VS.NET下程序排错与调试的方法,掌握编程规范,掌握Web.config的配置,掌握程序发布的方法.
二.实验课时
2课时。
三.实验内容
1. 编写会员管理系统的下列模块(任意两个):
1) 登录
2) 注册
3) 显示个人信息
4) 修改个人信息
5) 取回口令
代码:
(我写的登录和注册)
//Default
//.aspx
<body>
<a href="Default2.aspx">注册</a><br/>
<form id="form1" runat="server">
帐号:<asp:TextBox id="txbID" runat="server"/><br/>
密码:<asp:TextBox id="txbPSW" TextMode="Password" runat="server"/><br/>
<asp:Button id="btnDL" runat="server" Text="登录" OnClick="btnDLClicked" /><br/>
</form>
</body>
//.csprivate bool haveuser(string strID, string strPSW)
{
string ConnectionString = "server=.;database=rej;integrated security=true";
SqlConnection con = new SqlConnection(ConnectionString);
string command = "select * from users where id = '" + strID + "' and '" + strPSW +"'";
SqlDataAdapter adp = new SqlDataAdapter(command, ConnectionString);
DataSet ds = new DataSet();
adp.Fill(ds, "employee");
int num = ds.Tables["employee"].Rows.Count;
con.Close();
if(num>=1) return true;
return false;
}
public virtual void btnDLClicked (object sender, EventArgs args)
{
string ID = txbID.Text;
string PSW = txbPSW.Text;
if (haveuser(ID,PSW))
{
Session["ID"]=ID;
Response.Redirect("Default4.aspx");
}
}
//Default2
//.aspx
<body>
<a href="Default.aspx">登录</a><br/>
<form id="form1" runat="server">
帐号:<asp:TextBox id="txbID" runat="server"/><br/>
密码:<asp:TextBox id="txbPSW" TextMode="Password" runat="server"/><br/>
<asp:Button id="btnDL" runat="server" Text="注册" OnClick="btnZCClicked" /><br/>
</form>
</body>
//.csprivate bool canUse(string strID, string strPSW)
{
string cons = "server=.;database=rej;integrated security=true";
SqlConnection con = new SqlConnection(cons);
string strtxt = "select * from users where id = '" + strID +"'";
SqlCommand sql = new SqlCommand(strtxt, con);
con.open();
SqlDataReader reader = sql.ExecuteReader();
int num = reader.Count;
con.Close();
if(num>=1) return false;
command = "insert into users values('"+strID+"','"+strPSW+"')";
sql.ExecuteNonQuery();
return true;
}
public virtual void btnZCClicked (object sender, EventArgs args)
{
string ID = txbID.Text;
string PSW = txbPSW.Text;
//if(!canUse(ID, PSW)) return;
Session["ID"]=ID;
Response.Redirect("Default4.aspx");
}
//Default4
//.aspx
<body>
<form id="form1" runat="server">
用户<%=Session["ID"]%>,你好!
<asp:Button id="btnDC" runat="server" Text="登出" OnClick="btnDCClicked" /><br/>
</form>
</body>
//.cspublic virtual void btnDCClicked (object sender, EventArgs args)
{
Session.Remove("ID");
Response.Redirect("Default.aspx");
}
效果:
3. 上机调试本章的例题。
4. 实验思考题:
1) 编程规范有何作用?变量命名要遵循哪些规范?
按照编程规范写出的代码便于阅读与理解,对与大工程的十分方便。
骆驼命名法或其它的主流命名规范
2) 如何在Web.config中存放和读取数据库连接信息?
<appSettings>
<add key="connStr" value="Server=IP;Database=数据库名;uid=用户名;pwd=密码"/>
</appSettings>