共通関数の定義ファイルを追加(C#)
概要
共通関数を定義したクラスファイルを追加する。
クラスファイルには、データベースへの接続等、比較的どのシステムでも共通するであろう処理を記述してある。
クラスファイルには、データベースへの接続等、比較的どのシステムでも共通するであろう処理を記述してある。
前提条件
手順
[ソリューションエクスプローラ] -> 「新しい項目」
not found (500.jpg)
「クラス」を選択 -> 「追加」
not found (501.jpg)
class1.cs を以下のように変更。
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Odbc;
using System.Text;
using System.Windows.Forms;
public class class_common : Form
{
// ******************************************************
// DB接続
// ******************************************************
public bool DBConnect(
string DBType
, ref OdbcConnection Cn
, string strTarget
, string strDB
, string strUser
, string strPass
)
{
string ConnectionString = "";
if ((Cn == null))
{
Cn = new OdbcConnection();
}
switch (DBType)
{
case "SQLServer":
ConnectionString = "Driver={SQL Server};"
+ "SERVER=" + strTarget + ";"
+ "DATABASE=" + strDB + ";"
+ "UID=" + strUser + ";"
+ "PWD=" + strPass + ";";
break;
case ".NET開発サーバ":
ConnectionString = "Driver={SQL Server};"
+ "SERVER=" + strTarget + ";"
+ "DATABASE=" + strDB + ";"
+ "Integrated Security=SSPI;";
break;
}
try
{
Cn.ConnectionString = ConnectionString;
Cn.Open();
return true;
}
catch
{
return false;
}
}
// ******************************************************
// DB終了処理(接続を閉じる)
// ******************************************************
public bool DBClose( ref OdbcConnection Cn )
{
try
{
if ((int)Cn.State != 1)
{
Cn.Close();
}
return true;
}
catch
{
return false;
}
}
// ******************************************************
// DB読込み
//
// 引数1: DBへの接続
// 引数2: テーブルリーダー(更新クエリ発行時は使用しませんが、ダミーで渡してください。)
// 引数3: SQL
// 引数4: 更新フラグ。
// True: SQL発行のみ行います。
// False: SQL発行後、テーブルリーダーを作成します。
// 戻り値: True(成功) , False(エラー発生)
// ******************************************************
public bool DBGet(
ref OdbcConnection Cn
, ref DataTableReader Dtr
, string SqlQuery
, bool bUpadateFlg)
{
// コマンド作成
OdbcCommand command = new OdbcCommand();
command.CommandText = SqlQuery;
command.Connection = Cn;
// テーブルアダプタ+データセット作成
OdbcDataAdapter DataAdapter;
DataSet DataSet = new DataSet();
// テーブルリーダ初期化
Dtr = null;
try
{
// 更新フラグによって処理分岐
if (bUpadateFlg)
{
command.ExecuteNonQuery();
}
else
{
DataAdapter = new OdbcDataAdapter(command);
DataAdapter.Fill(DataSet);
Dtr = DataSet.CreateDataReader();
}
return true;
}
catch
{
return false;
}
}
}
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 * from hoge ";
// データ取得
DataTableReader Dtr = null;
if (!DBGet(ref Cn, ref Dtr, selectQuery, false))
{
MessageBox.Show("データ取得失敗");
}
DataTable DT = new DataTable();
DT.Load(Dtr);
dataGridView1.DataSource = DT;
}
}
}