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市的日出. 不过话又说回来, ...
随机推荐
- Windows socket之最简单的socket程序
原文:Windows socket之最简单的socket程序 最简单的服务器的socket程序流程如下(面向连接的TCP连接 ): 1. WSAStartup(); 初始化网络库的使用. 2. soc ...
- C# 获取与解析枚举类型的 DescriptionAttribute
原文:C# 获取与解析枚举类型的 DescriptionAttribute System.ComponentModel.DescriptionAttribute 这个 Attribute,经常被用来为 ...
- orleans开篇之hello world
orleans开篇之hello world 什么是orleans Orleans是一个建立在.NET之上的,设计的目标是为了方便程序员开发需要大规模扩展的云服务.Orleans项目基本上被认为是并行计 ...
- IT见解
IT见解 北京海淀区 2014-10-18 张俊浩 *域名的市值在走低,因其功能被新浪.腾讯微博.微信大V这种账号所代替 *小米将自己定位为互联网公司,而不是手机公司 *手机不远的未来会成为公共 ...
- PHP支付接口RSA验证
PHP 验签 Sign 验签数据准备: 公钥(Public key) Sign签名(一般是base64加密过的) Data参数(参数列表,Sign对应的参数值) php的openssl扩展里已经封装好 ...
- 客户端Webview重定向
今天在客户端的网页中写了句alert的代码,发现执行了两次,后来发现网页的地址写的是http://192.168.14.72/app 客户端Webview加载网页,对于不完全路径会重定向到完全路径,导 ...
- RPL协议介绍
RPL是IPv6 Routing Protocol for Low-Power and Lossy Networks的简称. 低功耗及有损网络(LLN)是一类内部链接和路由器都受限的网络,该网络下的路 ...
- JTAG应该如何接线
下面是某个ARM9评估板的原理图: 注意: 1. Vref和Vtarget可以直接连在一起,由被调试板提供3.3V或5V电源: 2. nTRST,最好上拉: 3. TDI,最好上拉 4. TMS,最好 ...
- 安装WindowsXP操作系统(Ghost版) - 初学者系列 - 学习者系列文章
Windows XP的Ghost版是经典的版本.因为XP相对较小些,所以用Ghost起来速度比较快.如果Ghost那个Windows 7之类的,速度就慢了.Windows 7建议还是安装比较快.下面简 ...
- ajax 请求数据
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...