做一个登录界面~
登录表里有用户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正确呢?
请前辈们指点一下。
登录表里有用户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正确呢?
请前辈们指点一下。
解决方案 »
- Type.GetType异常
- 如何强制结束进程,操作系统是否回收使用的内存
- C# 怎样运用多线程同步来提高运行速度
- winform中app.config配置问题
- 请问如何利用反射在c#中动态访问成员
- 如何把NumbericUpDown 放入DataGridView中
- 我的第一个C#软件出炉了!开放C#源码!
- 有关WinForm界面的问题
- 我是初学c#的新手,问大家一个问题、、、
- 请问商务快车(http://www.suminfo.com/suminfo/default.asp),它的搜索原理是什么?
- 为什么会出现对象不能从 DBNull 转换为其他类型。
- 为什么Winform中label控件的值不能随着程序的运行改变????
{
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;
}
另外不建议用select *, 用count(*) 好一些.
reader = cmd.ExecuteReader();
if(reader.read())
{ int count = reader.GetInt32(0);
if(count < 1) //密码错误 }
{
//在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是否正确。
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();
要分开的话,干脆你的数据层的那个方法返回一个bool变量嘛
按照楼主的思路,修改下楼主的设计:
原方法修改下名字,功能是取得用户信息,这样这个方法在复用的时候就不会有歧义了。
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;
}
在表示层调用后面方法进行判断是否可登录设计中尽量让你的单个方法的功能单一,不要很多功能都搞到一个方法里以上愚见,欢迎拍砖
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()我没弄懂~
{
....}然后你在按钮事件里调用此方法就OK了,另外一般开发界面层、业务层、数据底层如果你的方法在业务层,你在界面层注意引用业务层
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;
}再次感谢~~~~