DBConnect・DBClose・DBGet(System.Data.Odbc 名前空間)(C#)
概要
ADO.NET(ODBC)によるDB操作関数群。
※この項のサンプルは、私のローカルマシン(マシン名:YOKOSAN)の VisualStudio2005 に付属するSQLServer(ExpressEdition)に接続する場合の接続文字列をそのまま記載してある為、環境に応じて適宜変更すること。
参照
参考
前提条件
手順
Form1.cs[デザイン] に、DataGridView コントロールを配置する。
「親コンテナにドッキングする」を選択。
「親コンテナにドッキングする」を選択。
not found (498.jpg)
Form1.cs を以下のように変更。
「namespace」は適宜変更のこと。
「namespace」は適宜変更のこと。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Odbc;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication5
{
// 共通処理定義クラスを継承
public partial class Form1 : class_common
{
public Form1()
{
InitializeComponent();
string DBType = ".NET開発サーバ";
string strTarget = "YOKOSAN\\SQLEXPRESS";
string strDB = "master";
string strUser = "";
string strPass = "";
// コネクション作成
OdbcConnection Cn = new OdbcConnection();
if ( !DBConnect(DBType, ref Cn, strTarget, strDB, strUser, strPass))
{
MessageBox.Show("接続失敗");
}
// クエリ作成
string selectQuery = "select 1";
// データ取得
DataTableReader Dtr = null;
if (!DBGet(ref Cn, ref Dtr, selectQuery, false))
{
MessageBox.Show("データ取得失敗");
}
// dataGridView1 にデータソース指定
DataTable DT = new DataTable();
DT.Load(Dtr);
dataGridView1.DataSource = DT;
// 接続を閉じる
DBClose(ref Cn);
}
}
}
以下、実行結果。
not found (499.jpg)