用户'root'登录失败

时间:2016-05-22 15:29:16

标签: c# mysql asp.net

我试图创建一个忘记密码页面。用户将其电子邮件地址放在提供的框中。提交电子邮件时,无论是否在数据库中,都会对其进行过滤。我在con.Open()上有错误;当我尝试按下提交按钮时,总是会发生错误“用户root登录失败”。感谢您的帮助

    string username = string.Empty;
        string password = string.Empty;
        string constr = ConfigurationManager.ConnectionStrings["DBCon"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT username, password FROM user_tbl WHERE email = @Email"))
            {
                cmd.Parameters.AddWithValue("@Email", txbx_email.Text.Trim());
                cmd.Connection = con;
                con.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    if (sdr.Read())
                    {
                        username = sdr["Username"].ToString();
                        password = sdr["Password"].ToString();
                    }
                }
                con.Close();
            }
        }
        if (!string.IsNullOrEmpty(password))
        {
            MailMessage mm = new MailMessage("sender@gmail.com", txbx_email.Text.Trim());
            mm.Subject = "Password Recovery";
            mm.Body = string.Format("Hi {0},<br /><br />Your password is {1}.<br /><br />Thank You.", username, password);
            mm.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            NetworkCredential NetworkCred = new NetworkCredential();
            NetworkCred.UserName = "***@gmail.com";
            NetworkCred.Password = "***";
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            smtp.Send(mm);
            lbl_message.ForeColor = Color.Green;
            lbl_message.Text = "Password has been sent to your email address.";
        }
        else
        {
            lbl_message.ForeColor = Color.Red;
            lbl_message.Text = "This email address does not match our records.";
        }

1 个答案:

答案 0 :(得分:0)

该错误表示您尝试使用通过用户root连接到数据库,该用户root未作为登录添加到您的服务器。

更改允许登录服务器的用户或为您的SQL Server提供UIScreen用户登录权限

可以通过此选项将用户添加到服务器允许的登录

  

服务器&gt;安全&gt;登录