如何将ASP.net网站中数据库操作的分离类和pages.aspx链接起来



登录页面:当用户登录时,我应该检查密码和用户通过检查数据库中的表记录,name是否为true。因为我使用oop概念,所以我为DB操作,但我面临一个大问题在数据库类中找不到Login.aspx。带有的Registration.aspx我想插入新用户的数据,但我不能查看文本框,将其中的字符串添加到数据库任何帮助或任何将这些类链接在一起的方式。

这是我的数据库类代码

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO; using

System.Data.SqlClient;使用System.Configuration;使用System.Data.Sql;使用System.Data;使用System.Web.UI.WebControls;命名空间注册{};

//////数据库的摘要描述///////命名空间Login.aspx{};公共类数据库{SqlDataReader rdr=null;公共SqlCommand cmd_insert;public字符串USer=";public字符串Pass=";

SqlConnection conn = null;
Login log = new Login();



public void Read_record()
{
try
{
//string ID = Request.QueryString["id"];

conn = new SqlConnection("Data Source=SHIMOFCIS-PC\MYSQL;Initial Catalog=WebSite;Integrated

安全=SSPI");

SqlCommand cmd;
conn.Open();

cmd = new SqlCommand("select UserName,Password from Users ", conn);

rdr = cmd.ExecuteReader();
//using (var reader = cmd.ExecuteReader())
//{
if (rdr.Read()) // you don't need while loop
{
USer = rdr["UserName"].ToString();
Pass = rdr["Password"].ToString();
if (USer == log.UserName && Pass == log.Password)
{
rdr.Close();
conn.Close();
}


}

//}

}
finally
{
// close the reader
if (rdr != null)
{

rdr.Close();
}
// 5. Close the connection
if (conn != null)
{
conn.Close();
}
}
}
public void Insert_rows()
{

conn = new SqlConnection("Data Source=SHIMOFCIS-PC\MYSQL;Initial Catalog=WebSite;Integrated

安全=SSPI");

conn.Open();
cmd_insert = new SqlCommand("INSERT INTO Users (UserName,Password,FullName,Address,Mobile,Email) VALUES (@value1 ,

@value2,@value3,@value4,@value5,@value6,@value7)",conn);

} }

和这个alogin.aspx代码

`使用系统;使用System.Collections.Generic;使用System.Linq;使用System.Web;使用System.Web.UI;使用System.Web.UI.WebControls;

公共分部类登录名:System.Web.UI.Page{protected void Page_Load(对象发送方,EventArgs e){

}
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
DataBase db = new DataBase();
db.Read_record();
if (db.USer == Login1.UserName && db.Pass == Login1.Password)
{
Response.Redirect("~/Home.aspx?UserName=" + Login1.UserName);
}

} }`

在regestration.aspx中,我无法使用create用户控件,因为我必须填写特定的字段,所以我不能依赖它解决了没有像我在登录时那样看到每一个的问题,尽管工作不正常

我强烈建议使用MVP模式来分离代码,而不是使用数据库类。数据库类将无法直接执行此操作。您可以使用类似Nucleo MVP或WebFormsMvp的框架。有关MVP模式的更多信息,请查看Dino Esposito的文章。

最新更新