用户验证:
首先新建项目=“类库”――起名为TeEmial.DataAccess 删除本身自带的Class 手动创建"类" ――起名为UsersDataAccess.cs
在新建项目=“类库”――起名为 TeEmial.Rule 删除本身自带的Class 手动创建"类" ――起名为UserRules.cs
打开UsersDataAccess.cs class设置为public
添加 using System.Data;
using System.Data.SqlClient;
public class UsersDataAccess
{
SqlConnection Conn;
SqlDataAdapter UserDa;
public UsersDataAccess()
{
Conn = new SqlConnection("server=.;database=test;user id =sa; pwd ='123'");
}
/// <summary>
/// 检验用户
/// </summary>
/// <param name="name">用户名</param>
/// <param name="pwd">密码</param>
/// <returns></returns>
public DataSet UserLogion(string name, string pwd)
{
DataSet Userds =new DataSet();
string sql ="SELECT * FROM USERS WHERE NAME =@Name AND PWD =@Pwd ";
UserDa= new SqlDataAdapter();
UserDa.SelectCommand =new SqlCommand(sql,Conn);
UserDa.SelectCommand.Parameters.Add(new SqlParameter ("@Name",SqlDbType.NVarChar,50));
UserDa.SelectCommand.Parameters["@Name"].Value =name;
UserDa.SelectCommand.Parameters.Add(new SqlParameter ("@Pwd",SqlDbType.NVarChar,50));
UserDa.SelectCommand.Parameters["@Pwd"].Value=pwd;
UserDa.Fill(Userds);
return Userds;
}
生成一下
打开UserRules.cs class 设置为public 添加引用 项目――――把之前的项目添加进来 项目名称:(TeEmial.DataAccess)
在代码引用一下。
using System.Data;
using TeEmial.DataAccess;
namespace TdEmail.Rule
{
public class UserRules
{
TeEmial.DataAccess.UsersDataAccess Uda = new TeEmial.DataAccess.UsersDataAccess();
public bool CheckUser(string name, string pwd)
{
DataSet Uds = Uda.UserLogion(name, pwd);
if (Uds.Tables[0].Rows.Count > 0)
{
return true;
}
return false;
}
}
}
最后在DeFault.aspx.cs
Button 登陆按钮
TeEmail.Rule.UserRules Urule = new TdEmail.Bule.UserRules();
if (Urule.CheckUser(txtName.Text.Trim(), txtPwd.Text.Trim()))
{
lblMsg.Text = "ok";
}
else
{
lblMsg.Text = "no";
}