コメント |
@DIV
using System.Data.Odbc;
using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
// ********************************************************
// * 実行
// ********************************************************
public class App
{
public static void Main() {
DoAction myApp = new DoAction();
}
}
public class DoAction
{
public DoAction() {
// ============================================
// コンストラクタの呼び出し
// ============================================
lightbox lb = new lightbox(
"Driver={MySQL ODBC 3.51 Driver};" +
"SERVER=localhost;" +
"DATABASE=mydb;" +
"UID=root;" +
"PWD=" );
// ============================================
// プログラムが存在するディレクトリを取得
// ============================================
String pgDir = lb.GetProgramDir();
MessageBox.Show( pgDir );
// ============================================
// 変換したパスを初期ディレクトリとして設定
// ============================================
OpenFileDialog dlg = new OpenFileDialog();
dlg.InitialDirectory = pgDir;
dlg.Filter = "インポート用CSV|*.csv|全て|*.*";
dlg.FilterIndex = 2;
dlg.RestoreDirectory = true;
// 列名保存用配列
String[] aDataName = null;
// データ名用配列
String[] aDataValue = null;
// ファイルの中身用
Stream myStream = null;
// SQL 作成用
String str = "";
// 存在チェック用
String strQuery = "";
// ============================================
// ファイルを開くダイアログを表示
// ============================================
if ( dlg.ShowDialog() == DialogResult.OK ) {
// ============================================
// myStream としてファイルの中身を取得
// ============================================
myStream = dlg.OpenFile();
if ( !(myStream == null) ) {
// ============================================
// DB 接続
// ============================================
if ( !lb.Connect() ) {
myStream.Close();
MessageBox.Show( "接続失敗の為、終了します " );
System.Environment.Exit( 0 );
}
// ============================================
// myStream を読む為の StreamReader クラスを作成
// ファイルは Shift_JIS なので明示する必要がある
// ============================================
StreamReader sr =
new StreamReader(
myStream,
Encoding.GetEncoding("Shift_JIS")
);
// ============================================
// ループ処理で行を結合して、SQL 文を作成
// ============================================
int nCounter = 0;
int i = 0;
while( sr.Peek() >= 0 ) {
if ( nCounter == 0 ) {
aDataName = lb.GetArray(sr.ReadLine(),",");
}
else {
aDataValue = lb.GetArray(sr.ReadLine(),",");
strQuery = "select * from 社員マスタ where ";
strQuery += "社員コード = '" + aDataValue[0] + "'";
if ( lb.ExistData( strQuery ) ) {
// update 作成
str = "update 社員マスタ set ";
for( i = 0; i < aDataName.Length; i++ ) {
// データ内のシングルクォートの処理
aDataValue[i] = aDataValue[i].Replace("'", "''");
if ( i != 0 ) {
str += ",";
}
if ( i < 4 ) {
str += aDataName[i];
str += " = '";
str += aDataValue[i];
str += "'";
}
if ( i == 4 ) {
str += aDataName[i];
str += " = ";
str += aDataValue[i];
}
if ( i > 4 ) {
str += aDataName[i];
str += " = '";
str += aDataValue[i];
str += "'";
}
}
str += " where 社員コード = ";
str += "'" + aDataValue[0] + "'";
}
else {
// insert 作成
str = "insert into 社員マスタ (";
for( i = 0; i < aDataName.Length; i++ ) {
if ( i != 0 ) {
str += ",";
}
str += aDataName[i];
}
str += ") values(";
for( i = 0; i < aDataName.Length; i++ ) {
// データ内のシングルクォートの処理
aDataValue[i] = aDataValue[i].Replace("'", "''");
if ( i != 0 ) {
str += ",";
}
if ( i < 4 ) {
str += "'";
str += aDataValue[i];
str += "'";
}
if ( i == 4 ) {
str += aDataValue[i];
}
if ( i > 4 ) {
str += "'";
str += aDataValue[i];
str += "'";
}
}
str += ")";
}
MessageBox.Show(str);
lb.UpdateDB(str);
}
nCounter++;
}
// ============================================
// リーダとストリームを閉じる
// ============================================
sr.Close();
myStream.Close();
}
// ============================================
// 接続解除
// ============================================
lb.Close();
}
}
}
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// 処理クラス
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
public class lightbox
{
// 接続オブジェクト
private OdbcConnection myCon = new OdbcConnection();
// コマンドオブジェクト
private OdbcCommand myCommand = new OdbcCommand();
// 接続文字列
public String strCon = String.Empty;
// ********************************************************
// コンストラクタの定義( クラスと同名 )
// ********************************************************
public lightbox( String str ) {
strCon = str;
}
// ********************************************************
// 接続処理
// ********************************************************
public bool Connect() {
bool bRet = true;
// 接続文字列セット
myCon.ConnectionString = strCon;
// ====================================================
// 例外処理
// ====================================================
try {
// 接続
myCon.Open();
}
catch( Exception ex ) {
bRet = false;
MessageBox.Show( ex.Message );
}
// コマンドオブジェクトを接続に関係付ける
myCommand.Connection = myCon;
return bRet;
}
// ********************************************************
// 接続解除処理
// ********************************************************
public void Close() {
myCon.Close();
}
// ********************************************************
// 配列化関数
// ********************************************************
public String[] GetArray(String str,String d) {
// 区切り文字定義
String delimStr = d;
Char[] delimiter = delimStr.ToCharArray();
// 分解
String[] aRet = str.Split(delimiter);
return aRet;
}
// ********************************************************
// 存在チェック
// ********************************************************
public bool ExistData(String strQuery) {
OdbcDataReader myReader = null;
bool bRet = true;
// コマンドセット
myCommand.CommandText = strQuery;
// ====================================================
// 例外処理
// ====================================================
try {
// リーダーオブジェクト取得
myReader = myCommand.ExecuteReader();
}
catch( Exception ex ) {
myCon.Close();
MessageBox.Show( ex.Message );
System.Environment.Exit( 0 );
}
bRet = myReader.HasRows;
myReader.Close();
return bRet;
}
// ********************************************************
// 更新関数
// ********************************************************
public bool UpdateDB(String strExec) {
myCommand.CommandText = strExec;
// ====================================================
// 例外処理
// ====================================================
try {
// 実行
myCommand.ExecuteNonQuery();
}
catch( Exception ex ) {
myCon.Close();
MessageBox.Show( ex.Message );
System.Environment.Exit( 0 );
}
return true;
}
// ********************************************************
// プログラムが実行されているディレクトリを取得
// ********************************************************
public String GetProgramDir( ) {
System.Reflection.Assembly ab = System.Reflection.Assembly.GetExecutingAssembly();
System.Reflection.Module md = ab.GetModules()[0];
// **************************************************************
// ディレクトリ部分を取得( System.IO.Path.GetDirectoryName が簡単 )
// **************************************************************
String[] aDataDir = GetArray( md.FullyQualifiedName, @"\" );
String pgDir = String.Join( @"\", aDataDir, 0, aDataDir.Length - 1 );
return pgDir;
}
}
@END
|