简体   繁体   English

登录不起作用

[英]Login doesn't work

so, I'm trying to make a login form but I'm facing an error.. It doesn't continue, it shows only no data (the error message I've put if the username or password is incorrect, even tho I;ve set them correct) 所以,我正在尝试制作一个登录表单,但是我遇到了一个错误。它不会继续,它只显示任何数据(如果用户名或密码不正确,我会输入错误消息,甚至;已正确设置)

this is my code 这是我的代码

SqlConnection conn = new SqlConnection("Data Source=BRZAP\\MSSQLSERVERBTK; Initial Catalog=BrzAP; Integrated Security=True;");
try
{
    conn.Close();
    conn.Open();
    SqlCommand cmd = new SqlCommand(@"SELECT id,d_username,d_password FROM t_Login 
                            WHERE d_username=@usr AND 
                            d_password=@pw", conn);
    cmd.Parameters.AddWithValue("@usr", txtUID.ToString());
    cmd.Parameters.AddWithValue("@pw", txtP.ToString());
    SqlDataReader reader = cmd.ExecuteReader();
    if (reader.Read())
    {
       MessageBox.Show(reader["d_username"].ToString());
    }
    else
    {
        MessageBox.Show("no data");
    } 
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message.ToString());
    conn.Close();
}
finally
{
    conn.Close();
}

bellow is a pic of the data I have in sql. 下面是我在sql中拥有的数据的图片。 pic with data 图片与数据

Use txtUID.Text and txtP.Text instead of txtUID.ToString() and txtP.ToString() : 使用txtUID.TexttxtP.Text代替txtUID.ToString()txtP.ToString()

cmd.Parameters.AddWithValue("@usr", txtUID.Text);
cmd.Parameters.AddWithValue("@pw", txtP.Text);

The Text property gives you the actual value in the text box. Text属性为您提供文本框中的实际值。 But when you call .ToString() on a TextBox control, you will get something like: 但是,当您在TextBox控件上调用.ToString()时,将得到类似以下内容的信息:

System.Windows.Controls.Textbox System.Windows.Controls.Textbox

This means that the parameter will always be incorrect. 这意味着该参数将始终不正确。

See the documentation on MSDN 请参阅MSDN上的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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