Binding to data in an MS Access DB - WPF, C#, XAML
-
Wednesday, September 30, 2009 6:04 PMI am new to WPF - I have seen snippets of code here and there on databinding, datacontext, etc. as it relates to connecting to the data in an MS Access db for WPF (Visual Studio 2008), but I haven't had much success in actually connecting to the data. My application will need to both read from and write to the db from the UI. Can anyone give me a good code example & explanation on how to connect to the Access DB? Or, is there a really good tutorial floating around somewhere that might help? I greatly appreciate any help you can provide!
All Replies
-
Wednesday, September 30, 2009 9:39 PM
The connection to a persistency source is the same as before WPF. You can use VS connection wizard or build the connection string your self: http://www.connectionstrings.com/
Bigsby, Lisboa, Portugal
O que for, quando for, é que será o que é...
Wenn ist das Nunstruck git und Slotermeyer? Ja! ... Beiherhund das Oder die Flipperwaldt gersput!
http://bigsby.eu -
Wednesday, September 30, 2009 10:45 PM
Try this:
1, use MS Access to build a People table as below (file name is Database1.accdb)
People
ID Name Age
1 Tom 10
2 Jen 20
3 Peter 30
2, Create a new wpf project
a, right click on project, Add, New Item, Data, DataSet, and click Add on Add New Item dialog to create DataSet1.xsd
b, click Server Explorer on DataSet1.xsd
c, right click on Data Connections, Add Connection..., Choose Microsoft Access Database File, and click Continue
d, click Browse... button on Add Connection dialog to add MS Access Database1.accdb to Database file name: and leave password empty (optional, click Test Connection button to ensure connection is fine). Then click OK button
e, on Database1.accdb treeview, drill in to the People table and drag it onto the designer surface and DataSet1.Designer.cs is created and you will see PeopleTableAdapter and there is GetData() in it.
f, call PeopleTableAdapter.GetData() in Window.xaml.cs Window constructor as below
public Window()
{
InitializeComponent();
DataContext = (new PeopleTableAdapter()).GetData();
}
g, show data on ListBox on Window.xaml
<ListBox Name="listbox" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock Text="{Binding Path=Name}"/>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
William- Marked As Answer by Bruce.Zhou Friday, October 02, 2009 8:18 AM
-
Monday, July 11, 2011 4:48 PM
Thanks a lot, it helped
But how to navigate among the data, means next, prev etc.
-
Tuesday, February 07, 2012 3:48 AM
William
The code you provided was very helpful...but what if I want to display multiple columns from my access database including display a column containing a image(OLE object) ?
-
Sunday, June 17, 2012 11:31 AM
You can even use LINQ to SQL to fill dataGrid, for example:
var uta = new ExapleBDDataSetTableAdapters.UsersTableAdapter();
var UsersEnum = uta.GetData();var LinqRes = from UserRec in UsersEnum
where (UserRec.DostRaion.ToString()).Contains("101")
select UserRec;
dataGrid1.ItemsSource = LinqRes;And then, in XAML, use binding for each field you need to display
<DataGrid...>
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding Path=ID}" FontWeight="Bold" Foreground="#FF0000A9" />
<DataGridTextColumn Header="UserName" Binding="{Binding Path=Login}"/>
<DataGridTextColumn Header="FM" Binding="{Binding Path=FM}"/>
<DataGridTextColumn Header="PAR" Binding="{Binding Path=PAS}"/>
<DataGridTextColumn Header="Rol" Binding="{Binding Path=Rol}"/>
<DataGridTextColumn Header="DostRaion" Binding="{Binding Path=DostRaion}"/>
</DataGrid.Columns></<DataGrid>
-
Sunday, September 16, 2012 2:10 AM
One other tip. To assign different data contexts to different controls, you can replace this:
InitializeComponent();
DataContext = (new PeopleTableAdapter()).GetData();with this:
InitializeComponent();
listbox1.DataContext = (new PeopleTableAdapter()).GetData();
listbox2.DataContext = (new PeopleTableAdapter()).GetData();
listbox3.DataContext = (new AnotherTableAdapter()).GetData();and then set the binding paths in the XAML. By the way, if the Access name includes spaces (for example, "Product Code"), enclose it in single quotes, like this:
<TextBlock Text="{Binding Path='Product Code'}"/>Regards,
Anonymous9748

