使用 ASP.NET 为自己做一个相亲网站
文章目录一、效果预览二、源码下载三、需要用到的知识四、系统功能结构五、数据库的设计六、部分代码展示1、DataBase.cs类 操作数据库2、f.cs类json解析3、MarriageMsg.cs类发送信息一、效果预览相亲网站二、源码下载源码已经上传到我的GitHu...
·
一、效果预览
相亲网站
二、需要用到的知识
三、系统功能结构
四、数据库的设计
use db_JiaoYou
go
create table tb_User(
ID int primary key,
UserName varchar(100),
PassWord varchar(30),
NickName varchar(30),
Sex varchar(4),
BirthDay varchar(20),
Address varchar(150),
Stature varchar(10),
Avoirdupois varchar(10),
Education varchar(10),
Earning varchar(10),
Nation varchar(10),
BloodType varchar(6),
Looks varchar(20),
Metier varchar(20),
Housing varchar(20),
BuyCar varchar(20),
Marriage varchar(20),
HaveBaby varchar(6),
HavingBaby varchar(20),
Smoke varchar(6),
Dirnk varchar(6),
UseLanguage varchar(50),
SexLike varchar(500),
FriendCome varchar(150),
FriendAgeStar int,
FriendAgeEnd int,
FriendStatureStar int,
FriendMarriage varchar(10),
PhotoPath varchar(500)
)
create table tb_Msg(
ID int primary key,
Sender varchar(30),
Accepter varchar(30),
Msg text,
MsgDate datetime,
CheckRead bit,
)
create table tb_Friend(
ID int primary key,
UserName varchar(30),
FriendName varchar(30),
)
五、部分代码展示
1、DataBase.cs类 操作数据库
/// <summary>
/// DataBase 数据层类
/// </summary>
public class DataBase:IDisposable
{
public DataBase()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private SqlConnection con; //创建连接对象
#region 打开数据库连接
/// <summary>
/// 打开数据库连接.
/// </summary>
private void Open()
{
// 打开数据库连接
if (con == null)
{
con = new SqlConnection("Data Source=(local);DataBase=db_JiaoYou;Trusted_Connection=SSPI;");
}
if (con.State == System.Data.ConnectionState.Closed)
con.Open();
}
#endregion
#region 关闭连接
/// <summary>
/// 关闭数据库连接
/// </summary>
public void Close()
{
if (con != null)
con.Close();
}
#endregion
#region 释放数据库连接资源
/// <summary>
/// 释放资源
/// </summary>
public void Dispose()
{
// 确认连接是否已经关闭
if (con != null)
{
con.Dispose();
con = null;
}
}
#endregion
#region 传入参数并且转换为SqlParameter类型
/// <summary>
/// 转换参数
/// </summary>
/// <param name="ParamName">存储过程名称或命令文本</param>
/// <param name="DbType">参数类型</param>
/// <param name="Size">参数大小</param>
/// <param name="Value">参数值</param>
/// <returns>新的Parameter对象</returns>
public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
}
/// <summary>
/// 初始化参数值
/// </summary>
/// <param name="ParamName">存储过程名称或命令文本</param>
/// <param name="DbType">参数类型</param>
/// <param name="Size">参数大小</param>
/// <param name="Direction">参数方向</param>
/// <param name="Value">参数值</param>
/// <returns>新的 parameter 对象</returns>
public SqlParameter MakeParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value)
{
SqlParameter param;
if (Size > 0)
param = new SqlParameter(ParamName, DbType, Size);
else
param = new SqlParameter(ParamName, DbType);
param.Direction = Direction;
if (!(Direction == ParameterDirection.Output && Value == null))
param.Value = Value;
return param;
}
#endregion
#region 执行参数命令文本(无数据库中数据返回)
/// <summary>
/// 执行命令
/// </summary>
/// <param name="procName">命令文本</param>
/// <param name="prams">参数对象</param>
/// <returns></returns>
public int RunProc(string procName, SqlParameter[] prams)
{
SqlCommand cmd = CreateCommand(procName, prams);
cmd.ExecuteNonQuery();
this.Close();
//得到执行成功返回值
return (int)cmd.Parameters["ReturnValue"].Value;
}
/// <summary>
/// 直接执行SQL语句
/// </summary>
/// <param name="procName">命令文本</param>
/// <returns></returns>
public int RunProc(string procName)
{
this.Open();
SqlCommand cmd = new SqlCommand(procName, con);
cmd.ExecuteNonQuery();
this.Close();
return 1;
}
#endregion
#region 执行参数命令文本(有返回值)
/// <summary>
/// 执行查询命令文本,并且返回DataSet数据集
/// </summary>
/// <param name="procName">命令文本</param>
/// <param name="prams">参数对象</param>
/// <param name="tbName">数据表名称</param>
/// <returns></returns>
public DataSet RunProcReturn(string procName, SqlParameter[] prams, string tbName)
{
SqlDataAdapter dap = CreateDataAdaper(procName, prams);
DataSet ds = new DataSet();
dap.Fill(ds, tbName);
this.Close();
//得到执行成功返回值
return ds;
}
/// <summary>
/// 执行命令文本,并且返回DataSet数据集
/// </summary>
/// <param name="procName">命令文本</param>
/// <param name="tbName">数据表名称</param>
/// <returns>DataSet</returns>
public DataSet RunProcReturn(string procName, string tbName)
{
SqlDataAdapter dap = CreateDataAdaper(procName, null);
DataSet ds = new DataSet();
dap.Fill(ds, tbName);
this.Close();
//得到执行成功返回值
return ds;
}
#endregion
#region 将命令文本添加到SqlDataAdapter
/// <summary>
/// 创建一个SqlDataAdapter对象以此来执行命令文本
/// </summary>
/// <param name="procName">命令文本</param>
/// <param name="prams">参数对象</param>
/// <returns></returns>
private SqlDataAdapter CreateDataAdaper(string procName, SqlParameter[] prams)
{
this.Open();
SqlDataAdapter dap = new SqlDataAdapter(procName, con);
dap.SelectCommand.CommandType = CommandType.Text; //执行类型:命令文本
if (prams != null)
{
foreach (SqlParameter parameter in prams)
dap.SelectCommand.Parameters.Add(parameter);
}
//加入返回参数
dap.SelectCommand.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int, 4,
ParameterDirection.ReturnValue, false, 0, 0,
string.Empty, DataRowVersion.Default, null));
return dap;
}
#endregion
#region 将命令文本添加到SqlCommand
/// <summary>
/// 创建一个SqlCommand对象以此来执行命令文本
/// </summary>
/// <param name="procName">命令文本</param>
/// <param name="prams"命令文本所需参数</param>
/// <returns>返回SqlCommand对象</returns>
private SqlCommand CreateCommand(string procName, SqlParameter[] prams)
{
// 确认打开连接
this.Open();
SqlCommand cmd = new SqlCommand(procName, con);
cmd.CommandType = CommandType.Text; //执行类型:命令文本
// 依次把参数传入命令文本
if (prams != null)
{
foreach (SqlParameter parameter in prams)
cmd.Parameters.Add(parameter);
}
// 加入返回参数
cmd.Parameters.Add(
new SqlParameter("ReturnValue", SqlDbType.Int, 4,
ParameterDirection.ReturnValue, false, 0, 0,
string.Empty, DataRowVersion.Default, null));
return cmd;
}
#endregion
}
2、f.cs类json解析
/// <summary>
/// f 的摘要说明
/// </summary>
public static class f
{
public static string ToJson(this DataTable dt)
{
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
ArrayList arrayList = new ArrayList();
foreach (DataRow dataRow in dt.Rows)
{
Dictionary<string, object> dictionary = new Dictionary<string, object>(); //实例化一个参数集合
foreach (DataColumn dataColumn in dt.Columns)
{
dictionary.Add(dataColumn.ColumnName, dataRow[dataColumn.ColumnName].ToString());
}
arrayList.Add(dictionary); //ArrayList集合中添加键值
}
return "{root:" + javaScriptSerializer.Serialize(arrayList) + "}"; //返回一个json字符串
}
}
3、MarriageMsg.cs类发送信息
//引入
using System.Data.SqlClient;
/// <summary>
/// MarriageMsg 的摘要说明
/// </summary>
public class MarriageMsg
{
DataBase data = new DataBase();
public MarriageMsg()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region 发送信息实体类
private string sender = "";
private string accepter = "";
private string msg = "";
private bool checkRead = false;
/// <summary>
/// 发送方
/// </summary>
public string Sender
{
get { return sender; }
set { sender = value; }
}
/// <summary>
/// 接收方
/// </summary>
public string Accepter
{
get { return accepter; }
set { accepter = value; }
}
/// <summary>
/// 发送信息
/// </summary>
public string Msg
{
get { return msg; }
set { msg = value; }
}
/// <summary>
/// 阅读状态
/// </summary>
public bool CheckRead
{
get { return checkRead; }
set { checkRead = value; }
}
#endregion
/// <summary>
/// 添加短消息到数据库
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
public int MsgAdd(MarriageMsg msg)
{
SqlParameter[] parms ={
data.MakeInParam("@Sender",SqlDbType.VarChar,100,msg.Sender),
data.MakeInParam("@Accepter",SqlDbType.VarChar,100,msg.Accepter),
data.MakeInParam("@Msg",SqlDbType.Text,0,msg.Msg),
};
return data.RunProc("Insert into tb_Msg(Sender, Accepter,Msg) values (@Sender, @Accepter,@Msg)", parms);
}
/// <summary>
/// 获取接收者短信息
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
public DataSet MsgSelectAccepterMsg(MarriageMsg msg)
{
SqlParameter[] parms ={
data.MakeInParam("@Accepter",SqlDbType.VarChar,100,msg.Accepter),
};
return data.RunProcReturn("SELECT * FROM v_msg where Accepter=@Accepter order by id desc", parms, "v_msg");
}
/// <summary>
/// 获取接收者短信息
/// </summary>
/// <param name="msg">信息实例对象</param>
/// <param name="CheckRead">True已阅读 False未阅读</param>
/// <returns></returns>
public DataSet MsgSelectAccepterMsg(MarriageMsg msg, bool CheckRead)
{
SqlParameter[] parms ={
data.MakeInParam("@Accepter",SqlDbType.VarChar,100,msg.Accepter),
data.MakeInParam("@CheckRead",SqlDbType.Bit,1,CheckRead),
};
return data.RunProcReturn("SELECT * FROM v_msg where Accepter=@Accepter and CheckRead=@CheckRead order by id desc", parms, "v_msg");
}
/// <summary>
/// 根据ID查询短信息
/// </summary>
/// <param name="ID"></param>
/// <returns></returns>
public DataSet MsgSelectID(string ID)
{
return data.RunProcReturn("Select * from tb_Msg where id =" + ID, "tb_msgid");
}
/// <summary>
/// 获取删除短信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public int MsgDelete(string id)
{
return data.RunProc("delete from tb_Msg where id=" + id);
}
/// <summary>
/// 修改阅读状态
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public int MsgUpdate(string id)
{
return data.RunProc("Update tb_Msg set CheckRead=1 where id=" + id);
}
}
篇幅有限,仅展示这些代码
所有使用的素材来自 明日科技, 感谢明日科技书籍《ASP.NET实战入门》
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献2条内容
所有评论(0)