ms访问数据库2007与c#中的wpf之间的连接,并以wpf显示数据 [英] Connection between ms access database 2007 and wpf in c# and to display the data in wpf

查看:96
本文介绍了ms访问数据库2007与c#中的wpf之间的连接,并以wpf显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我试图在ms access 2007和wpf之间建立连接。



当我调试代码时,我只是出现了表格。



没有错误也没有输出。



我有一张doctor_records表。

使用WPF -visual studio 2010.

Wpf主窗口 - 有四个文本框和4个标签。



请找到以下代码,请告诉我我哪里出错。



代码:

  public   partial   class  MainWindow:Window 
{
OleDbConnection con = new OleDbConnection( @ Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\ Users\SR0 \Documents\Doctors.accdb);
public MainWindow()
{
InitializeComponent();
Bind();

}
私人 void Bind()
{
con.Open();
OleDbDataAdapter da = new OleDbDataAdapter( 选择*来自Doctors_Records,con);
DataTable dt = new DataTable();
da.Fill(dt);
con.Close();
}
}
}

解决方案

您需要使用数据绑定。



查看这个



WPF数据绑定 - 第1部分 [ ^ ]


这是我做的:



在Window.xaml中:



窗口>

网格x:名称=root>

DataGrid ItemsSource ={Binding}>



/ DataGrid>

/网格>

/窗口>



(我无法输入'< ;'出于某种原因在上面的代码中...但我的窗口中只有一个DataGrid包含在一个名为'root'的网格中。



==== ================



在Window.xaml.cs中:



公共部分类MainWindow:Window

{



DataTable dt = new DataTable();

OleDbConnection con = new OleDbConnection(@Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\Users\username\Documents\Access\SampleData.accdb);

public MainWindow()

{

InitializeComponent();

GetData();



}

private void获取数据()

{

con.Open();

OleDbDataAdapter da = new OleDbDataAdapter(Select * from Table1,con);



da.Fill(dt);



root.DataContext = dt;



con.Close();

}





}

Hi ,

I tried to establish a connection between ms access 2007 and wpf .

When i debug the code i just have the form appear.

No errors and no output also.

I am having a doctors_records table.
using WPF -visual studio 2010.
Wpf main window - has four text boxes and 4 labels.

Please find the following code and please let me know where i am going wrong.

Code:

     public partial class MainWindow : Window
    {
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\SR0\Documents\Doctors.accdb");
        public MainWindow()
        {
            InitializeComponent();
            Bind();

        }
        private void Bind()
        {
            con.Open();
            OleDbDataAdapter da = new OleDbDataAdapter("Select * from Doctors_Records", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            con.Close();
        }
    }
}

解决方案

you need to use Data Binding.

check this out

WPF Data Binding - Part 1[^]


Here's what I did:

In Window.xaml:

Window>
Grid x:Name="root">
DataGrid ItemsSource="{Binding}" >

/DataGrid>
/Grid>
/Window>

(I cannot type the '<' for some reason in the above code... But I only have a DataGrid in my window wrapped in a grid named 'root')

====================

In Window.xaml.cs:

public partial class MainWindow : Window
{

DataTable dt = new DataTable();
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\username\Documents\Access\SampleData.accdb");
public MainWindow()
{
InitializeComponent();
GetData();

}
private void GetData()
{
con.Open();
OleDbDataAdapter da = new OleDbDataAdapter("Select * from Table1", con);

da.Fill(dt);

root.DataContext = dt;

con.Close();
}


}


这篇关于ms访问数据库2007与c#中的wpf之间的连接,并以wpf显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆