Я построил форму WPF с DataGrid и хотел бы заполнить ее таблицей из базы данных ODB C. Мой текущий код, кажется, в состоянии открыть соединение, но я не могу заполнить данные в DataGrid. Может кто-нибудь объяснить мне, почему?
- TableName = COMPANY
- DatabaseName = DBFS
Эта часть работает (с закомментированным разделом в конце). Форма WPF отображается без ошибок:
using System.Data.Odbc;
using System.Windows;
using System.Data;
namespace DB_inspector
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
OdbcConnection dbConnection = new OdbcConnection("Driver={Pervasive ODBC Client Interface};ServerName=0875;dbq=@DBFS;Uid=Master;Pwd=Password;");
string strSql = "select * from COMPANY";
dbConnection.Open();
OdbcDataAdapter dadapter = new OdbcDataAdapter();
dadapter.SelectCommand = new OdbcCommand(strSql, dbConnection);
DataTable table = new DataTable("COMPANY");
//dadapter.Fill(table);
//this.DataGrid1.DataContext = table;
//DataGrid1.ItemsSource = table.DefaultView;
//dadapter.Update(table);
dbConnection.Close();
}
}
}
Это не работает, работает без ошибок, но я не вижу даже формы WPF, появившейся через некоторое время ожидания:
using System.Data.Odbc;
using System.Windows;
using System.Data;
namespace DB_inspector
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
OdbcConnection dbConnection = new OdbcConnection("Driver={Pervasive ODBC Client Interface};ServerName=0875;dbq=@DBFS;Uid=Master;Pwd=Password;");
string strSql = "select * from COMPANY";
dbConnection.Open();
OdbcDataAdapter dadapter = new OdbcDataAdapter();
dadapter.SelectCommand = new OdbcCommand(strSql, dbConnection);
DataTable table = new DataTable("COMPANY");
dadapter.Fill(table);
this.DataGrid1.DataContext = table;
DataGrid1.ItemsSource = table.DefaultView;
dadapter.Update(table);
dbConnection.Close();
}
}
}
Приложение .xaml
<Application x:Class="DB_inspector.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DB_inspector"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
XAML
<Window x:Class="DB_inspector.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Liinos_inspector"
mc:Ignorable="d"
Title="DB database inspector" Height="595.404" Width="1005.571">
<Grid>
<DataGrid x:Name="DataGrid1" HorizontalAlignment="Left" Height="478" Margin="10,76,0,0" VerticalAlignment="Top" Width="978"/>
<Image HorizontalAlignment="Left" Height="41" Margin="829,15,0,0" VerticalAlignment="Top" Width="141" Source="logo_small.jpg"/>
<TextBox HorizontalAlignment="Left" Height="31" Margin="10,25,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="489"/>
<Button Content="Search" HorizontalAlignment="Left" Height="31" Margin="516,25,0,0" VerticalAlignment="Top" Width="133" Background="#FF73DA63" BorderBrush="{x:Null}" Foreground="White"/>
</Grid>
</Window>