WPF登录身份验证表单SQL Server数据库

时间:2017-04-22 19:55:01

标签: c# wpf xaml sql-server-2012

我需要知道如何检查输入的密码,User_ID在我的SQL Server 2012中创建的表中是正确的。

请给我一个链接或代码,如果有人知道它从一开始就像连接字符串等......!

提前感谢所有程序员:P

2 个答案:

答案 0 :(得分:1)

实现身份验证与数据库编程不同。你应该首先关注核心问题。

这就是你真正需要的:

WPF: Implementing Custom Authentication And Authorization

可选,

Entity Framework Code First to an Existing Database

答案 1 :(得分:-1)

由于时间不够,我不会为我的应用程序遵循实体框架,但我仍然从这个链接得到答案。

https://msdn.microsoft.com/en-us/library/fksx3b4f.aspx

这是我的代码,检查输入的User_ID和密码是否正确....

    private String ConnectionString;
    private SqlConnection connection;
    private SqlCommand cmd = new SqlCommand();
    private SqlDataReader reader;

    public MainWindow()
    {
        InitializeComponent();

        ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\RFIDdatabase.mdf;Integrated Security=True";
        connection = new SqlConnection(ConnectionString);
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            Console.WriteLine(ID.Text + " and " + Pass.Password);

            cmd.CommandText = "Select * from Login "
                            + "where USER_ID = '" + ID.Text + "' "
                            + "and Password = '" + Pass.Password + "'";

            Console.WriteLine(cmd.CommandText);
            cmd.Connection = connection;
            connection.Open();
            reader = cmd.ExecuteReader();

            int i = 0;

            while (reader.Read())
            {
                i++;
            }


            if (i == 1)
            {
                Window1 win = new Window1();
                win.Show();
            }
            else {
                MessageBox.Show("INCORRECT PASSWORD OR USER ID", "Authentication Failed");
            }

        }
        catch (Exception ex)
        {
            Console.WriteLine("Catch Block = " + ex);
        }
        finally {

            connection.Close();
        }
    }