2017-07-27 54 views
0

我創建了一個函數「Recover(string UserEmail)」,該函數在用戶忘記其登錄信息時接收用戶的電子郵件。該函數運行名爲「Recover_PassWord」的存儲過程,我希望它返回用戶名和密碼,以便我可以發送電子郵件給用戶。我不太熟悉用於創建返回此信息的簡單存儲過程的語法。我也不太熟悉在C#中存儲返回的值,所以我可以使用它。

Recover_PassWord:
從存儲過程中返回值並在C#中使用它

CREATE DEFINER=`root`@`localhost` PROCEDURE `Recover_Password`(IN Email CHAR(40)) 
BEGIN 
    SELECT UserName,PassWord FROM userdata 
    WHERE Email=ContactEmail; 
END 

恢復(字符串USEREMAIL):

public ActionResult Recover(string UserEmail) 
    { 
     Login model = new Login(); 
     MySqlConnection connect = DbConnection(); 
     MySqlCommand cmd = new MySqlCommand("Recover_PassWord",connect); 
     cmd.CommandType = CommandType.StoredProcedure; 
     cmd.Parameters.AddWithValue("@Email", UserEmail); 
     MySqlDataReader rd = cmd.ExecuteReader(); 
     while (rd.Read()) 
     { 
      if (UserEmail == rd["ContactEmail"].ToString()) 
      { 
       MailMessage msg = new MailMessage(); 
       msg.To.Add(UserEmail); 
       msg.Subject = "Recovered Login Information"; 
       msg.Body = "Hello," + Environment.NewLine + "Your Login Information: "; //this is where i want to be able to use the returned value 
      } 
     } 
     return null; 
    } 

任何人有我如何才能做到這一點的任何建議和意見?

+0

你嘗試使用實體框架嗎? –

+0

我沒有。最近有沒有關於如何使用它的文章? – julianc

+0

你應該嘗試一下,真的很容易使用它調用SP http://www.entityframeworktutorial.net/EntityFramework5/setup-entityframework-environment.aspx –

回答

0

嘗試下面的代碼。如果有效,請將答案標爲正面。謝謝!

public ActionResult Recover(string UserEmail) 
    { 
     Login model = new Login(); 
     MySqlConnection connect = DbConnection(); 
     MySqlCommand cmd = new MySqlCommand("Recover_PassWord",connect); 
     cmd.CommandType = CommandType.StoredProcedure; 
     cmd.Parameters.AddWithValue("@Email", UserEmail); 
     MySqlDataReader rd = cmd.ExecuteReader(); 
     while (rd.Read()) 
     { 
      if (UserEmail == rd["ContactEmail"].ToString()) 
      { 
       try 
       { 
        SmtpClient mySmtpClient = new SmtpClient("my.smtp.exampleserver.net"); 

        // set smtp-client with basicAuthentication 
        mySmtpClient.UseDefaultCredentials = false; 
        System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("username", "password"); 
        mySmtpClient.Credentials = basicAuthenticationInfo;      

        MailMessage msg = new MailMessage(); 
        msg.To.Add(UserEmail); 
        msg.Subject = "Recovered Login Information"; 
        msg.Body = "Hello," + Environment.NewLine + "Your Login Information: "; //this is where i want to be able to use the returned value 

        msg.Body += Environment.NewLine + "username:" + (string)rd["username"]; 
        msg.Body += Environment.NewLine + "password:" + (string)rd["password"];   

        mySmtpClient.Send(msg); 

       } 
       catch (SmtpException ex) 
       { 
        throw new ApplicationException 
        ("SmtpException has occured: " + ex.Message); 
       } 
       catch (Exception ex) 
       { 
        throw ex; 
       } 
      } 
     } 
     return null; 
    } 
+0

不應該「mySmtpClient.Send(myMail);」是「mySmtpClient.Send(msg);」? – julianc

+0

哇,對不起,我在這裏得到了我的代碼,沒錯,只是改變! 更正上面的代碼! –

+0

除了我得到一個smtpexception幾乎作品。 「發送郵件失敗」沒有InnerException。你知道可能是什麼問題嗎? – julianc

0

你必須使用:這樣調用,

MySqlCommand cmd = new MySqlCommand("call Recover_PassWord",connect); 

如果您使用的參數:

create procedure Recover_Password(Email char(40)) 
BEGIN 
SELECT UserName,PassWord FROM userdata 
    WHERE ContactEmail =Email; 
end 

使用:

String emailInput = "thisIsMyEmailInput"; 
    MySqlCommand cmd = new MySqlCommand("call Recover_Password('" + emailInput + "')",connect); 
+0

如果是存儲過程而不是函數。函數和存儲過程是不同的。 –