2017-07-27 6 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

は、以下のコードを試してみてくださいあなたは、パラメータを使用している場合

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

、このように呼んでいます。それがうまくいったら、答えを肯定的なものとしてマークしてください。ありがとうございました!

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);" be "mySmtpClient.Send(msg);" – julianc

+0

うわー、申し訳ありません、私はここに自分のコードを持っています、そうです、ちょうど変更! 上記のコードを修正してください。 –

+0

ほとんど例外なく、私はsmtpexceptionを取得します。 InnerExceptionを伴わない「メール送信失敗」何が問題なのか知っていますか? – julianc

0

あなたは使用する必要があります:

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

これはストアドプロシージャであり、関数ではありません。関数とストアドプロシージャは異なります。 –

関連する問題