簡體   English   中英

如何修復此三層登錄表單?

[英]How to fix this 3-tier Login form?

您好,我只是在研究3層應用程序。 我現在有這個簡單的登錄表單的問題。 我總是想出這個錯誤:

Entities.Entities是一種類型,但不像變量那樣使用。

始終顯示:用戶ID不正確。

public int SearchEntry(UsersEntity UserEntity)
{
  SqlConnection con = new SqlConnection(); 
  con.ConnectionString = connStr;

  try
  {
    SqlCommand cmd = new SqlCommand("sp_SelectUser", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@User", SqlDbType.VarChar, 50).Value = UserEntity.UserName;
    cmd.Parameters.Add("@Pass", SqlDbType.VarChar, 50).Value = UserEntity.Password ;

    SqlParameter p1 = new SqlParameter("ret", SqlDbType.Int);
    p1.Direction = ParameterDirection.ReturnValue;
    cmd.Parameters.Add(p1); 

    con.Open();
    cmd.ExecuteNonQuery();
    Convert.ToInt32(cmd.Parameters["ret"].Value);

    int s = cmd.ExecuteNonQuery();
    cmd.Dispose();

    return s;
  }
  catch (SqlException)
  {
     throw;
  }
  finally
  {
    con.Close();
    con.Dispose();                  
  }

------------------------------------------------------------------------------------------
 public class BLL
 {
   public int SearchEntry(UsersEntity UserEnity)
   {
     DAL pDAL = new DAL();

     int i = pDAL.SearchEntry(UserEnity);
     return i;               
   }    
}

-------------------------------------------------------------------------------------------
PRESENTATION LAYER:

private void btnLogin_Click(object sender, EventArgs e)
{
  BLL aBl = new BLL();

  UsersEntity uEt = new UsersEntity();             
  uEt.UserName = txtUser.Text;
  uEt.Password = txtPass.Text;

  int r = ****(int)aBl.SearchEntry(uEt.UserName,uEt.Password);****
  if (r == -1)
  {
    MessageBox.Show("Incorrect User Id");
  }
  else if (r == -2)
  {
    MessageBox.Show("Incorrect Password");
  }
  else if (r == 1)
  {
    MessageBox.Show("Welcome");    
  }
  else
  {
    MessageBox.Show("Incorrect User Id / Password ");
  }                   
}

存儲過程代碼

ALTER PROCEDURE [dbo].[sp_SelectUser] 
-- Add the parameters for the stored procedure here
        @User VARCHAR(50),
        @Pass VARCHAR(50)
AS
        DECLARE @Ap AS VARCHAR(50)

        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        -- Insert statements for procedure here
        SELECT @Ap = @Pass
        FROM   InventoryManager.dbo.Users
        WHERE  UserName = @User

        IF @Ap    IS NULL
        RETURN -1
        ELSE
        IF @Ap=@Pass
        RETURN 1
        ELSE
        RETURN -2

您收到的錯誤是因為SearchEntry()需要一個UsersEntity作為參數,但是您使用兩個字符串來調用它。

嘗試更換:

int r = ****(int)aBl.SearchEntry(uEt.UserName,uEt.Password);****

與:

int r = (int)aBl.SearchEntry(uEt);

話雖如此,您的代碼中可能會有更多錯誤。

這段代碼看起來很可疑。

           con.Open();
           cmd.ExecuteNonQuery();
           Convert.ToInt32(cmd.Parameters["ret"].Value);

          int s = cmd.ExecuteNonQuery();
            cmd.Dispose();
           return s;

您正在兩次調用ExecuteNonQuery 並將cmd.Parameters["ret"].Value轉換為int,但不將結果分配給任何東西

您可以發布存儲過程代碼以便我們了解其意圖嗎?

編輯

我已將我的答案標記為CW,因為我沒有時間或意願去解決所有問題,但除了上述幾點和關於“ Entities.Entities”的錯誤之外,

該存儲過程也注定會失敗

    SELECT @Ap = @Pass
    FROM   InventoryManager.dbo.Users
    WHERE  UserName = @User

如果傳入了正確的UserName,則會將變量@Ap設置為您最初傳入的變量@Pass的值(根本不檢查表中存儲的值)並返回1。我懷疑這是故意的嗎?

我可能應該提到,以明文形式存儲密碼絕對不是最佳實踐。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM