C# ASP.net 入门之简单通讯录
简单通讯录功能虽然简单,却包括了制作一个网站的基本功能!各个模块可以作为新手入门的参考。
简单通讯录实现功能:1.登录 2.注册 3.后台管理 4.前台登录显示 5.创建联系人 6.密码修改
代码下载:http://download.csdn.net/detail/wyz365889/5773253
实现功能效果图如下:
主要代码实现如下:
1.底层数据模块
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data;
/// <summary>
/// DBManage 的摘要说明
/// </summary>
public class DBManage
{
public DBManage()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
string strConn = @"Data Source=WYZ-PC\SQL2005;Integrated Security=SSPI;Initial Catalog=addressBook;";
public SqlConnection conn;
public void sqlConn() //连接数据库
{
try
{
conn = new SqlConnection(strConn);
conn.Open();
}
catch
{
return;
}
}
//读取语句执行结果
public SqlDataReader readResult(string strSql)
{
try
{
SqlCommand sqlComd = new SqlCommand(strSql, conn);
return sqlComd.ExecuteReader();
}
catch
{
throw;
}
}
//读取数据到操作表中
public bool readData(string strSql, out DataTable dt)
{
dt = new DataTable();
try
{
SqlDataAdapter sda = new SqlDataAdapter(strSql, conn);
sda.Fill(dt);
return true;
}
catch (Exception e)
{
return false;
}
}
//执行插入,更新语句
public bool execSql(string strSql)
{
SqlCommand sc = new SqlCommand(strSql, conn);
try
{
sc.ExecuteNonQuery();
return true;
}
catch (Exception e)
{
return false;
}
}
//查询是否存在数据
public bool isExistData(string strSql)
{
bool flag = false;
try
{
using (SqlCommand sc = new SqlCommand())
{
sc.CommandText = strSql;
sc.Connection = conn;
SqlDataReader sr = sc.ExecuteReader();
if (sr.HasRows)
{
flag = true;
}
sr.Close();
}
}
catch (Exception e)
{
flag = false;
}
return flag;
}
public void closeDB()
{
conn.Close();
conn.Dispose();
}
}
2.登录代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button_Login_Click(object sender, EventArgs e)
{
DBManage db = new DBManage();
db.sqlConn(); //数据库连接
string strUserID = TextBox1_userID.Text.Trim();
string strPwd = TextBox2_pwd.Text.Trim();
if (strUserID == "" || strPwd == "")
{
Label1_meg.Text = "提示:用户名或者密码不能为空!";
}
else
{
string strSql = @"select * from tb_user";
if (!db.isExistData(strSql)) //不存在用户,添加默认用户
{
db.execSql(@"insert into tb_user values('admin','admin','admin','管理员')");
}
string strSql2 = "select * from tb_user where userID='" + strUserID + "' and pwd='" + strPwd + "'";
if (db.isExistData(strSql2))
{
SqlDataReader sqlRead = db.readResult(strSql2);
sqlRead.Read();
string strRole = sqlRead[3].ToString();
sqlRead.Close();
db.closeDB(); //关闭数据库
if (strRole.Trim().Equals("管理员")) //管理员权限
{
Response.Redirect("admin.aspx?userID=" + strUserID);
}
else if (strRole.Trim() == "普通用户") //普通用户权限
{
Response.Redirect("userInfo.aspx?userID=" + strUserID);
}
}
else
{
Label1_meg.Text = "提示:密码或帐号不正确,请重新输入!";
TextBox1_userID.Text = "";
TextBox2_pwd.Text = "";
}
}
}
}
3.注册代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class register : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
DBManage db;
protected void Button_reg_Click(object sender, EventArgs e)
{
db = new DBManage();
db.sqlConn();
string strUserID = TextBox1_userID.Text.Trim();
string strName = TextBox2_name.Text.Trim();
string strPwd = TextBox4_pwd1.Text.Trim();
string strPwd2 = TextBox1_pwd2.Text.Trim();
string strRole = "普通用户";
string strSql2 = "insert into tb_user values('" + strUserID + "','" + strName + "','" + strPwd + "','" + strRole + "')";
if (db.execSql(strSql2))
{
Response.Write("<script>alert('注册成功!');window.location.href ='login.aspx'</script>");
return;
}
}
}
4.后台管理代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class admin : System.Web.UI.Page
{
DBManage db;
string strUserID;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
MyBind("select * from tb_user");
strUserID = Request.QueryString["userID"].ToString();
HyperLink1_pwd.NavigateUrl = "~/pwd.aspx?userID=" + strUserID;
}
}
void MyBind(String strSql)
{
db = new DBManage();
db.sqlConn();
SqlDataAdapter da = new SqlDataAdapter(strSql, db.conn);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
string strKey = TextBox1_key.Text.Trim();
string strID = "";
if (DropDownList1_select.Text == "用户名")
{
strID = "userID";
}
if (DropDownList1_select.Text == "姓名")
{
strID = "userName";
}
if (DropDownList1_select.Text == "权限")
{
strID = "role";
}
String strSql = "select * from tb_user where " + strID + " like '" + strKey + "%'";
MyBind(strSql);
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
db = new DBManage();
db.sqlConn();
string sql = "delete from tb_user where userID='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
SqlCommand cmd = new SqlCommand(sql, db.conn);
cmd.ExecuteNonQuery();
db.closeDB();
MyBind("select * from tb_user");//调用MyBind()子程序
}
}
5.前台登录显示
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class userInfo : System.Web.UI.Page
{
DBManage db;
string strUserID="";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
MyBind("select * from tb_info");
strUserID = Request.QueryString["userID"].ToString();
HyperLink1_pwd.NavigateUrl = "~/pwd.aspx?userID=" + strUserID;
HyperLink1_new.NavigateUrl = "~/newInfo.aspx?userID=" + strUserID;
}
}
void MyBind(String strSql)
{
db = new DBManage();
db.sqlConn();
SqlDataAdapter da = new SqlDataAdapter(strSql, db.conn);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sql = "delete from tb_info where num=" + GridView1.DataKeys[e.RowIndex].Values[0].ToString() + " and userID='"
+ GridView1.DataKeys[e.RowIndex].Values[1].ToString() + "'";
db = new DBManage();
db.sqlConn();
SqlCommand cmd = new SqlCommand(sql, db.conn);
//执行删除操作
cmd.ExecuteNonQuery();
db.closeDB();
MyBind("select * from tb_info");//调用MyBind()子程序
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
String strSql = "select * from tb_info where num=" + GridView1.DataKeys[e.NewEditIndex].Values[0].ToString()+" and userID='"
+GridView1.DataKeys[e.NewEditIndex].Values[1].ToString()+"'";
MyBind(strSql);
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
MyBind("select * from tb_info");
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
db = new DBManage();
db.sqlConn();
TextBox name, sex, phone, qq,birthday, remark;
name = (TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0];
sex = (TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0];
phone = (TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0];
qq = (TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0];
birthday = (TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0];
remark = (TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0];
String strSql = "update tb_info set name='" + name.Text + "',sex='" + sex.Text +
"',phone='" + phone.Text + "',qq='" + qq.Text + "',birthday=" + birthday.Text.Substring(0,9)+ ",remark='" +
remark.Text + "' where num=" + GridView1.DataKeys[e.RowIndex].Values[0].ToString() + " and userID='"
+ GridView1.DataKeys[e.RowIndex].Values[1].ToString() + "'";
db.execSql(strSql);
GridView1.EditIndex = -1;
MyBind("select * from tb_info");//调用MyBind()子程序
}
protected void Button1_Click(object sender, EventArgs e)
{
string strKey = TextBox2.Text.Trim();
String strSql = "select * from tb_info where name like '" + strKey + "%' or phone like '" + strKey
+"%' or qq like '" + strKey + "%'";
MyBind(strSql);
}
}
6.密码修改
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class pwd : System.Web.UI.Page
{
string strUserID;
DBManage db;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
strUserID = Request.QueryString["userID"].ToString();
TextBox1_userID.Text = strUserID;
TextBox1_userID.Enabled = false;
}
}
protected void Button_pwd_Click(object sender, EventArgs e)
{
db = new DBManage();
db.sqlConn();
string strNewPwd = TextBox4_pwd1.Text.Trim();
string strRPwd = TextBox1_pwd2.Text.Trim();
string strSql = "select * from tb_user where userID='" + TextBox1_userID.Text.ToString() + "'";
Label1.Text = TextBox1_userID.Text + db.isExistData(strSql).ToString() + strNewPwd.Equals(strRPwd).ToString();
if (db.isExistData(strSql))
{
if (strNewPwd.Equals(strRPwd))
{
string strSql2 = "update tb_user set pwd='" + strNewPwd + "' where userID='" + TextBox1_userID.Text.ToString() + "'";
if (db.execSql(strSql2))
{
Label1.Text = "密码修改成功!";
}
}
else
{
Label1.Text = "两遍输入密码不一样!";
}
}
db.closeDB();
}
}
7.创建联系人
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class newInfo : System.Web.UI.Page
{
DBManage db;
string strUserID;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1_birthday.Enabled = false;
strUserID = Request.QueryString["userID"].ToString();
HyperLink1_back.NavigateUrl = "~/userInfo.aspx?userID=" + strUserID;
}
else
{
strUserID = Request.QueryString["userID"].ToString();
}
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
TextBox1_birthday.Text = Calendar1.SelectedDate.ToString("yyyy-MM-dd");
}
protected void Button_reg_Click(object sender, EventArgs e)
{
db = new DBManage();
db.sqlConn();
string strName = TextBox2_name.Text.Trim();
string strPhone = TextBox4_phone.Text.Trim();
string strQQ = TextBox1_qq.Text.Trim();
string strRemark = TextBox3_remark.Text;
string strSex = "";
if (RadioButton1.Checked)
{
strSex = "男";
}
if (RadioButton2.Checked)
{
strSex = "女";
}
string strBir = TextBox1_birthday.Text;
string strSql = "select * from tb_info where name='" + strName + "' or phone='"+ strPhone + "'";
if (db.isExistData(strSql))
{
Label1.Text = "该信息已存在!";
return;
}
string strSql2 = "insert into tb_info values('"+ strUserID +"','"+ strName + "','" + strSex + "','"
+ strPhone + "','" + strQQ + "','" + strBir + "','" + strRemark + "')";
Label1.Text = strSql2;
if (db.execSql(strSql2))
{
Label1.Text = "新建联系人成功!";
return;
}
}
}

浙公网安备 33010602011771号