[英]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. 我创建了一个函数“ Recover(string UserEmail)”,该函数可以在用户忘记登录信息时接收用户的电子邮件。 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.
此函数运行一个名为“ Recover_PassWord”的存储过程,我希望它返回用户名和密码,以便向用户发送电子邮件。 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.
我也不太熟悉在C#中存储返回的值,因此我可以利用它。
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): 恢复(字符串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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.