做一个登录界面~
登录表里有用户ID,密码,用户姓名等等用户信息
用户输入ID,密码两个信息,满足时进入管理界面这个是我写的数据层的代码
//EmployeeInfoTable  实体类
public EmployeeInfoTable Login(EmployeeInfoTable eif)
        {
            DbOperate dbo = new DbOperate();
            SqlCommand com = new SqlCommand();
            com.CommandText = string.Format("select *from EmployeeInfoTable where EIPwd='{0}' and EIID={1}",eif .EIPwd ,eif .EIID );            dbo.TableName = EmployeeInfoTableDataOperate.TABLENAME;
            DataSet ds = dbo.Select(com );
            return eif;
        }在表示层应该怎么用条件判断输入密码和ID正确呢?
请前辈们指点一下。

解决方案 »

  1.   

    private void btn_Login_Click(object sender, System.EventArgs e)
    {
    for (int i=0;i<myTable.Rows.Count;i++)
    {
    this.myRow = myTable.Rows[i];
    //只有当输入的用户名和密码同时对应上数据库中记录时,才能通过校验
    if (myRow[0].ToString().Trim()==this.txt_ID.Text.ToString().Trim() && myRow[4].ToString().Trim()==this.txt_Pwd.Text.ToString().Trim())
    {
    blCanLogin = true;
    strUser = myRow[0].ToString().Trim();//保存用户名
    strDepartment = myRow[1].ToString().Trim();//保存部门名称
    this.Close();//关闭窗体
    return;
    }
    }
    MessageBox.Show( "您输入的用户号或密码不正确!");
    return;
    }
      

  2.   

    感觉用reader读取比较好,用dataset占用资源比较大.
    另外不建议用select *, 用count(*) 好一些.
    reader = cmd.ExecuteReader();
    if(reader.read())
    {   int count = reader.GetInt32(0);
         if(count < 1) //密码错误 }  
      

  3.   

    我爱罗同学,我觉得Login的方法这样写比较好:public EmployeeInfoTable Login(EmployeeInfoTable eif)
    {
         //在EmployeeInfoTable中根据传入密码和ID查询出当前用户
            List<EmployeeInfoTable> userlist= repository.GetByConditions("EIID='"+eif.EIID+"'","EIPwd='"+eif.EIPwd+"'");
            
         //如果查询结果为空则该用户不存在
            if (userlist != null)
            {
                return userlist[0];
            }
            else
            {
                return null;
            }
    }这样,表示层可以通过判断Login(currentuser)是否为null判断出输入密码和ID是否正确。
      

  4.   

    bool success=false;            SqlConnection connection=new SqlConnection();
                connection.ConnectionString="";//你的数据库连接字符串
                SqlCommand com = new SqlCommand(); 
                com.CommandText = string.Format("select *from EmployeeInfoTable where EIPwd='{0}' and EIID={1}",eif .EIPwd ,eif .EIID ); 
                connection.Open();
                SqlDataReader reader=com.ExecuteReader();
    if(reader.Read())
    {
    success=true;//表示找到对应的用户名和密码
    }
    reader.Close();
    con.Close();
      

  5.   


    要分开的话,干脆你的数据层的那个方法返回一个bool变量嘛
      

  6.   

    无知的批判下楼主的代码:
    按照楼主的思路,修改下楼主的设计:
    原方法修改下名字,功能是取得用户信息,这样这个方法在复用的时候就不会有歧义了。
    public EmployeeInfoTable GetUserInfo(EmployeeInfoTable eif) 
            { 
                DbOperate dbo = new DbOperate(); 
                SqlCommand com = new SqlCommand(); 
                com.CommandText = string.Format("select *from EmployeeInfoTable where EIPwd='{0}' and EIID={1}",eif .EIPwd ,eif .EIID );             dbo.TableName = EmployeeInfoTableDataOperate.TABLENAME; 
                DataSet ds = dbo.Select(com ); 
                return eif; 
            } 
    增加一个方法,判断用户是否可以登录
    public bool CanLogin(EmployeeInfoTable eif)
    {
        EmployeeInfoTable eit = GetUserInfo(eif);
        if( eit != null && eit.Rows.Count > 0 )
        {
            return true;
        }
        return false;
    }
    在表示层调用后面方法进行判断是否可登录设计中尽量让你的单个方法的功能单一,不要很多功能都搞到一个方法里以上愚见,欢迎拍砖
      

  7.   

    呵呵~~谢谢大家了~~试了很久~但都不行~首先EmployeeInfoTable表里不只ID和密码两列,还有员工姓名,性别部门等等的信息
    2楼应该是最明白我代码的但是还是不行~我是个菜鸟~到底哪不对我也不怎么明白~应该是上面那个原因吧还有就是我代码里的DbOperate 类,我写好配置文件了。。所以在数据层数据库连接代码就省了using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Data.SqlTypes;
    using System.Configuration;namespace SuperMarket.DAL
    {
    public class DbOperate
    {
    private SqlConnection con;
    private SqlCommand com;
    private SqlDataAdapter adpt;
    private DataSet ds;
    private string _tableName;
    private SqlTransaction tran;
    public string TableName
    {
    get{return this._tableName;}
    set{this._tableName=value;}
    }
    public DbOperate()
    {
    this.con=new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
    this.com=con.CreateCommand();
    this.adpt=new SqlDataAdapter();
    this.ds=new DataSet();
    this._tableName="";
    } private void BeginTran()
    {
    if(this.com!=null && (this.com.CommandText!="" || this.com.CommandText!=null))
    {
    this.tran=this.con.BeginTransaction();
    }
    } public DataSet Select(SqlCommand dcom)
    {
                this.com = dcom;
                this.com.Connection = this.con;
                this.con.Open();
                this.adpt.SelectCommand = this.com;            this.BeginTran();
                this.com.Transaction = this.tran;
    try
    {
    this.adpt.Fill(this.ds,this.TableName);
    this.tran.Commit();
    }
    catch(Exception ex)
    {
    this.tran.Rollback();
    throw ex;
    }
    finally
    {
    this.con.Close();
    } return this.ds;
    }
    public DataSet MuttlineUpdate(DataSet ds,SqlCommand dcom)
    {            this.com = dcom;
                this.com.Connection = this.con;
                this.adpt.UpdateCommand = this.com;
                this.con.Open();            this.BeginTran();
                this.com.Transaction = this.tran;
    try
    {
    this.adpt.Update(ds,this.TableName);
    this.tran.Commit();
    }
    catch(Exception ex)
    {
    this.tran.Rollback();
    throw ex;
    }
    finally
    {
    this.con.Close();
    } return this.ds;
    } public int UpDate(SqlCommand dcom)
    {
    int result=0;
                this.con.Open();            this.com = dcom;
                this.com.Connection = this.con;            this.BeginTran();
                this.com.Transaction = this.tran;
    try
    {
    result=this.com.ExecuteNonQuery();
    this.tran.Commit();
    }
    catch(Exception ex)
    {
    this.tran.Rollback();
    throw ex;
    }
    finally
    {
    this.con.Close();
    } return result;
    } public int Delete(SqlCommand dcom)
    {
    int result=0;
                this.com = dcom;
                this.com.Connection = this.con;
                this.con.Open();
                this.BeginTran();
                this.com.Transaction = this.tran;
    try
    { result=this.com.ExecuteNonQuery();
    this.tran.Commit();
    }
    catch(Exception ex)
    {
    this.tran.Rollback();
    throw ex;
    }
    finally
    {
    this.con.Close();
    } return result;
    } public int Insert(SqlCommand dcom)
    {
    int result=0;
                this.com = dcom;
                this.com.Connection = this.con;
                this.con.Open();
                this.BeginTran();
                this.com.Transaction = this.tran;
    try
    {
    result=this.com.ExecuteNonQuery();
    this.tran.Commit();
    }
    catch(Exception ex)
    {
    this.tran.Rollback();
    throw ex;
    }
    finally
    {
    this.con.Close();
    } return result;
    }
    }
    }配置文件:
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <connectionStrings>
    <add name="ConnectionString" connectionString="server=127.0.0.1;uid=sa;pwd=;database=SuperMarket"/>
    </connectionStrings>
    </configuration>4楼那位前辈的代码我没看懂~呵呵~ repository.GetByConditions()我没弄懂~
      

  8.   

    一般来说写个登录校验函数,返回 bool,接收uid,pass 然后查库返回true or false,public bool CheckLogin(string uid,string pass)
    {
      ....}然后你在按钮事件里调用此方法就OK了,另外一般开发界面层、业务层、数据底层如果你的方法在业务层,你在界面层注意引用业务层
      

  9.   

    呵呵~谢谢各位了~~我吧Login方法的返回值改成int就写好了~
    public int  Login(EmployeeInfoTable eif)
            {
                DbOperate dbo = new DbOperate();
                SqlCommand com = new SqlCommand();
                com.CommandText = string.Format("select *from EmployeeInfoTable where EIPwd='{0}' and EIID={1}", eif.EIPwd, eif.EIID);            dbo.TableName = EmployeeInfoTableDataOperate.TABLENAME;
                DataSet ds = dbo.Select(com);
                int count=0;
                count = ds.Tables[0].Rows.Count;
                return count;
            }再次感谢~~~~