为了使一篇文章就能介绍完整个示例,我选用了简单的:登陆+注册+留言版本功能,来一步一步介绍如何使用的:
当前环境是:vs2005+sql2005
从数据库说起吧,我们创建一个新的数据库,起名叫:message
接着新建两个表:users+message,上图:
为避免本篇节过长,本示例只选用最关键的字段了,大伙可以根据上面的图自己创建数据库与表。
接着我们新建网站来示例[这里没选择新建应用程序,怕个别人没装sp1补丁,示例打不开!]
接下来一步一脚印了:
1:新建网站项目,如起名叫:cyq.data.messagedemoproject
新建完后,f5运行,产生配置文件web.config先,接着在配置文件里添加数据库链接:
<connectionstrings>
<add name="conn" connectionstring="server=.;database=message;uid=sa;pwd=123456"/>
</connectionstrings>
2:添加引用:cyq.data.dll
:
浏览定位选择cyq.data.dll
3:生成分页查询存储过程与枚举:我们新建页面:writeout.aspx,用于生成输出:
a:生成分页存储过程:
protected void page_load(object sender, eventargs e)
{
//输出分页存储过程
response.write(cyq.data.sql.outputdata.getselectbaseoutputtohtmlforsql2005());
}
写好后,右键浏览该页面,把输出的信息“ctrl+a -> ctrl+c"全选->复制
我们把生成的分页存储过程到数据库里执行一下:
b:生成表枚举
我们还是在当前页面的里敲代码:
{
//输出存储过程
//response.write(cyq.data.sql.outputdata.getselectbaseoutputtohtmlforsql2005());
//输出表视图枚举:
cyq.data.sql.outputdata data = new cyq.data.sql.outputdata();
response.write(data.outputalltableenum(cyq.data.sql.outputdata.tabletype.u, cyq.data.sql.outputdata.fileddescriptiontype.sql2005));
}
我们注释掉了生成存储过程的,添加了生成表枚举,说明参数:
tabletype.u:指生成表枚举
tabletype.v:指生成视图枚举
dileddescriptiontype.nodescription:不生成注释
dileddescriptiontype.sql2005:生成注释--对应sql 2005
dileddescriptiontype.sql2000:生成注释--对应sql 2000
备注:注释信息取自数据库字段的说明,如果数据库字段说明没写东西,生成的就是no descrpiotn
接着,我们新添加一个类:tablenames,来存放生成的枚举信息:生成后的类默认会在app_code文件夹下:
4:开始项目:我们新建两个页面:登陆(login.aspx);注册(reg.aspx);留言界面[默认已有](default.aspx)
a:注册用户:界面如下图,html代码大伙自己布局了:
如图:只有用户名和密码:双击进“提交注册”按钮事件里敲入以下代码:
protected void btnreg_click(object sender, eventargs e)
if (txtpassword.text != txtpasswordagain.text)
{
lbmsg.text = "两次密码不一致!";
return;
}
maction action = new maction(tablenames.users);
if (action.getcount(string.format("username='{0}'", txtusername.text.trim())) > 0)
lbmsg.text = "用户已存在!";
action.close();
else
action.getfrom(txtusername);
action.getfrom(txtpassword);
if (action.insert())
{
session["id"] = action.get<int>(users.id);
action.close();
response.redirect("default.aspx");
}
代码不多,大伙看的懂我就不解释了,注册成功后转向default.aspx页面!
b:登陆界面:截图如下,界面加多了个链接到注册页面去了:
同样双击“登陆”进入后台代码如下:
protected void btnlogin_click(object sender, eventargs e)
if(action.fill(string.format("username='{0}' and password='{1}'", txtusername.text.trim(), txtpassword.text.trim())))
session["id"] = action.get<int>(users.id);
response.redirect("default.aspx");
lbmsg.text = "用户密码错误!";
代码同样很简洁,登陆后记录下session,转向default.aspx页面:
c:留言页面:我们来看一下界面:[这里先清除下数据库的数据,重新注册添加一点数据]
我先清除以前的旧数据,数据库sql执行一下语句:
truncate table [message]
truncate table users
好,重新注册下,留下言,上图:
如上图所示,整个页面分为:
1:最左上角的欢迎
2:三个列表[注册用户/我的留言/所有留言],分别使用gridview/datalist/repeater绑定
3:右下角的提交留言
我们一个一个看代码:
1:最左上角的欢迎:判断没登陆后就直接转向登陆,如果登陆,直接用session["id"]填充数据
protected void loadmyinfo()
if (session["id"] == null)
response.redirect("login.aspx");
maction action = new maction(tablenames.users);
if (action.fill(session["id"]))
action.setto(labusername);
else
labusername.text = "读取数据失败!";
2:接下来三个列表的绑定:
protected void loaduserlistinfo()//gridview 绑定用户注册信息
gvusers.datasource = action.select();
gvusers.databind();
action.close();
protected void loadmymessagelist()//datalist 绑定我的留言
int count;
maction action = new maction(tablenames.message);
dalmymessage.datasource = action.select(1, 10, string.format("userid={0}", session["id"]), out count);
dalmymessage.databind();
protected void loadallmessagelist()//repeater 绑定所有用户留言
maction action = new maction(customersql.message);
rptmessage.datasource = action.select();
rptmessage.databind();
最后一个绑定所有用户留言时,涉及两个表关联,所以传入的表名是customersql.message,这个是怎么来的?
我app_code文件夹下我新建一个类:customersql,来统一管理sql:
public class customersql
public const string message = "(select m.*,u.username from message m left join users u on m.userid=u.id) v";
最后,我们需要在page_load方法里加载上面的几个方法:
loadmyinfo();
loaduserlistinfo();
loadmymessagelist();
loadallmessagelist();
3:提交留言:提交完后,自我刷新一下界面。
protected void btnsubmit_click(object sender, eventargs e)
action.getfrom(txtbody);
action.set(message.userid, session["id"]);
action.insert();
response.redirect(request.rawurl);
4:还有个退出呢:
protected void btnlogout_click(object sender, eventargs e)
session["id"] = null;
response.redirect("login.aspx");
至此,一个注册+登陆+留言功能的小项目,就轻松写完了,拿上面的代码和你以前写的代码比较下,感觉哪个简洁方便实用呢?
如果用你目前使用的方法写一个一样的,需要花费的时间和精力又是多少?欢迎拍砖留言!
版权声明:本文原创发表于博客园,作者为路过秋天,原文链接:
http://www.cnblogs.com/cyq1162/archive/2010/08/18/1801518.html
<a href="http://files.cnblogs.com/cyq1162/cyq.data.messagedemoproject.rar"></a>