[ADO.NET]DataTableのデータをバインドする
DataTableのデータをバインドする
データベースからDataTableに取得したデータをそのままコントロールにバインド(結びつけ)するには、
各コントロールのDataSourceプロパティを使用する。
バインドが可能なコントロールの中には
・コンボボックス
・リストボックス
・DataGridView
などがある。
以下は、NorthWindデータベースからEmployeesテーブルを取得し、その中の
City列のデータをコンボボックスに表示する例。
string connectionString = "略";
using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand("SELECT * FROM Employees", connection))
using (SqlDataAdapter adapter = new SqlDataAdapter(command))
{
DataTable table = new DataTable();
table.Locale = CultureInfo.InvariantCulture;
try
{
adapter.Fill(table);
}
catch (SqlException)
{
}
comboBox1.DataSource = table;
comboBox1.DisplayMember = "City";
}
結果は以下のようになる。
[ADO]データベースからデータを取得する
データベースからデータを取得する
参考:データベースへの接続
ADO.NETの大きな特徴として(ADO.NETに限らないけど)、データ接続オブジェクトと
非接続オブジェクトの分離がなされている部分がある。
接続オブジェクトを用いて非接続オブジェクトにデータをキャッシュし、データベースへの
接続を行わずにデータにアクセスすることができる。
ここでは、SqlDataAdapterオブジェクトを用いてテーブルのデータを取得する方法の紹介を行う。
string connectionString = "略";
using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand("SELECT * FROM Employees", connection))
using (SqlDataAdapter adapter = new SqlDataAdapter(command))
{
DataTable table = new DataTable();
table.Locale = CultureInfo.InvariantCulture;
try
{
adapter.Fill(table);
}
catch (SqlException)
{
}
// 取得したデータの社員番号をチェック
foreach (DataRow row in table.Rows)
{
int EmployeeID = Convert.ToInt32(row["EmployeeID"]);
}
}
SqlConnectionオブジェクトに接続文字列、SqlCommandオブジェクトにSELECTクエリ文を設定し、
SqlDataAdapterオブジェクトに渡す。
SqlDataAdapterオブジェクトのFill()メソッドを使用して、DataTable(非接続オブジェクト)に
データを一括で格納させる。
[ADO]データベースへの接続
データベースへの接続を確認する
[環境]
サーバ:SqlServer2005 ExpressEdition
DB:NorthWind
データベースへ接続するには、SqlConnectionを使用する。
string connectionString = "略";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
if (connection.State == ConnectionState.Open)
{
MessageBox.Show("OPEN");
}
connection.Close();
}
SqlConnectionオブジェクトに接続文字列を設定し、Open()メソッドを呼ぶことで
データベースへの接続を確立する。
また、閉じる際はClose()メソッドを用いる。
※接続文字列については、
「サーバエクスプローラ」→「NorthWind」を選択。→右クリック→プロパティ
とやれば、接続文字列が表示されるので、これを入れてやればとりあえずOK。
詳細な設定をしたい人は調べてください。
[ADO]サンプルデータベースの入手(NorthWind)
次のサイトからサンプルデータベース(NorthWind)を入手する。
Northwind and pubs Sample Databases for SQL Server 2000
実行すると(自分の環境では)
C:\SQL Server 2000 Sample Databases
にサンプルデータベースが保存された。
使用する際は、VisualStudioからまたはSQL Server Management Studioからアタッチする。
■VisualStudioからアタッチ
「表示」→「サーバエクスプローラ」
↓
「データ接続」→「接続の追加」
↓
データソースを「Microsoft SQL Server」に。
↓
サーバ名を選択
↓
データベースファイルのアタッチ→「NORTHWND.MDF」を選択
以上で、NorthWindデータベースが使用可能となる。
今後当ブログでデータベースを扱う際は、NorthWindを参照しての説明とする。
■NorthWindに含まれるテーブル
・Categories
・CustomerCustomerDemo
・CustomerDemographics
・Customers
・Employees
・EmployeeTerritories
・Order Details
・Orders
・Products
・Region
・Shippers
・Suppliers
・Territories
- プログラミングMicrosoft ADO.NET (マイクロソフト公式解説書)/David Sceppa
- ¥7,140
- Amazon.co.jp
[C#]ToolStripMenuItemの表示状態を変更する
ToolStripMenuItemの表示状態を変更する
MenuStripなどに子アイテムとしてくっつけるToolStripMenuItemの表示状態を切り替えるには、
Visibleプロパティではなく、Availableプロパティを変更する。
↓のような感じ。
/// <summary>
/// ボタンでドロップダウンアイテムの表示を切り替えるテスト
/// </summary>
private void button1_Click(object sender, EventArgs e)
{
subsubToolStripMenuItem.Available = !subsubToolStripMenuItem.Available;
}
こいつには原因つかむまで結構やられた。。