ソース掲示板




すべてから検索

キーワード   条件 表示 現行ログ 過去ログ トピックス 名前 本文

  メンテナンス 前画面に戻る

対象スレッド 件名: C# : Odbc 経由のデータベースへの select の結果を DataGrid に表示
名前: lightbox
処理選択
パスワード

件名 C# : Odbc 経由のデータベースへの select の結果を DataGrid に表示
名前 lightbox
コメント
@HTML
▼ 販売管理B.mdb のダウンロード<br>
<a href="http://winofsql.jp/download/hanbaib.zip"><img src="http://winofsql.jp/image/lightbox_download.jpg" style="border: solid 0px #000000"></a>
@HEND

@SHOW
https://lh3.googleusercontent.com/-eKT2olkz18Q/WSPCDgFbbAI/AAAAAAAAkhg/h3vP6RyA7xkbxpH35__d4Mlsaf93k43DgCHM/s579/_img.png

System.Data.Linq を参照します

https://lh3.googleusercontent.com/-eWYNdjg7NL0/WSPDVoGqa7I/AAAAAAAAkh0/cISxxDW3jP4V4ickV7AOkI9cx40pU-5PQCHM/s571/_img.png

@END

@DIV
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.Linq;
using System.Data.Odbc;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1 {
	/// <summary>
	/// MainWindow.xaml の相互作用ロジック
	/// </summary>
	public partial class MainWindow : Window {

		private OdbcConnection cn = null;
		private ObservableCollection<Syain> syain_list = null;

		public MainWindow() {
			InitializeComponent();
		}

		private void Button_Click_1(object sender, RoutedEventArgs e) {
			Console.WriteLine("クリックされました");

			string cs =
				"Provider=MSDASQL" +
				";Driver={Microsoft Access Driver (*.mdb)}" +
				@";Dbq=..\..\lib\販売管理B.mdb" +
				";";

			string query;
			if ( simei.Text != "") {
				query = String.Format("select * from 社員マスタ where 氏名 like '%{0}%'", simei.Text);
			}
			else {
				query = "select * from 社員マスタ";
			}

			try {
				cn = new OdbcConnection(cs);
				DataContext context = new DataContext(cn);
				syain_list = new ObservableCollection<Syain>(
						context.ExecuteQuery<Syain>(query)
				);
				this.dataGrid.DataContext = syain_list;
			}
			catch (Exception ex) {
				Debug.WriteLine(ex.Message);
			}

			if (cn.State == ConnectionState.Open) {
				cn.Close();
				cn.Dispose();
			}


			// DataGrid にバインド
			dataGrid.DataContext = syain_list;


			// DataGrid にバインドした ObservableCollection 内のデータ
			for (int i = 0; i < syain_list.Count; i++ ) {
				Debug.WriteLine(syain_list[i].社員コード + "|" + syain_list[i].氏名);
			}

		}

		// DataGrid をダブルクリックした時の行データの取得
		private void dataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e) {
			int row = dataGrid.SelectedIndex;
			if ( row >= 0 ) {
				Syain syain = (Syain)dataGrid.Items.GetItemAt(row);
				Debug.WriteLine(syain.社員コード + "|" + syain.氏名);

				MessageBox.Show(syain.社員コード + "|" + syain.氏名,
					"確認",
					MessageBoxButton.OK,
					MessageBoxImage.Information);
			}
		}

	}
}

@END