您的位置:程序门 -> .net技术 -> web services



菜鸟关于利用sql数据库登录网页验证的问题,附编码,求救  :)


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


菜鸟关于利用sql数据库登录网页验证的问题,附编码,求救 :)[已结贴,结贴人:magic_simon]
发表于:2007-03-20 19:29:21 楼主
两个网页,网页1是登录界面,正确的话转到网页2,在网页2上显示登录名,调试没问题,但是真正登录时却只是显示网页1,其他没有任何显示,麻烦高手帮忙看看
以下是代码,
webform1.aspx:


<%@   page   language= "c# "   codebehind= "webform1.aspx.cs "   autoeventwireup= "false "   inherits= "_1.webform1 "   %>
<!doctype   html   public   "-//w3c//dtd   html   4.0   transitional//en "   >
<html>
<head>
<title> webform1 </title>
<meta   name= "generator "   content= "microsoft   visual   studio   .net   7.1 ">
<meta   name= "code_language "   content= "c# ">
<meta   name= "vs_defaultclientscript "   content= "javascript ">
<meta   name= "vs_targetschema "   content= "http://schemas.microsoft.com/intellisense/ie5 ">
</head>
<body   ms_positioning= "gridlayout ">
<form   id= "form1 "   method= "post "   runat= "server ">
<font   face= "宋体 ">
<asp:button   id= "btn_login "   style= "z-index:   106;   left:   384px;   position:   absolute;   top:   200px "
runat= "server "   text= "登录 "> </asp:button>
</font>
<table   id= "table2 "   style= "z-index:   102;   left:   256px;   position:   absolute;   top:   96px "   cellspacing= "1 "
cellpadding= "1 "   width= "300 "   border= "1 ">
<tr>
<td   style= "width:   62px "> 用户名 </td>
<td>
<asp:textbox   id= "textbox1 "   runat= "server "   width= "160px "> </asp:textbox> </td>
</tr>
<tr>
<td   style= "width:   62px "> 密&nbsp;   码 </td>
<td>
<asp:textbox   id= "textbox2 "   runat= "server "   width= "160px "   textmode= "password "> </asp:textbox> </td>
</tr>
</table>
<asp:label   id= "label1 "   style= "z-index:   103;   left:   304px;   position:   absolute;   top:   48px "   runat= "server "> 网页1 </asp:label>
</form>
</body>
</html>


webform1.aspx.cs:

using   system;
using   system.collections;
using   system.componentmodel;
using   system.data;
using   system.drawing;
using   system.web;
using   system.web.sessionstate;
using   system.web.ui;
using   system.web.ui.webcontrols;
using   system.web.ui.htmlcontrols;
using   system.data.sqlclient;  
namespace   _1
{
///   <summary>
///   webform1   的摘要说明。
///   </summary>
public   class   webform1   :   system.web.ui.page
{
protected   system.web.ui.webcontrols.textbox   textbox1;
protected   system.web.ui.webcontrols.textbox   textbox2;
protected   system.web.ui.webcontrols.button   btn_login;
protected   system.web.ui.webcontrols.label   label1;

private   void   page_load(object   sender,   system.eventargs   e)
{
//   在此处放置用户代码以初始化页面
}

#region   web   窗体设计器生成的代码
override   protected   void   oninit(eventargs   e)
{
//
//   codegen:   该调用是   asp.net   web   窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}

///   <summary>
///   设计器支持所需的方法   -   不要使用代码编辑器修改
///   此方法的内容。
///   </summary>
private   void   initializecomponent()
{        
this.load   +=   new   system.eventhandler(this.page_load);

}
#endregion

private   void   btn_login_click(object   sender,   system.eventargs   e)
{

sqlconnection   sqlconn   =   new   sqlconnection( "data   source=(p4);database=1;uid=sa;pwd= ");
sqlconn.open();
sqlcommand   sqlcom   =   sqlconn.createcommand();
sqlcom.commandtext   =   "select   name   from   user   where   name= ' "   +   textbox1.text
+   " '   and   regiondescription= ' "   +   textbox2.text   +   " ' ";
int   count   =   convert.toint32(sqlcom.EXECutescalar());//获取sql语句的值   强制转换成数值类型
if   (count   >   0)
{
session[ "name "]   =   textbox1.text;

page.response.redirect( "webform2.aspx ");
}
else
{
response.write( " <script   lanuage=javascript> alert( '用户名或密码有误! ');location= 'javascript:history.go(-1) ' </script> ");
return;
}

}
}
}


webform2.aspx:

<%@   page   language= "c# "   codebehind= "webform2.aspx.cs "   autoeventwireup= "false "   inherits= "_1.webform2 "   %>
<!doctype   html   public   "-//w3c//dtd   html   4.0   transitional//en "   >
<html>
<head>
<title> webform2 </title>
<meta   name= "generator "   content= "microsoft   visual   studio   .net   7.1 ">
<meta   name= "code_language "   content= "c# ">
<meta   name= "vs_defaultclientscript "   content= "javascript ">
<meta   name= "vs_targetschema "   content= "http://schemas.microsoft.com/intellisense/ie5 ">
</head>
<body   ms_positioning= "gridlayout ">
<form   id= "form1 "   method= "post "   runat= "server ">
<font   face= "宋体 ">
<asp:label   id= "label1 "   style= "z-index:   101;   left:   200px;   position:   absolute;   top:   128px "   runat= "server "
width= "256px "   height= "80px "> </asp:label> </font>
</form>
</body>
</html>


webform2.aspx.cs:

using   system;
using   system.collections;
using   system.componentmodel;
using   system.data;
using   system.drawing;
using   system.web;
using   system.web.sessionstate;
using   system.web.ui;
using   system.web.ui.webcontrols;
using   system.web.ui.htmlcontrols;
using   system.data.sqlclient;
namespace   _1
{
///   <summary>
///   webform2   的摘要说明。
///   </summary>
public   class   webform2   :   system.web.ui.page
{
protected   system.web.ui.webcontrols.label   label1;

private   void   page_load(object   sender,   system.eventargs   e)
{
  label1.text   =   session[ "name "].tostring();
}

#region   web   窗体设计器生成的代码
override   protected   void   oninit(eventargs   e)
{
//
//   codegen:   该调用是   asp.net   web   窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}

///   <summary>
///   设计器支持所需的方法   -   不要使用代码编辑器修改
///   此方法的内容。
///   </summary>
private   void   initializecomponent()
{        
this.load   +=   new   system.eventhandler(this.page_load);

}
#endregion
}
}
发表于:2007-03-20 19:36:221楼 得分:0
数据库为p4   下的   user   ,表中内容为id:1,name:张三,password:0000......
发表于:2007-03-20 23:22:462楼 得分:0
沉了,自己顶一下,不好意思
发表于:2007-03-21 00:40:183楼 得分:20
按照你的思路:
数据库为p4   下的   user   ,表中内容为id:1,name:张三,password:0000......
有三个字段:id   name   password   那么下面这句代码regiondescription从何而来
sqlcom.commandtext   =   "select   name   from   user   where   name= ' "   +   textbox1.text
+   " '   and   regiondescription= ' "   +   textbox2.text   +   " ' ";
大哥,请设断点调试啊!
我的思路:用datatable和sqldataadapter将user导入datatable,然后和textbox判断来决定动作。个人认为你上面的方法不好。
发表于:2007-03-21 10:31:594楼 得分:0
apumam()   :  
        谢谢你的深夜回复   :)     请问能按你的方法给我个源程序吗,毕竟条条大路通罗马,我又是个菜鸟,多学点以后就少走些弯路。
发表于:2007-03-21 10:32:525楼 得分:0
我的邮箱   :   magic_simon@msn.com
直接贴到这里也行     :)
发表于:2007-03-21 11:27:266楼 得分:0
各位高手也请帮帮忙,谢谢啦
发表于:2007-03-28 00:20:587楼 得分:0
sqldataadapter   sda   =   new   sqldataadapter( "select... ",sqlconn);
//注意不用显式的开,关sqlconn.
datatable   datatable   =   new   datatable();//可起名或不起名
sda.fill(datatable);//datatable已经得到表数据
//和session值进行判断
if(datatable[int类型行数][int类型列数].tostring()   ==   session[ "name "].tostring())
    {
            ...
    }

其实我也是菜鸟,后来老师告诫我们:sql语句先到查询分析器试成功再用.
我的邮箱:   sunyue3228@sina.com   欢迎交流


快速检索

最新资讯
热门点击