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市的日出. 不过话又说回来, ...
随机推荐
- java中Integer包装类的具体解说(java二进制操作,全部进制转换)
程序猿都非常懒,你懂的! 今天为大家分享的是Integer这个包装类.在现实开发中,我们往往须要操作Integer,或者各种进制的转换等等.我今天就为大家具体解说一下Integer的使用吧.看代码: ...
- 单元测试之Qunit
单元测试之Qunit 前言 因为公司开发了一套javascript SDK需要测试,在网上找了很久,找到了JQuery团队开发的QUnit,和基于JUnit的JsUnit,还有一些还没有看,先讲讲QU ...
- 关于session_start()这个问题
关于session_start()这个问题,其实网上很多解决的方法,论坛也好多人回答这类的问题, 现在的状况是依然有警告提示Warning: session_start() [function.ses ...
- sql点滴37—mysql中的错误Data too long for column '' at row 1
原文:sql点滴37-mysql中的错误Data too long for column '' at row 1 1.MYSQL服务 我的电脑——(右键)管理——服务与应用程序——服务——MYSQ ...
- Visual Studio Contact
Visual Studio Contact(); 直播笔记 昨天微软干了几件了不起的事:.NET开发环境将开源.跨平台支持(Mac OS X和Linux).多设备支持(WP.Android和iOS ...
- Mike and Feet(CF 547B)
Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- android shape总结 和控制的风格定制
1:shape总结 1):shape文件是放置在drawable文件下的.res/drawable/filename.xml. 2):shape类型:android:shape. 一共同拥有四种:re ...
- Angularjs 与Ckeditor
Angularjs 与Ckeditor Angularjs 诞生于Google是一款优秀的前端JS框架,已经被用于Google的多款产品当中.AngularJS有着诸多特性,最为核心的是:MVC.模块 ...
- BitMap画图
package com.example.examples_05_07; import android.content.Context; import android.graphics.Bitmap; ...
- WinForm媒体播放器
媒体播放控件(Windows Media Player )的常用属性和方法,并且利用它设计一个简单的媒体应用程序——媒体播放器.该媒体播放器可以播放 wav.avi.mid 和 mp3 等格式的文件. ...