从存储过程中返回值并在C#中使用它

问题描述:

我创建了一个函数“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 –

尝试下面的代码。如果有效,请将答案标为正面。谢谢!

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

你必须使用:这样调用,

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

如果是存储过程而不是函数。函数和存储过程是不同的。 –