DataContext访问accdb

时间:2014-12-09 18:05:40

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

我可以使用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文件?

2 个答案:

答案 0 :(得分:0)

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

以下是MSDN文档的链接:

TableAdapter Overview

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

NorthwindDataSet northwindDataSet = new NorthwindDataSet();

NorthwindDataSetTableAdapters.CustomersTableAdapter customersTableAdapter = 
    new NorthwindDataSetTableAdapters.CustomersTableAdapter();

customersTableAdapter.Fill(northwindDataSet.Customers);

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

How to Update Data using a TableAdapter

可以在此处找到有关Insert命令的文档:

How to Insert Data using a TableAdapter

答案 1 :(得分:0)

使用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