背景我是一个asp.net菜鸟,暂时开始学习ASP.NET,在此记录下我个人敲的代码,没有多少参考价值,请看到的盆友们为我点个赞支持我一下,多谢了。网站介绍根据书上的例子做了一个比较粗糙的登录例子,里面的代码都是自己敲出来的,而且很少使用封装方法,就是为了让自己能更清楚的记住做的过程。这个网站包含注册、登录、修改密码三个功能。注册介绍
新建一个Web窗体,即UserManagers.aspx。不粘贴前台代码了。然后编写注册方法,包括用户名当作主键,SqlDataReader方式读取数据库,SqlCommand参数添加数据等要点PRotectedvoidButton1_Click(objectsender, EventArgs e)
{if(txtName.Text ==""|| txtPwd.Text ==""|| txtConfirm.Text =="")
{this.Page.RegisterStartupScript("ss","<script>alert('用户名密码不能为空')</script>");return;
}if(txtPwd.Text.Equals(txtConfirm.Text))
{//查看当前用户是否存在SqlConnection sqlConn =newSqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
sqlConn.Open();stringsql ="select * from tb_user where username = '"+ txtName.Text.Trim() +"'";
SqlCommand sqlCommand=newSqlCommand(sql, sqlConn);
SqlDataReader sqlDataReader=sqlCommand.ExecuteReader();if(sqlDataReader.Read())
{
Page.RegisterStartupScript("","<script>alert('用户名已存在!')</script>");return;
}
sqlDataReader.Close();//新增用户stringstrInsert ="insert into tb_user(username, pwd, marks) values (@username,@pwd, @marks)";
sqlCommand=newSqlCommand(strInsert, sqlConn);
sqlCommand.Parameters.Add("@username", SqlDbType.VarChar);
sqlCommand.Parameters["@username"].Value =txtName.Text;
sqlCommand.Parameters.Add("@pwd", SqlDbType.VarChar,20);
sqlCommand.Parameters["@pwd"].Value =txtPwd.Text;
sqlCommand.Parameters.Add("@marks", SqlDbType.VarChar,1000);
sqlCommand.Parameters["@marks"].Value ="zbq测试";
sqlCommand.ExecuteNonQuery();
sqlConn.Close();
Page.RegisterStartupScript("","<script>alert('注册成功!')</script>");
Response.Redirect("Default.aspx?Name="+ txtName.Text +"");
}
}
界面效果如下
登录介绍
首先添加登录窗口ManageLogin.aspx,然后写登录代码,包含验证码这一要点protectedvoidbtnLogin_Click(objectsender, EventArgs e)
{if(string.IsNullOrEmpty(txtName.Text)||string.IsNullOrEmpty(txtPwd.Text) ||string.IsNullOrEmpty(txtValid.Text))
{
Page.RegisterStartupScript("","<script>alert('信息填写不完全!')</script>");return;
}if(!txtValid.Text.ToUpper().Equals(session["ValidNums"]))
{
Page.RegisterStartupScript("","<script>alert('验证码不正确!')</script>");return;
}
SqlConnection sql=newSqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
sql.Open();stringselect="select * from tb_user t where t.username = '"+ txtName.Text.Trim() +"' and pwd = '"+ txtPwd.Text.Trim() +"'";
SqlCommand command=newSqlCommand(select, sql);
SqlDataReader dataReader=command.ExecuteReader();if(dataReader.Read())
{//成功就跳转Response.Redirect("Default.aspx?Name="+ txtName.Text +"");
}else{
Page.RegisterStartupScript("","<script>alert('账户名或密码错误!')</script>");
dataReader.Close();return;
}
登录效果如图 修改密码介绍首先建立一个EditPwd.aspx窗体<tableclass="table"border="1px"align="center"><tr><tdclass="firstTd">用户名:</td><td><asp:DropDownListrunat="server"ID="names"Width="200px"Height="20px"/></td></tr><tr><tdclass="firstTd">原密码:</td><td><asp:TextBoxrunat="server"ID="txtOldPwd"TextMode="PassWord"/></td></tr><tr><tdclass="firstTd">新密码:</td><td><asp:TextBoxrunat="server"ID="txtNewPwd"TextMode="Password"></asp:TextBox></td></tr><tr><tdclass="firstTd"> </td><tdalign="right"><span><asp:Buttonrunat="server"ID="btnSure"OnClick="btnSure_Click"Text="确认登录"/><asp:Buttonrunat="server"ID="btnCancle"OnClick="btnCancle_Click"Text="取消"/></span></td></tr></table>
然后编写修改方法,包含SqlDataAdapter + DataSet关键点
protectedvoidPage_Load(objectsender, EventArgs e)
{//初始化数据if(!IsPostBack)
{
SqlConnection sql=newSqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
sql.Open();stringselect="select * from tb_user";
SqlDataAdapter sqlDataAdapter=newSqlDataAdapter(select, sql);
DataSet dataSet=newDataSet();
sqlDataAdapter.Fill(dataSet);
sql.Close();if(dataSet.Tables[0].Rows.Count>0)
{for(intindex =0; index < dataSet.Tables[0].Rows.Count; index++)
{
names.Items.Add(dataSet.Tables[0].Rows[index][1].ToString());
}
}
}
}protectedvoidbtnSure_Click(objectsender, EventArgs e)
{if(string.IsNullOrEmpty(txtNewPwd.Text) ||string.IsNullOrEmpty(txtOldPwd.Text))
{
Page.RegisterStartupScript("","<script>alert('密码不能为空或者不能不相等!')</script>");return;
}
SqlConnection sqlConnection=newSqlConnection("server=PC-20150424DMHQ;database=MyDatas;uid=sa;pwd=123456");stringselect="select * from tb_user where username = '"+names.Text +"'";
SqlCommand sqlCommand=newSqlCommand(select, sqlConnection);
sqlConnection.Open();
SqlDataReader sqlDataReader=sqlCommand.ExecuteReader();if(sqlDataReader.Read())
{if(sqlDataReader["pwd"].ToString() !=txtOldPwd.Text)
{
Page.RegisterStartupScript("","<script>alert('密码输入错误!')</script>");return;
}
}else{
Page.RegisterStartupScript("","<script>alert('数据库连接错误!')</script>");return;
}
sqlConnection.Close();
sqlDataReader.Close();//修改密码sqlConnection.Open();stringupdatePwd ="update tb_user set pwd = '"+ txtNewPwd.Text +"' where username = '"+ names.Text +"'";
sqlCommand=newSqlCommand(updatePwd, sqlConnection);
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
Page.RegisterStartupScript("","<script>alert('修改成功!')</script>");
Page_Load(null,null);
}
修改密码界面效果