하고재비
[WPF] Datagrid and MDB 본문
WPF환경에서 Access의 MDB파일을 Select 하여 DataGrid에 뿌려보자.
이후 기초적인 CRUD, Where절 활용까지 게시할 예정.(2018년 안으로...ㅎ)
1. 프로젝트 생성 후 DB가져오기
테이블(, 필요에 따라 뷰)에 체크를 꼭 해줘야 합니다.
2. coding
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | <!-- XAML --> <Window x:Class="WPF_MDB_Example.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:WPF_MDB_Example" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Grid> <DataGrid x:Name="dataGrid_Orders" HorizontalAlignment="Left" Margin="10" VerticalAlignment="Top"/> </Grid> </Window> <!-- cs --> namespace WPF_MDB_Example { /// <summary> /// MainWindow.xaml에 대한 상호 작용 논리 /// </summary> /// public partial class MainWindow : Window { string ConString = ConfigurationManager .ConnectionStrings["WPF_MDB_Example.Properties.Settings.MDB_SampleConnectionString"] .ConnectionString; OleDbCommand cmd = null; // 데이터 원본에 대해 실행할 SQL 문이나 저장된 프로시저를 나타냅니다. public MainWindow() { InitializeComponent(); set_Data(); } public void set_Data() { try { string CmdString = string.Empty; using (OleDbConnection con = new OleDbConnection(ConString)) { CmdString = "SELECT * FROM `Orders`"; cmd = new OleDbCommand(CmdString, con); OleDbDataAdapter sda = new OleDbDataAdapter(cmd); DataTable DT = new DataTable("Run Information"); sda.Fill(DT); dataGrid_Orders.ItemsSource = DT.DefaultView; } } catch (Exception e) { MessageBox.Show(e.Message, "Material Infomation Select Failed"); return; } } } } | cs |
코딩을 하다보면 Configuration 쪽에서 밑줄이 그일텐데 눈치껏 참조에서 System.configuration 참조 추가.
3. 실행 결과
3-1. Select table
3-2. 결과
'C# Windows Forms WPF' 카테고리의 다른 글
[C#] 프로그램 방식의 click event 발생 (0) | 2019.09.03 |
---|---|
[C#] 지수 변환 (0) | 2019.06.14 |
[C#] 콘솔창 유지 (0) | 2019.06.02 |
[WPF] User Control Example for WPF (0) | 2018.12.09 |
[C#] C# Log4Net log남기기 (0) | 2018.12.09 |
Comments