简体   繁体   中英

Retrieving data from database in WPF Desktop application

I am new to c# and I had just learned how to input data into datatable of the database.mdf within my WPF Application.

This is done via the following code

SqlConnection sqlCon = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;");
try
{
    if (sqlCon.State == ConnectionState.Closed)
        sqlCon.Open();
    String query = "SELECT COUNT(1) FROM tbl WHERE Username=@Username and Password=@Password";
    SqlCommand sqlCmd = new SqlCommand(query, sqlCon);
    sqlCmd.CommandType = CommandType.Text;
    sqlCmd.Parameters.AddWithValue("@Username", txtbxUsername.Text);
    sqlCmd.Parameters.AddWithValue("@Password", pswbxPassword.Password);
    int count = Convert.ToInt32(sqlCmd.ExecuteScalar());
    if (count == 1)
    {
        MessageBox.Show("Login successfully!");

        DetailPage newpage = new DetailPage();
        newpage.Show();
        this.Close();
    }
    else
    {
        MessageBox.Show("Username or password is incorrect");
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
finally
{
    sqlCon.Close();
}

Currently after logging in via the login page, there's a detail page where I want to display details like firstname, lastname, age which are saved in my database previously. May I ask what are the SqlCommands I could use to retrieve all these parameters data from the database?

In your place, I would add a User class in my application

public class user
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MailAddress{ get; set; }
}


//You can keep your Logged user in a static class


public static class PublicParameters
{ 
  public static User CurrentUser;

  //Define only one connection string in your application.
  public static string ConnectionString= @"Data Source=   (LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated 
 Security=True;"
}




    void Login()
    {
        using (SqlConnection sqlConn = new SqlConnection(ConnectionString))
        {
            using (SqlCommand sqlComm = new SqlCommand("SELECT COUNT(1) FROM tbl WHERE Username=@Username AND Password=@Password", sqlConn))
            {
                if (sqlComm.Connection.State == System.Data.ConnectionState.Closed)
                    sqlComm.Connection.Open();
                SqlDataReader sqlRd = sqlComm.ExecuteReader();
                sqlComm.Parameters.AddWithValue("@Username", txtbxUsername.Text);
                sqlComm.Parameters.AddWithValue("@Password", pswbxPassword.Password);

                //Your username column must be unique
                while (sqlRd.Read())
                {
                    PublicParameters.CurrentUser = new Controllers.User();
                    PublicParameters.CurrentUser.FirstName = sqlRd["FirstName"].ToString();
                    PublicParameters.CurrentUser.LastName = sqlRd["LastName"].ToString();
                    PublicParameters.CurrentUser.MailAddress = sqlRd["MailAddress"].ToString();
                    //And other properties to assign
                }

                if(PublicParameters.CurrentUser != null)
                {
                    MessageBox.Show("Login successfully!");
                    //Yo have your logged user 
                }
                else
                {
                    MessageBox.Show("Username or password is incorrect");
                }
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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