完成用户注册、登录页面,实现数据的真实操作。

本文介绍了两个ASP.NET应用程序实例,一是使用HTML表格展示九九乘法表的方法,二是实现用户注册与登录功能的具体代码实现,包括数据库连接、参数设置及执行SQL语句等关键步骤。

编写小九九:

protected void Page_Load(object sender, EventArgs e)
{
HtmlTable table = new HtmlTable();
for (int i = 1; i <= 9; i++)
{
HtmlTableRow row = new HtmlTableRow();
for (int j = 1; j <=i; j++)
{
HtmlTableCell cell = new HtmlTableCell();
cell.InnerText = j + "*" + i + "=" + j * i;
row.Cells.Add(cell);
}
table.Rows.Add(row);
}
table.Width = "100%";
table.CellSpacing = 0;
table.CellPadding = 10;
table.Border = 1;
table.BorderColor = "#000000";
this.p1.Controls.Add(table);
}

注册:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnTClick(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=张海涛;integrated Security=true;Initial Catalog=ASP.NET注册";
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
string a;
if (Radio1.Checked)
{
a = "男";
}
else
{
a = "女";
}

string filename=File1.PostedFile.FileName;
filename=DateTime.Now.Ticks.ToString()+filename.Substring(filename.LastIndexOf("."));
//this.File1.PostedFile.SaveAs(Server.MapPath("upload")+"\\"+fillname);
this.File1.PostedFile.SaveAs(Server.MapPath("upload")+"\\"+filename);
cmd.CommandText = "insert into Login(Name,Sex,Password,Image) values ('" + this.name.Value + "','" + a + "','" + this.Password1.Value + "','" + filename + "')";
SqlDataReader myreader = cmd.ExecuteReader();
con.Close();
this.Response.Write("注册成功");

//StringBuilder sb = new StringBuilder();
//sb.Append("用户名是:").Append(this.name.Value);
//sb.Append("<br/>");
//sb.Append("密码是:").Append(this.Password1.Value);
//sb.Append("<br/>");
//sb.Append("性别是:");
//if (Radio1.Checked)
//{
// sb.Append("男");
//}
//else
//{
// sb.Append("女");
//}
//sb.Append("<br/>");
//string fillname=this.File1.PostedFile.FileName;
//fillname=DateTime.Now.Ticks.ToString()+fillname.Substring(fillname.LastIndexOf("."));
//this.File1.PostedFile.SaveAs(Server.MapPath("upload")+"\\"+fillname);

//sb.Append("头像:");
//sb.Append("<img src=\"upload/"+fillname+"\"/>");
//this.divResult.InnerHtml=sb.ToString();
}
}

登陆:

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 Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnDClick(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=张海涛;integrated Security=true;Initial Catalog=ASP.NET注册";

SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;

cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "mp_login3";

SqlParameter Pname = new SqlParameter("@Name", SqlDbType.VarChar, 10, "Name");
Pname.Direction = ParameterDirection.Input;
Pname.Value = this.Text1.Value.Trim();
cmd.Parameters.Add(Pname);

SqlParameter PSex = new SqlParameter("@Sex", SqlDbType.VarChar, 50, "Sex");
PSex.Direction = ParameterDirection.Output;
cmd.Parameters.Add(PSex);

SqlParameter PPassword = new SqlParameter("@Password", SqlDbType.VarChar, 50, "Password");
PPassword.Direction = ParameterDirection.Output;
cmd.Parameters.Add(PPassword);

SqlParameter PImage = new SqlParameter("@Image", SqlDbType.VarChar, 50, "Image");
PImage.Direction = ParameterDirection.Output;
cmd.Parameters.Add(PImage);


conn.Open();
int a = cmd.ExecuteNonQuery();
if (this.Text1.Value == cmd.Parameters["@Name"].Value.ToString().Trim() && this.Text2.Value == cmd.Parameters["@Password"].Value.ToString().Trim())
{
Response.Write("用户名是:" + cmd.Parameters["@Name"].Value.ToString());
Response.Write("性别是:" + cmd.Parameters["@Sex"].Value.ToString());
Response.Write("<img src=\"upload/" + cmd.Parameters["@Image"].Value.ToString() + "\"/>");

//头像是:
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值