简单通讯录功能虽然简单,却包括了制作一个网站的基本功能!各个模块可以作为新手入门的参考。


简单通讯录实现功能: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;
}
}
}

C# ASP.net 入门之简单通讯录的更多相关文章

  1. 【Asp.net入门07】第一个ASP.NET 应用程序-创建数据模型和存储库

    1.理解概念 先理解一下两个概念. 模型 模型是指数据的结构类型,以及可调用的方法.对面向对象编程方法来说,其实就是类.模型类就是一个描述数据的类.只有把数据按一定方式描述出来,我们才能在程序中方便地 ...

  2. MFC制作简单通讯录程序

    学习c++和MFC一段时间了,苦于没有项目实战,所以自己写了一个简单的简单通讯录程序,以前用c#写简单很多,例程是这本书上的实例,我的第一个winform程序也是从这本书上学的,总结c#写的话更简单, ...

  3. 踢爆IT劣书出版黑幕——由清华大学出版社之《C语言入门很简单》想到的(1)

    1.前言与作者 首先声明,我是由于非常偶然的机会获得<C语言入门很简单>这本书的,绝对不是买的.买这种书实在丢不起那人. 去年这书刚出版时,在CU论坛举行试读推广,我当时随口说了几句(没说 ...

  4. [电子书] 《Android编程入门很简单》

    <Android编程入门很简单>是一本与众不同的Android学习读物,是一本化繁为简,把抽象问题具体化,把复杂问题简单化的书.本书避免出现云山雾罩.晦涩难懂的讲解,代之以轻松活泼.由浅入 ...

  5. ASP开发入门+实战电子书共50本 —下载目录

    小弟为大家整理50个ASP电子书籍,有入门,也有实战电子书,做成了一个下载目录,欢迎大家下载. 资源名称 资源地址 ASP.NET开发实战1200例_第I卷 http://down.51cto.com ...

  6. Redis入门很简单之六【Jedis常见操作】

    Redis入门很简单之六[Jedis常见操作] http://www.tuicool.com/articles/vaqABb http://www.cnblogs.com/stephen-liu74/ ...

  7. 《Mysql 入门很简单》(读后感①)

    下载完整版<Mysql 入门很简单>,点击这里~: http://files.cnblogs.com/files/zhengyeye/MySQL%E5%85%A5%E9%97%A8%E5% ...

  8. Hibernate入门2.简单的项目开发实例

    Hibernate入门2.简单的项目开发实例 这一节通过一个简单的项目学习Hibernate项目的配置 代码下载 : 链接: http://pan.baidu.com/s/1zlgjl 密码: p34 ...

  9. 资深架构师Sum的故事:正则!入门就是这样简单

    | 故事背景 职场如战场!Sum带领三个小队友用了两周,成功把代理功能给干出来了.如果说产品经理是最魔鬼的指挥官,那测试就是最魔鬼的教官.这两周,让Sum深深领略了什么是X市的日出. 不过话又说回来, ...

随机推荐

  1. nexus私服linux搭建问题

    一.最近搭建nexus私服,从官网下载下来总是报503服务器无效,很是无奈,最后在网上找到一个可以用的 收藏起来,这里给大家共享一下 下载地址:http://pan.baidu.com/s/1kT3U ...

  2. C语言库函数大全及应用实例五

    原文:C语言库函数大全及应用实例五                                                 [编程资料]C语言库函数大全及应用实例五 函数名: getcurdi ...

  3. svg的自述

    svg可缩放矢量图形(Scalable Vector Graphics). SVG 使用 XML 格式定义图像. SVG 是使用 XML 来描述二维图形和绘图程序的语言. 什么是SVG? SVG 指可 ...

  4. 让低版本的IE浏览器 强制渲染为IE8 或者 以上 浏览器模式

    让低版本的IE浏览器 强制渲染为IE8 或者 以上 浏览器模式 那么就要用下面的方法:让网页兼容ie9 复制代码 代码如下: <!–[if lte IE 8]> <meta http ...

  5. Qt Creator(编译器MinGW)中使用__attribute__(packed)的问题

    http://www.bttr-software.de/forum/mix_entry.php?id=11767 假设我们从串口中读到一串数据,当我们想要处理这串数据的时候通常是这样做的: 1 将这些 ...

  6. ubuntu下Eclipse无法启动

    我的Ubuntu是12.04 LTS版的.jdk是官网下载后解压就可以用的.配置好PATH和CLASSPATH后,双击Eclipse弹出这个窗口: 但是如果通过终端,以命令行的方式执行 ./eclip ...

  7. 基于Jcrop的图片上传裁剪加预览

    最近自己没事的时候研究了下图片上传,发现之前写的是有bug的,这里自己重新写了一个! 1.页面结构 <!DOCTYPE html> <html lang="en" ...

  8. VMWare 11安装操作系统 - 初学者系列 - 学习者系列文章

    在2010年的时候,我写过一篇关于VMWare的安装操作系统的博文.但是今天在QQ群里有人问起VMWare安装操作系统的问题,虽然回答了,但是回头看了下当时那篇博文,决定重新写一文. 首先要获取VMW ...

  9. INNO SETUP数据库的连接与创建

    原文:INNO SETUP数据库的连接与创建 说明一下:这块程序的前半部分在INNO SETUP的实例里面就有,而后面如果对数据库进行备份和还原在实例里面没有,在网上也不好找,是本人费了好大劲一句一句 ...

  10. 介绍一款基于jquery好用的编辑框htmlbox.full.js

    1. 可选择背景颜色,自主选择工具,感觉挺好用的,不过需要注意,添加引用后找不到工具图标的图片,找到脚本修改idir:属性改成自己的图片文件夹存放路径即可. asp.net mvc3提交内容报错提示含 ...