« NpgsqlでPostgreSQLにアクセス[DataGridViewでコンボボックス] | メイン | NpgsqlでPostgreSQLにアクセス[リレーション#1] »

NpgsqlでPostgreSQLにアクセス[一休み]

2007/07/18 21:54

前回の続きですが、今回は前にやったことの繰り返し。

ベンダーを登録する画面を作ります。
内容はカテゴリの登録と同じです(^^;

フォームは以下のとおり。
form_mst_vendor.gif

ソースは次のとおりです。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Npgsql;

namespace Npgsql_sample
{
    public partial class form_mst_vendor : Form
    {
        // メンバ変数
        // データベース接続子
        private NpgsqlConnection m_conn = new NpgsqlConnection
        (
              "Server=localhost;"
            + "Port=5432;"
            + "User Id=Npgsql_sample;"
            + "Password=hogehoge;"
            + "Database=Npgsql_sample;"
            + "Encoding=UNICODE"
        );
        // mst_vendorのデータアダプタ
        private NpgsqlDataAdapter m_da_mst_vendor = new NpgsqlDataAdapter();
        // データセット
        private DataSet m_ds = new DataSet();

        public form_mst_vendor()
        {
            InitializeComponent();
        }

        private void form_mst_vendor_Load(object sender, EventArgs e)
        {
            // mst_vendorのデータアダプタ構成
            // selectコマンド
            m_da_mst_vendor.SelectCommand = new NpgsqlCommand
            (
                   "select"
                +      " vendor_id"
                +     ", vendor_name"
                +     ", create_date"
                +     ", update_date"
                + " from"
                +      " mst_vendor"
                + " order by vendor_name",
                m_conn
            );
            // insertコマンド
            m_da_mst_vendor.InsertCommand = new NpgsqlCommand
            (
                   "insert into mst_vendor ("
                +      " vendor_name"
                + " ) values ("
                +      " :vendor_name"
                + " )",
                m_conn
            );
            m_da_mst_vendor.InsertCommand.Parameters.Add(new NpgsqlParameter("vendor_name", NpgsqlTypes.NpgsqlDbType.Text, 0, "vendor_name", ParameterDirection.Input, false, 0, 0, DataRowVersion.Current, DBNull.Value));
            // updateコマンド
            m_da_mst_vendor.UpdateCommand = new NpgsqlCommand
            (
                "update mst_vendor set"
                + " vendor_name=:vendor_name"
                + ", update_date=current_timestamp"
                + " where"
                + " vendor_id=:vendor_id",
                m_conn
            );
            m_da_mst_vendor.UpdateCommand.Parameters.Add(new NpgsqlParameter("vendor_name", NpgsqlTypes.NpgsqlDbType.Text   , 0, "vendor_name", ParameterDirection.Input, false, 0, 0, DataRowVersion.Current , DBNull.Value));
            m_da_mst_vendor.UpdateCommand.Parameters.Add(new NpgsqlParameter("vendor_id"  , NpgsqlTypes.NpgsqlDbType.Integer, 0, "vendor_id"  , ParameterDirection.Input, false, 0, 0, DataRowVersion.Original, DBNull.Value));
            // deleteコマンド
            m_da_mst_vendor.DeleteCommand = new NpgsqlCommand
            (
                   "delete from mst_vendor"
                + " where"
                +      " vendor_id=:vendor_id",
                m_conn
            );
            m_da_mst_vendor.DeleteCommand.Parameters.Add(new NpgsqlParameter("vendor_id"  , NpgsqlTypes.NpgsqlDbType.Integer, 0, "vendor_id"  , ParameterDirection.Input, false, 0, 0, DataRowVersion.Original, DBNull.Value));
            // RowUpdateイベントの追加
            m_da_mst_vendor.RowUpdated += new NpgsqlRowUpdatedEventHandler(mst_vendorRowUpdated);

            // データセット生成
            m_da_mst_vendor.Fill(m_ds, "mst_vendor");

            // bind_mst_vendorを設定
            bind_mst_vendor.DataSource = m_ds;
            bind_mst_vendor.DataMember = "mst_vendor";

            // カラムを関連付け
            col_vendor_id.DataPropertyName   = "vendor_id";
            col_vendor_name.DataPropertyName = "vendor_name";
            col_create_date.DataPropertyName = "create_date";
            col_update_date.DataPropertyName = "update_date";
        }

        // 保存ボタンをクリック
        private void cmd_save_Click(object sender, EventArgs e)
        {
            int update_count = 0;
            try
            {
                update_count += m_da_mst_vendor.Update(m_ds.Tables["mst_vendor"]);
            }
            catch (Exception ex)
            {
                MessageBox.Show("ベンダーの保存に失敗しました。\n\n[内容]\n" + ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            MessageBox.Show(update_count.ToString() + "件、保存しました。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        // mst_vendorのデータアダプタのRowUpdatedイベント
        private void mst_vendorRowUpdated(Object sender, NpgsqlRowUpdatedEventArgs e)
        {
            if (e.Status == UpdateStatus.Continue)
            {
                if (e.StatementType == StatementType.Insert)
                {
                    // vendor_idとcreate_date,update_dateを取得
                    NpgsqlCommand cmd = new NpgsqlCommand("select vendor_id, create_date, update_date from mst_vendor where vendor_id=currval('mst_vendor_vendor_id_seq')", m_conn);
                    try
                    {
                        NpgsqlDataReader reader = cmd.ExecuteReader();
                        reader.Read();
                        e.Row["vendor_id"]   = reader["vendor_id"];
                        e.Row["create_date"] = reader["create_date"];
                        e.Row["update_date"] = reader["update_date"];
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
                else if (e.StatementType == StatementType.Update)
                {
                    // update_dateを取得
                    NpgsqlCommand cmd = new NpgsqlCommand("select update_date from mst_vendor where vendor_id=" + e.Row["vendor_id"].ToString(), m_conn);
                    try
                    {
                        NpgsqlDataReader reader = cmd.ExecuteReader();
                        reader.Read();
                        e.Row["update_date"] = reader["update_date"];
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }
        }

        // フォームを閉じるときのイベント
        private void form_mst_vendor_FormClosing(object sender, FormClosingEventArgs e)
        {
            int update_count = 0;
            update_count += m_ds.Tables["mst_vendor"].Select(null, null, DataViewRowState.Added).Length;
            update_count += m_ds.Tables["mst_vendor"].Select(null, null, DataViewRowState.ModifiedCurrent).Length;
            update_count += m_ds.Tables["mst_vendor"].Select(null, null, DataViewRowState.Deleted).Length;
            if (update_count > 0)    // 1件以上更新されていれば確認画面を表示
            {
                if (MessageBox.Show("ベンダーが保存されていません。\n本当に閉じますか?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    e.Cancel = true;
                }
            }

        }
    }
}

次は、商品編集画面にベンダーごとの単価を登録できるようにします。

投稿者: kkino :  パーマリンク

トラックバック

このエントリーのトラックバックURL:
http://90h.dip.jp/cgi-bin/mt/mt-tb.cgi/162

コメントを投稿






検索


About

2007/07/18 21:54に投稿されたエントリーのページです。

ひとつ前の投稿は「NpgsqlでPostgreSQLにアクセス[DataGridViewでコンボボックス]」です。

次の投稿は「NpgsqlでPostgreSQLにアクセス[リレーション#1]」です。

他にも多くのエントリーがあります。メインページアーカイブページも見てください。

最近のエントリー

カテゴリー

Apache (2)
C# (8)
Delphi (3)
MovableType (1)
Npgsql (7)
Oracle (2)
PHP (2)
PostgreSQL (10)
Windows (3)
XML (1)
ZETA (1)
debian (17)
mysql (1)
ubuntu (1)
その他 (4)
オンラインソフト (1)
玄箱 (13)

アーカイブ

このブログのフィードを取得