首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

写了一个三层的登录,不知道是那出错了!该如何解决

2012-02-07 
写了一个三层的登录,不知道是那出错了!Web层代码:publicpartialclass_Default:System.Web.UI.Page{LoginBu

写了一个三层的登录,不知道是那出错了!
Web层代码:
public   partial   class   _Default   :   System.Web.UI.Page  
{
        LoginBusiness.Business   rule   =   new   LoginBusiness.Business();

        protected   void   Page_Load(object   sender,   EventArgs   e)
        {

        }
        protected   void   btnLogin_Click(object   sender,   EventArgs   e)
        {
                string   user   =this.txtUser.Text.Trim();
                string   userpwd   =   this.txtPwd.Text.Trim();
                bool   flag   =   rule.LoginUser(user,   userpwd);
                if   (flag   ==   true)
                {
                        Response.Write( " <script   language= 'javascript '> alert( '登陆成功 '); </script> ");
                   
                }
                else
                {
                        Response.Write( " <script   language= 'javascript '> alert( '登陆失败 '); </script> ");
                }
        }
}
------
数据层:
namespace   LoginData
{
        public   class   SqlHelper
        {
                public   string   conStr   =   ConfigurationManager.AppSettings[ "ConnectionStrng1 "].ToString();
                public   int   ExcuteNonQuery(string   sql)
                {
                        SqlConnection   conn   =   new   SqlConnection();
                        conn.ConnectionString   =   conStr;
                        SqlCommand   cmd   =   new   SqlCommand();
                        cmd.CommandText   =   sql;
                        try
                        {
                                conn.Open();
                                cmd.ExecuteNonQuery();
                        }
                        catch   (Exception   e)


                        {
                                string   error   =   e.Message;
                                return   0;
                        }
                        finally
                        {
                                conn.Close();
                        }
                        return   1;
                }
                public   DataSet   ExcuteDataSet()
                {
                        DataSet   ds   =   new   DataSet();
                        return   ds;
                }
        }
}
--------
业务层:
namespace   LoginBusiness
{
        public   class   Business
        {
                LoginData.SqlHelper   data   =   new   LoginData.SqlHelper();
//在此设置断点在WEB页面上输入用户名和密码,都能够看到有返回值。
                public   bool   LoginUser(string   username,   string   password)
                {
                        string   sql   =   "select   *   form   username   where   users= "   +   username   +   "and   password= "   +   password;
                        int   flag;
                        flag   =   data.ExcuteNonQuery(sql);
//设置断点查看其值flag=0,失败了!是不是数据库出现了问题还是什么?望大家帮帮忙!
                        if   (flag   ==   1)
                        {
                                return   true;
                        }
                        else
                        {
                                return   false;
                        }
                }


        }
}

[解决办法]
ExcuteNonQuery 这个函数里面是不是完全执行成功了
设置个断点跟踪一下。
[解决办法]
验证永远正确是吧? 因为数据访问层永远会返回1

[解决办法]
把查询语句最好把数据存在DataTable或其他中..

判断table是否为空...
[解决办法]
debug下看你的连接字符是不是为空值!!
[解决办法]
public static string conStr = ConfigurationManager.AppSettings[ "ConnectionStrng1 "].ToString();

// 这里应该是静态的吧?

[解决办法]
conStr变为静态的

热点排行