【问题标题】:DataContext access to accdb对 accdb 的 DataContext 访问
【发布时间】:2015-01-05 00:00:34
【问题描述】:

我能够使用 WPF 连接到 accdb (Access 2007)。我使用了https://social.msdn.microsoft.com/Forums/vstudio/en-US/92af8085-e9e7-411b-9ab0-52ae0e62942c/binding-to-data-in-an-ms-access-db-wpf-c-xaml?forum=wpf 的信息。

给定:

DataContext = (new _DbTest.DataSet1TableAdapters.PeopleTableAdapter()).GetData();

哪些命令可以让我读取和写入这个 accdb 文件?

【问题讨论】:

    标签: wpf ms-access visual-studio-2013


    【解决方案1】:

    如果 new _DbTest.DataSet1TableAdapters.PeopleTableAdapter() 语句生成 TableAdapter,您可以按照 MSDN 上的文档了解如何获取/插入/更新数据。

    这是 MSDN 文档的链接:

    TableAdapter Overview

    以下是链接中有关如何从 TableAdapter 中提取数据的一些示例代码。

    NorthwindDataSet northwindDataSet = new NorthwindDataSet();
    
    NorthwindDataSetTableAdapters.CustomersTableAdapter customersTableAdapter = 
        new NorthwindDataSetTableAdapters.CustomersTableAdapter();
    
    customersTableAdapter.Fill(northwindDataSet.Customers);
    

    您可以继续使用更新/插入命令在数据库中更新或插入行。插入命令的文档可以在这里找到:

    How to Update Data using a TableAdapter

    关于插入命令的文档可以在这里找到:

    How to Insert Data using a TableAdapter

    【讨论】:

      【解决方案2】:

      使用 Access 2007 表 (CustomersTable) 和两个字段(CustomerID、CompanyName),以下 OleDb C# 代码可查找、修改、添加和删除:

      using System.Data;
      using System.Data.Common;
      using System.Data.OleDb;
      
      string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\..\\OleDbExample.accdb";
      using (OleDbConnection connection = new OleDbConnection(connectionString))
      {
      //The connection is automatically closed when the code exits a "using" block.
        using (OleDbCommand command = connection.CreateCommand())
        {
          try
          {
            OleDbDataReader reader;
            connection.Open();
      
            //Read the database
            command.CommandText = "SELECT ID, CustomerID, CompanyName FROM CustomersTable";
            reader = command.ExecuteReader();
            while (reader.Read())
            {
              string st = reader.GetInt32(0) + "  " + reader.GetInt32(1) + "  " + reader.GetString(2);
              Console.WriteLine(st);
            }
            reader.Close();
      
            //Add to the database
            command.CommandText = "INSERT INTO CustomersTable(CustomerID, CompanyName) VALUES(?, ?)";
            command.Parameters.Clear();
            command.Parameters.Add("@p1", OleDbType.VarChar).Value = 22;
            command.Parameters.Add("@p2", OleDbType.VarChar).Value = "xyz3";
            command.ExecuteNonQuery();
      
            //Find a specific value
            command.CommandText = "SELECT CustomerID, CompanyName FROM CustomersTable WHERE CompanyName = @p1";
            command.Parameters.Clear();
            command.Parameters.Add("@p1", OleDbType.VarChar).Value = "Zap";
            reader = command.ExecuteReader();
            reader.Close();
      
            //Find a similar value
            command.CommandText = "SELECT CustomerID, CompanyName FROM CustomersTable WHERE CompanyName LIKE @p1";
            command.Parameters.Clear();
            command.Parameters.Add("@p1", OleDbType.VarChar).Value = "%Za%";
            reader = command.ExecuteReader();
            reader.Close();
      
            //Modify an entry
            command.CommandText = "UPDATE CustomersTable SET CompanyName = ? WHERE CompanyName = ?";
            command.Parameters.Clear();
            command.Parameters.Add("@p1", OleDbType.VarChar).Value = "xyz7"; // SET ... final value
            command.Parameters.Add("@p2", OleDbType.VarChar).Value = "xyz3"; // WHERE ... initial value
            command.ExecuteNonQuery();
      
            //Delete row with ID = 51
            command.CommandText = "DELETE FROM CustomersTable WHERE ID = @p1";
            command.Parameters.Clear();
            command.Parameters.Add("@p1", OleDbType.VarChar).Value = 51;
            command.ExecuteNonQuery();
      
            //Delete all rows
            command.CommandText = "DELETE * FROM CustomersTable";
            command.ExecuteNonQuery();
          }//try
          catch (Exception ex)
          {
            Console.WriteLine(ex.Message);
          }
          command.Dispose();
          connection.Dispose();
        }//using
      }//using
      

      【讨论】:

        猜你喜欢
        • 2010-11-10
        • 1970-01-01
        • 2011-05-18
        • 1970-01-01
        • 1970-01-01
        • 2011-03-13
        • 2021-12-21
        • 1970-01-01
        相关资源
        最近更新 更多