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;
}
}
}
C# ASP.net 入门之简单通讯录的更多相关文章
- 【Asp.net入门07】第一个ASP.NET 应用程序-创建数据模型和存储库
1.理解概念 先理解一下两个概念. 模型 模型是指数据的结构类型,以及可调用的方法.对面向对象编程方法来说,其实就是类.模型类就是一个描述数据的类.只有把数据按一定方式描述出来,我们才能在程序中方便地 ...
- MFC制作简单通讯录程序
学习c++和MFC一段时间了,苦于没有项目实战,所以自己写了一个简单的简单通讯录程序,以前用c#写简单很多,例程是这本书上的实例,我的第一个winform程序也是从这本书上学的,总结c#写的话更简单, ...
- 踢爆IT劣书出版黑幕——由清华大学出版社之《C语言入门很简单》想到的(1)
1.前言与作者 首先声明,我是由于非常偶然的机会获得<C语言入门很简单>这本书的,绝对不是买的.买这种书实在丢不起那人. 去年这书刚出版时,在CU论坛举行试读推广,我当时随口说了几句(没说 ...
- [电子书] 《Android编程入门很简单》
<Android编程入门很简单>是一本与众不同的Android学习读物,是一本化繁为简,把抽象问题具体化,把复杂问题简单化的书.本书避免出现云山雾罩.晦涩难懂的讲解,代之以轻松活泼.由浅入 ...
- ASP开发入门+实战电子书共50本 —下载目录
小弟为大家整理50个ASP电子书籍,有入门,也有实战电子书,做成了一个下载目录,欢迎大家下载. 资源名称 资源地址 ASP.NET开发实战1200例_第I卷 http://down.51cto.com ...
- Redis入门很简单之六【Jedis常见操作】
Redis入门很简单之六[Jedis常见操作] http://www.tuicool.com/articles/vaqABb http://www.cnblogs.com/stephen-liu74/ ...
- 《Mysql 入门很简单》(读后感①)
下载完整版<Mysql 入门很简单>,点击这里~: http://files.cnblogs.com/files/zhengyeye/MySQL%E5%85%A5%E9%97%A8%E5% ...
- Hibernate入门2.简单的项目开发实例
Hibernate入门2.简单的项目开发实例 这一节通过一个简单的项目学习Hibernate项目的配置 代码下载 : 链接: http://pan.baidu.com/s/1zlgjl 密码: p34 ...
- 资深架构师Sum的故事:正则!入门就是这样简单
| 故事背景 职场如战场!Sum带领三个小队友用了两周,成功把代理功能给干出来了.如果说产品经理是最魔鬼的指挥官,那测试就是最魔鬼的教官.这两周,让Sum深深领略了什么是X市的日出. 不过话又说回来, ...
随机推荐
- Delphi三层网络架构代码实现
Delphi三层网络架构代码实现 1 .三层网络的概念 三层架构(3-tier architecture) 通常意义上的三层架构就是将整个业务应用划分为: 表现层(UI).业务逻辑层(BLL).数据访 ...
- 收集整理的非常有用的PHP函数
原文:收集整理的非常有用的PHP函数 项目中经常会需要一些让人头疼的函数,作为开发者应该整理一个自己的函数库,在需要之时复制过来即可.本文作者收集整理数十个PHP项目中常用的函数,保证能正常运行,你只 ...
- Cacti+Nagios监控平台完美整合
Cacti+Nagios监控平台完美整合 本博文出自51CTO博客吴光科博主,有任何问题请进入博主页面互动讨论!博文地址:http://wgkgood.blog.51cto.com/1192594/1 ...
- 安装Windows8.1操作系统 - 初学者系列 - 学习者系列文章
Windows 8这款操作系统是微软最新的操作系统.虽然微软做了推广,但是据消息称市场份额暂时没那么高.下面就对该操作系统的安装进行简要介绍. 1. 将光盘装入光驱,设置BIOS中光驱启动,启动计算 ...
- Android 获取截图 并将其保存到本地sd在卡路径
/** * 获取当前屏幕和保存截图 */ private void GetandSaveCurrentImage() { //1.构建Bitmap WindowManager windowManage ...
- 《互联网初创企业password》书评
今天试用了一下<互联网初创企业password>这本书.我觉得这本书开始的很真实,从学校刚毕业那会儿.它是生命,他们的牛b时间,一直想做点什么来证明自己.具体地,并且现在是在最好的时候.互 ...
- 缓存,spring
applicationcontext.xml xmlns:cache="http://www.springframework.org/schema/cache" xsi:schem ...
- Mac OS X下环境搭建 Sublime Text 2 环境变量配置 开发工具配置Golang (Go语言)
Golang (Go语言) Mac OS X下环境搭建 环境变量配置 开发工具配置 Sublime Text 2 一.安装Golang的SDK 在官网http://golang.org/ 直接下载安装 ...
- Bootstrap 图标
Bootstrap 图标由 Glyphicons 提供.详情可以去bootstrap官网进行查看. 用法: <i class="icon_class_name">< ...
- CLR中的垃圾回收机制
CLR中采用代(generation)来作为其垃圾回收的一种机制,其唯一的目的是提升程序的性能.基予代的垃圾回收器有以下假设: ·对象越新,其生存周期越短. ·对象越老,其生存周期越长. ·回收堆的一 ...