简体   繁体   中英

Returning value from Stored Procedure and using it in C#

I have created a function "Recover(string UserEmail)" that takes in the user's email for when he/she forgets their login information. This function runs a stored procedure called "Recover_PassWord", which I would like it to return the Username and Password so I can send an email to user. I am not too familiar with the syntax for creating a simple stored procedure that returns this information. I am also not too familiar with storing the returned value in C# so i can make use of it.

Recover_PassWord:

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

Recover(String 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;
    }

Anyone have any recommendations and advice on how i can achieve this?

You have to use: call like this,

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

if you are using parameter:

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

to use:

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

try the code below. If it works, please mark the answer as positive. Thank you!

    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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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