デザインパターン�サンプルを掲載����。��カテゴリ�更新���り�定期��。
Factory Methodƒpƒ^�[ƒ“ Œã•Ò
‘O•Ò‚Ì�ÝŒv‚©‚çŽÀ‘•‚ÖˆÚ‚è‚Ü‚·�B
‚Ü‚¸‚ÍDataBaseƒNƒ‰ƒX‚ÌŠî’êƒNƒ‰ƒX‚©‚ç
Šî’êƒNƒ‰ƒX‚Å‚ÍOpen�AClose�AFill�AUpdate‚̉¼‘zƒ�ƒ\ƒbƒh‚ð’è‹`‚µ‚Ü‚·�B
‚³‚ç‚Ƀvƒ�ƒpƒeƒB‚É‚ÍConnection�ACommand‰¼‘zƒvƒ�ƒpƒeƒB‚ð—pˆÓ‚µ‚Ü‚·�B
�@�@
DataBase.cs Šî’êƒNƒ‰ƒX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;

namespace Cancer.ADO
{
    public class  DataBase
    {
        // ƒ�ƒ“ƒo•Ï�”
        private DbConnection _connection = null;
        private DbCommand _command = null;

        // ƒvƒ�ƒpƒeƒB
        public virtual DbConnection Connection
        {
            get
            {
                return _connection;
            }
        }
        public virtual DbCommand Command
        {
            get
            {
                return _command;
            }
        }

        // ƒ�ƒ\ƒbƒh
        public virtual void Open()
        {
            Connection.Open();
        }

        public virtual void Close()
        {
            Connection.Close();
        }
        public virtual void Fill(DataSet ds)
        {
            ;
        }
        public virtual void UpDate(DataSet ds)
        {
            ;
        }
    }
}

ŽŸ‚ÉŠî’êƒNƒ‰ƒX‚ðŒp�³‚µ‚½MSAccess‚ð‘€�ì‚·‚éƒNƒ‰ƒX‚ðŽÀ‘•‚µ‚Ü‚·�B
OleDBƒf�[ƒ^ƒvƒ�ƒoƒCƒ_�[‚ÌConnection‚ÆCommand‚ðƒRƒ“ƒXƒgƒ‰ƒNƒ^‚Å�¶�¬‚µ‚Ă܂·�B
ƒvƒ�ƒpƒeƒB‚Íoverride‚Å�Ä’è‹`‚µ‚Ä‚¢‚Ü‚·�B
‘O•҂ňê��‚ÉŽg‚Á‚Ä—~‚µ‚¢ƒCƒ“ƒXƒ^ƒ“ƒX‚Ì�¶�¬•û–@‚ð—pˆÓ‚·‚邯‚ ‚Á‚½‚̂ł·‚ª�A
‚±‚±‚ł͊ù‚É�¶�¬‚µ‚Ă܂·�B�B‚±‚̃vƒ�ƒpƒeƒB‚ªˆê��‚ÉŽg‚Á‚Ä—~‚µ‚¢ƒCƒ“ƒXƒ^ƒ“ƒX‚ƂȂè‚Ü‚·�B
ƒfƒUƒCƒ“ƒpƒ^�[ƒ“‚̃Tƒ“ƒvƒ‹‚Ȃ̂ŎÀ�Ûƒf�[ƒ^‚ð‘€�ì‚·‚é�ˆ—�‚ÍŽÀ‘•‚µ‚Ă܂¹‚ñ�B

DBMSAccess.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;

namespace Cancer.ADO
{
    public class DBMSAccess : DataBase
    {
        // ƒ�ƒ“ƒo•Ï�”
        private OleDbConnection _connection = null;
        private OleDbCommand _command = null;

        // ƒRƒ“ƒXƒgƒ‰ƒNƒ^
        public DBMSAccess()
        {
            _connection = new OleDbConnection();
            _command = new OleDbCommand();
        }

        // ƒvƒ�ƒpƒeƒB
        public override DbConnection Connection
        {
            get
            {
                return _connection;
            }
        }
        public override DbCommand Command
        {
            get
            {
                return _command;
            }
        }
    }
}

“¯‚¶‚悤‚ÉSQLServer‚âPostgreSQL‚à�ì�¬‚µ‚Ü‚·�B

DBSQLServer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;

namespace Cancer.ADO
{
    public class DBSQLServer : DataBase
    {
        private SqlConnection _connection = null;
        private SqlCommand _command = null;

        public DBSQLServer()
        {
            _connection = new SqlConnection();
            _command = new SqlCommand();
        }
        public override DbConnection Connection
        {
            get
            {
                return _connection;
            }
        }
        public override DbCommand Command
        {
            get
            {
                return _command;
            }
        }
    }
}

DBPostgreSQL.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using Npgsql;

namespace Cancer.ADO
{
    public class DBPostgreSQL : DataBase
    {
        // ƒ�ƒ“ƒo•Ï�”
        private NpgsqlConnection _connection = null;
        private NpgsqlCommand _command = null;

        // ƒRƒ“ƒXƒgƒ‰ƒNƒ^
        public DBPostgreSQL()
        {
            _connection = new NpgsqlConnection();
            _command = new NpgsqlCommand();
        }

        // ƒvƒ�ƒpƒeƒB
        public override DbConnection Connection
        {
            get
            {
                return _connection;
            }
        }
        public override DbCommand Command
        {
            get
            {
                return _command;
            }
        }

        // ƒ�ƒ\ƒbƒh
        public override void Fill(DataSet ds)
        {
            using (NpgsqlDataAdapter adapter = new NpgsqlDataAdapter())
            {
                _command.Connection = _connection;
                adapter.SelectCommand = _command;
                adapter.Fill(ds);
            }
        }
        public override void UpDate(DataSet ds)
        {
            using (NpgsqlDataAdapter adapter = new NpgsqlDataAdapter())
            {
                _command.Connection = _connection;
                adapter.Update(ds);
            }
        }

    }
}

ŽŸ‚ÍFactoryƒNƒ‰ƒX‚ÌŽÀ‘•‚ƂȂè‚Ü‚·�B
FactoryƒNƒ‰ƒX‚ð�¶�¬‚µ‚ă�ƒ\ƒbƒh‚ðŽÀ�s‚Æ‚¢‚¤‚͎̂èŠÔ‚Ȃ̂Å
�ÓIƒ�ƒ\ƒbƒh‚É‚µ‚Ă܂·�B‚Ü‚½ˆø�”‚É•¶Žš—ñ‚ð—p‚¢‚邯“ü—̓~ƒX‚ª”­�¶‚·‚é‚Ì‚Å
—ñ‹“’l‚ð�Ý’è‚·‚邿‚¤‚É‚µ‚Ă܂·�B

DBFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
using System.Collections.Generic;
using System.Text;

namespace Cancer.ADO
{
    public class DBFactory
    {
        // —ñ‹“‘Ì
        public enum DBType
        {
            MSAccess,
            SQLServer,
            PostgreSQL,
        }

        // ƒ�ƒ\ƒbƒh
        public static DataBase DataBaseFactory(DBType type)
        {
            switch (type)
            {
                case DBType.MSAccess:
                    return new DBMSAccess();

                case DBType.SQLServer:
                    return new DBSQLServer();

                case DBType.PostgreSQL:
                    return new DBPostgreSQL();
            }
            return new DataBase();
        }
    }
}

‚Å‚Í�AŽÀ�Û‚ÉŽg‚Á‚Ă݂܂·�B
�Ú‘±‚·‚éDB‚ÍPostgreSQL‚Æ‚µ‚Ü‚·�B

ƒeƒXƒgƒR�[ƒh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private void button1_Click(object sender, EventArgs e)
{
    // PosgreSQL�ê—p‚̃Nƒ‰ƒX‚̃Cƒ“ƒXƒ^ƒ“ƒX‚ð�ì�¬‚µ‚Ü‚·�B
    // ‚±‚±‚Å‚ÍDBPostgreSQLƒNƒ‰ƒX‚ƂȂè‚Ü‚·�B
    DataBase db = DBFactory.DataBaseFactory(DBFactory.DBType.PostgreSQL);

    // DBPostgreSQLƒNƒ‰ƒX‚̃Cƒ“ƒXƒ^ƒ“ƒX‚ð�ì�¬‚µ‚½Žž“_‚Å
    // ŠÖ˜AƒNƒ‰ƒX‚ÌDbConnection‚ÍNpgsqlConnection‚Å�¶�¬‚µ‚Ü‚·�B
    db.Connection.ConnectionString = "Server=localhost;Port=5432;User Id=[ƒ†�[ƒU–¼];Password=[ƒpƒXƒ��[ƒh];Database=[ƒf�[ƒ^ƒx�[ƒX–¼];";

    // �Ú‘±‚µ‚Ü‚·�B
    db.Open();

    // ƒf�[ƒ^‚ðŽæ“¾‚µ‚Ü‚·�B
    DataSet ds = new DataSet();
    db.Command.CommandText = "SELECT * FROM m_hyouhin";
    db.Fill(ds);

    // ‰ð�œ‚µ‚Ü‚·�B
    db.Close();
}

db.Connection.ConnectionString‚Ì[ƒ†�[ƒU–¼][ƒpƒXƒ��[ƒh][ƒf�[ƒ^ƒx�[ƒX]‚Í“K“–‚È’l‚ð‚¢‚ê‚Ä‚­‚¾‚³‚¢�B
DBPostgreSQL‚ÍNpgsqlƒvƒ�ƒoƒCƒ_�[‚ð—˜—p‚µ‚Ä‚¢‚Ü‚·�B
Npgsql‚̓lƒbƒg‚©‚çƒ_ƒEƒ“ƒ��[ƒh‚µ‚Ä‚­‚¾‚³‚¢�B
http://pgfoundry.org/frs/?group_id=1000140



Copyright (C) 2011 ”L‚Ì‹C‚܂܂ÈC#“ú‹L