gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:35:15 25 4
gpt4 key购买 nike

我创建了一个函数“Recover(string UserEmail)”,当用户忘记登录信息时,该函数会接收用户的电子邮件。该函数运行一个名为“Recover_PassWord”的存储过程,我希望它返回用户名和密码,以便我可以向用户发送电子邮件。我不太熟悉创建返回此信息的简单存储过程的语法。我也不太熟悉在 C# 中存储返回值,因此我可以利用它。

恢复密码:

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

恢复(字符串用户电子邮件):

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

有人对我如何实现这一目标有任何建议和建议吗?

最佳答案

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

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

关于c# - 从存储过程返回值并在 C# 中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45362652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com