Visual Studio双层数据库(DAL)

时间:2017-01-11 17:34:51

标签: c# asp.net sql-server visual-studio-2013

的Login.aspx

protected void btnLogin_Click(object sender, EventArgs e)
    {
        String tbNRIC = txtLogUsername.Text.ToString();
        String tbPassword = txtLogPassword.Text.ToString();

        PatientDAO fmTd = new PatientDAO();
        fmTd.getPatientByNricPassword(tbNRIC, tbPassword);

        if(?? > 0)
        {
            Response.Redirect("Home.aspx");
        }
        else
        {

        }
    }

如何检查用户名和密码是否匹配后如何响应?

PatientDAO.cs

public int getPatientByNricPassword(String tbNRIC, String tbPassword)
        {
            string DBConnect = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;

            SqlDataAdapter da;
            DataSet ds = new DataSet();
            Patient mypatient = new Patient();

            StringBuilder sqlStr = new StringBuilder();

            int result = 0;

            sqlStr.AppendLine("Select * from Login where");
            sqlStr.AppendLine("tbNRIC = @paratbNRIC AND tbPassword = @paratbPassword");

            try
            {
                SqlConnection myConn = new SqlConnection(DBConnect);
                da = new SqlDataAdapter(sqlStr.ToString(), myConn);

                da.SelectCommand.Parameters.AddWithValue("@paratbNRIC", tbNRIC);
                da.SelectCommand.Parameters.AddWithValue("@paratbPassword", tbPassword);
                DataTable dt = new DataTable();
                da.Fill(dt);
            }
            catch (SqlException ex)
            {
                logManager log = new logManager();
                log.addLog("PatientDAO.getPatientByNricPassword", sqlStr.ToString(), ex);
                mypatient = null;
            }
            return result;
        }

如何将结果传递给Login.aspx?

Patient.cs

public class Patient
    {
        public string tbNRIC { get; set; }
        public string tbPassword { get; set; }
    }

如何从PatientDAO查看用户名和密码是否匹配以及Login.aspx Response.Redirect。

1 个答案:

答案 0 :(得分:0)

我可以看到的代码存在一些问题。

首先在方法getPatientByNricPassword中,您似乎正在返回result变量,但尚未分配。在这种情况下,你可能想要返回记录数。

其次,您尚未使用凭据详细信息初始化PatientDAO fmTd = new PatientDAO();。 适当地初始化值 -

PatientDAO fmTd = new PatientDAO();
fmTd.tbNRIC = tbNRIC; // which is txtLogUsername.Text
fmTd.tbPassword = tbPassword ; //which is txtLogPassword.Text

请注意,您不需要放置ToString(),因为text属性已经是字符串。

第三,你应该有变量例如recordCount来存储dal方法的结果,你可以进一步检查if这样的条件 -

var recordCount = fmTd.getPatientByNricPassword(tbNRIC, tbPassword);
if(recordCount  > 0)
  {
     Response.Redirect("Home.aspx");
  }