首页 > 代码库 > WPF以access为数据库,简单实现一个显示数据和更新数据的实例
WPF以access为数据库,简单实现一个显示数据和更新数据的实例
做一个小实例,如下图,
1、设计数据库access:
2、在vs2013中创建wpf,把上数据库保存为db.mdb拷贝到项目下,并打开App.config文件添加数据库连接字符串
<?xml version="1.0" encoding="utf-8" ?><configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" /> </startup> <connectionStrings> <add name="conn" connectionString ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db.mdb;Persist Security Info=False" /> </connectionStrings></configuration>
3、创建连接数据库的类
public class DbHelper { public static OleDbConnection getCon(){ string strcon = System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ToString(); return new OleDbConnection(strcon); } }
4、创建Model类
public class Person { public string username { get; set; } public string pwd { get; set; } public int age { get; set; } public DateTime createdate { get; set; } public DateTime updatedate { get; set; } public string classroom { get; set; } public int id { get; set; } }
5、在mainwindow.cs类中添加数据和绑定数据
private Random rnd = new Random(); private ObservableCollection<Person> list { get; set; } public MainWindow() { InitializeComponent(); add();//添加数据 BindData();//绑定数据 cr.ItemsSource = new List<string> { "一班", "二班", "三班" }; } public void BindData() { using (var conn = DbHelper.getCon()) { list = new ObservableCollection<Person>(conn.Query<Person>("select * from users").ToList<Person>()); } this.DataContext = list; } public void add() { using (var db = DbHelper.getCon()) { for (int i = 0; i < 10; i++) { db.Execute("insert into users(username,pwd,age,createDate,classroom) values(@username,@pwd,@age,@createDate,@classroom)", new { username = "张三", pwd = "123", age = rnd.Next(20), createDate=DateTime.Now.ToString(), classroom = "一班" }); } } }
6、界面设计:
<Window x:Class="WpfApplication3.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="auto"/> <RowDefinition Height="*"/> <RowDefinition Height="auto"/> <RowDefinition Height="22"/> </Grid.RowDefinitions> <TextBlock Text="access数据基本读写操作"></TextBlock> <DataGrid Grid.Row="1" Name="dg" ItemsSource="{Binding}" CanUserAddRows="False" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="姓名" Binding="{Binding username}" Width="100"></DataGridTextColumn> <DataGridTextColumn Header="密码" Binding="{Binding pwd}"></DataGridTextColumn> <DataGridTextColumn Header="年龄" Binding="{Binding age}"></DataGridTextColumn> <DataGridTextColumn Header="添加时间" Binding="{Binding createdate}"></DataGridTextColumn> <DataGridTextColumn Header="更新时间" Binding="{Binding updatedate}"></DataGridTextColumn> <DataGridComboBoxColumn x:Name="cr" Header="班级" SelectedItemBinding="{Binding classroom}"></DataGridComboBoxColumn> </DataGrid.Columns> </DataGrid> <WrapPanel Grid.Row="2" DataContext="{Binding ElementName=dg,Path=SelectedItem}"> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"> <TextBlock Text="姓名:"/> <TextBlock MinWidth="100" Text="{Binding username}"/> </StackPanel> <StackPanel Orientation="Horizontal"> <TextBlock Text="密码:"/> <TextBlock MinWidth="100" Text="{Binding pwd}"/> </StackPanel> <StackPanel Orientation="Horizontal"> <TextBlock Text="年龄:"/> <TextBlock MinWidth="100" Text="{Binding age}"/> </StackPanel> <StackPanel Orientation="Horizontal"> <TextBlock Text="添加时间:"/> <TextBlock MinWidth="100" Text="{Binding createdate}"/> </StackPanel> <StackPanel Orientation="Horizontal"> <TextBlock Text="更新时间:"/> <TextBlock MinWidth="100" Text="{Binding updatedate}"/> </StackPanel> <StackPanel Orientation="Horizontal"> <TextBlock Text="班级:"/> <TextBlock MinWidth="100" Text="{Binding classroom}"/> </StackPanel> </WrapPanel> <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Grid.Row="3" > <Button Content="刷新" Height="22" VerticalAlignment="Top" Click="Button_Click" /> <Button Content="更新" Click="Button_Click_1"/> </StackPanel> </Grid></Window>
7、对刷新 和更新按钮添加事件:
private void Button_Click(object sender, RoutedEventArgs e) { BindData(); } private void Button_Click_1(object sender, RoutedEventArgs e) { var p=dg.SelectedItem as Person; if (p == null) { MessageBox.Show("请选择要更新的行"); return; } using (var conn = DbHelper.getCon()) { conn.Execute("update users set username=@username,pwd=@pwd,age=@age,createdate=@c,updatedate=@u,classroom=@classroom where id=@id", new { username=p.username,pwd=p.pwd,age=p.age,c=p.createdate,u=p.updatedate,classroom=p.classroom ,id=p.id }); MessageBox.Show("ok"); } }
WPF以access为数据库,简单实现一个显示数据和更新数据的实例
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。