简体   繁体   English

如何将单独的类用于ASP.net网站中的数据库操作与pages.aspx链接

[英]How to link separated class for database operations in website in ASP.net with the pages.aspx

Login Page: when the user logs in I should check if password and user name is true or not through checking the table records in database. 登录页面:用户登录时,应通过检查数据库中的表记录来检查密码和用户名是否正确。 Because I'm working using oop concept I created a separate class for DB operations but I face a big problem that the text boxes in the Login.aspx can't be seen in database class. 因为我使用的是oop概念,所以为数据库操作创建了一个单独的类,但是我面临一个大问题,即在数据库类中看不到Login.aspx中的文本框。 The with Registration.aspx I want to insert data of the new user but I can't see the textboxes to take the strings inside them to add in the database any help or any way to link those classes together. Registration.aspx我想插入新用户的数据,但是看不到文本框将字符串带入其中,从而无法在数据库中添加任何帮助或以任何方式将这些类链接在一起。

here's my data Base class code 这是我的数据库基类代码

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

System.Data.SqlClient; System.Data.SqlClient; using System.Configuration; 使用System.Configuration; using System.Data.Sql; 使用System.Data.Sql; using System.Data; 使用System.Data; using System.Web.UI.WebControls; 使用System.Web.UI.WebControls; namespace Registration { }; 名称空间注册{};

/// /// Summary description for DataBase /// /// //namespace Login.aspx { }; /// ////数据库的摘要描述/// /// //命名空间Login.aspx {}; public class DataBase { SqlDataReader rdr = null; 公共类DataBase {SqlDataReader rdr = null; public SqlCommand cmd_insert; 公共SqlCommand cmd_insert; public String USer=""; public String USer =“”; public String Pass=""; public String 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 

Security=SSPI"); 安全性= 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 

Security=SSPI"); 安全性= 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); @ value2,@ value3,@ value4,@ value5,@ value6,@ value7)“,conn);

 } } 

and this alogin.aspx code 和这个alogin.aspx代码

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

public partial class Login : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { 公共局部类登录:System.Web.UI.Page {受保护的无效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); } } }` 

and in regestration.aspx i couldn't use create user control beacuse i have to do specific fields to fill in so i couldn't depend on it to solve the problem of not seein each like i do in login and it although not working quiet well 在regestration.aspx中,我无法使用创建用户控件,因为我必须填写特定字段,所以我不能依靠它来解决无法像我在登录时那样看到彼此的问题,尽管它不能正常工作好

I would highly suggest using an MVP pattern to separate the code, rather than a database class. 我强烈建议使用MVP模式来分隔代码,而不是数据库类。 The database class won't be able to do it directly. 数据库类将无法直接执行此操作。 You can use a framework like Nucleo MVP or WebFormsMvp . 您可以使用Nucleo MVPWebFormsMvp之类的框架。 For more on the MVP pattern, check out Dino Esposito's article . 有关MVP模式的更多信息,请查看Dino Esposito的文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM