none
how to read data from MS Access database in MVVM RRS feed

  • Question

  • I am following this MVVM example:

    http://www.eggheadcafe.com/tutorials/aspnet/8a2ea78b-f1e3-45b4-93ef-32b2d802ae17/wpf-datagrid-custom-paging-and-sorting.aspx

    every things are good except the following code which represent the data:

    /// <summary>

        /// A list of products. This should be replaced by a database.

        /// </summary>

        private static ObservableCollection<Product> products = new ObservableCollection<Product>

        {

            new Product(1, "Book"),

            new Product(2, "Desktop Computer"),

            new Product(3, "Notebook"),

            new Product(4, "Netbook"),

            new Product(5, "Business Software"),

            new Product(6, "Antivirus Software"),

            new Product(7, "Game Console"),

            new Product(8, "Handheld Game Console"),

            new Product(9, "Mobile Phone"),

            new Product(10, "Multimedia Software"),

            new Product(11, "PC Game")           

        };

     

    how can I replace this code by another one which retrieve data from access database?

    Thursday, June 2, 2011 10:22 AM

Answers

  • Hello,

    From your description, it looks that your provided collection product’s data was returned by database. So based on my understanding, you should have a Product table in your database. I suggest you can check InsertOnSubmit method. Here’s the code snippet about how to insert data from the database.

    public static void InsertOrUpdateCustomer(string customerId, <you can pass more parameter about special table you want to insert>)
      {
    
        NorthWindDataClassesDataContext dc = new 
        NorthWindDataClassesDataContext();
    
        var v = (from c in dc.GetTable<Customer>()
                    where c.CustomerID == customerId
                    select c).SingleOrDefault();
    
        if(v == null)
        {
          try
          {
            // create new customer record since customer ID
            // does not exist
            Table<Customer> customers = Accessor.GetCustomerTable();
            Customer cust = new Customer();
    
            cust.CustomerID = customerId;
            cust.CompanyName = companyName;
    
            customers.InsertOnSubmit(cust);
            customers.Context.SubmitChanges();
          }
          catch (Exception ex)
          {
            throw ex;
          } 
        }
        else
        {
          try
          {
            matchedCustomer.CompanyName = companyName;
    
            dc.SubmitChanges();
          }
          catch (Exception ex)
          {
            throw ex;
          }
        }
    
    
    

    Please check this article for more information about LINQ to SQL via C#. Hope this helps. If I misunderstood, please feel free to elaborate your question kindly.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Larcolais Gong Monday, June 13, 2011 10:18 AM
    • Unmarked as answer by Larcolais Gong Wednesday, June 15, 2011 2:58 AM
    • Marked as answer by Adel K Wednesday, June 22, 2011 5:25 PM
    Monday, June 6, 2011 7:25 AM