ë‹·ë„·, WPF, XAML, ¤ë‌¼´, 맬, –´ëŒ‘„°‌´© AOD.NET 간단 ˆ œ(C#, Wpf,odp.net,ado.net,linq simple example)_WPF굜/WPF•™›

  • View
    514

  • Download
    10

Embed Size (px)

Text of ë‹·ë„·, WPF, XAML, ¤ë‌¼´,...

PowerPoint

C#, WPF, ADO.NETXAML, Oracle ODP.NET, Linq (www.topcredu.co.kr),

Oracle Develpoer Tools C#, WPF, ADO.NET, ODP.NET, , OracleDataAdapter, Linq DB EMP ListView .Visual Studio 2015 ODP.NET Oracle Developer Tool for Visual Studio 2015 . ( http://www.oracle.com/technetwork/topics/dotnet/downloads/odacmsidownload-2745497.html ) NETWORK\Admin tnsnames.ora Oracle Server ONJ = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = onj) DB ) )

WPF ( : WpfOracleTest) : -> -> -> Oracle.ManagedDataAccess PC 64Bit 10 -> (Target Platform) x64 . (32Bit )

MainWindow.xaml(1)

MainWindow.xaml(3)

MainWindow.xaml.cs(1)using System;using System.Collections.Generic;using System.Windows;using System.Windows.Controls;using System.Data;using Oracle.ManagedDataAccess.Client;using System.Collections;using System.Linq;namespace WpfOracleTest { public partial class MainWindow : Window {

MainWindow.xaml.cs(2) public MainWindow() { InitializeComponent(); } OracleConnection conn; private void DB_Connect(object sender, RoutedEventArgs e) { try { string strCon = "data source=onj;User ID=scott;Password=tiger"; conn = new OracleConnection(strCon); conn.Open(); MessageBox.Show("DB Connection OK!"); }

MainWindow.xaml.cs(3) catch(Exception error) { MessageBox.Show(error.ToString()); } } /* Connection, Command, DataReader */ private void Select_Emp(object sender, RoutedEventArgs e) { string sql = "select empno, ename, job from emp "; OracleCommand comm = new OracleCommand(); if(conn == null) DB_Connect(this, null); comm.Connection = conn; comm.CommandText = sql;

MainWindow.xaml.cs(4)OracleDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection); List emps = new List(); while (reader.Read()) { emps.Add(new EmpViewModel() { Empno = reader.GetInt32(reader.GetOrdinal("empno")), Ename = reader.GetString(reader.GetOrdinal("ename")), Job = reader.GetString(reader.GetOrdinal("job")) });} lstView.ItemsSource = emps; }

MainWindow.xaml.cs(5)/* OracleDataAdapter EMP */ private void Select_Emp2(object sender, RoutedEventArgs e) { OracleDataAdapter adapter = new OracleDataAdapter(); string sql = "select empno, ename, job from emp "; OracleCommand comm = new OracleCommand(); if (conn == null) DB_Connect(this, null); comm.Connection = conn; adapter.SelectCommand = comm; comm.CommandText = sql; DataSet ds = new DataSet("emps"); adapter.Fill(ds, "emp");

MainWindow.xaml.cs(6) // Clear the ListView control lstView.Items.Clear(); List emps = new List(); for (int i = 0; i < ds.Tables["emp"].Rows.Count; i++) { DataRow dr = ds.Tables["emp"].Rows[i]; emps.Add(new EmpViewModel() { Empno = System.Convert.ToInt32(dr["empno"]), Ename = dr["ename"].ToString(), Job = dr["job"].ToString() }); } lstView.ItemsSource = emps; conn.Close(); }

MainWindow.xaml.cs(7) /* Linq EMP */ private void Select_Emp3(object sender, RoutedEventArgs e) { OracleDataAdapter adapter = new OracleDataAdapter(); string sql = "select empno, ename, job from emp "; OracleCommand comm = new OracleCommand(); if (conn == null) DB_Connect(this, null); comm.Connection = conn; adapter.SelectCommand = comm; comm.CommandText = sql; DataSet ds = new DataSet("emps"); adapter.Fill(ds, "emp"); // Clear the ListView control lstView.Items.Clear();

MainWindow.xaml.cs(8)List emps = new List(); IEnumerable query = from myemp1 in ds.Tables["emp"].AsEnumerable() where myemp1["job"].Equals("CLERK") select myemp1; foreach (DataRow dr in query) { emps.Add(new EmpViewModel() { Empno = System.Convert.ToInt32(dr["empno"]), Ename = dr["ename"].ToString(), Job = dr["job"].ToString() }); } lstView.ItemsSource = emps; conn.Close(); } }}

EmpViewModel.cs(1)using System;using System.ComponentModel;using System.Windows.Input;namespace WpfOracleTest{ public class EmpViewModel : INotifyPropertyChanged { int empno = 0; string ename = string.Empty; string job = string.Empty;// public event PropertyChangedEventHandler PropertyChanged; // public public int Empno { get { return empno; } set { this.empno = value; } } // public public string Ename { get { return ename; } set { this.ename = value; } }

EmpViewModel.cs(2) // public public string Job { get { return job; } set { this.job = value; } } protected void OnPropertyChanged(string propertyName) { // . if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } }}