親フォルダ
001.using System;
002.using System.Collections.Generic;
003.using System.ComponentModel;
004.using System.Data;
005.using System.Data.Odbc;
006.using System.Drawing;
007.using System.Linq;
008.using System.Text;
009.using System.Threading.Tasks;
010.using System.Windows.Forms;
011. 
012.namespace BuildField
013.{
014.    public partial class Form1 : Form
015.    {
016.        // 単独
017.        //private TextBox textBox1;
018.        // 配列
019.        private TextBox[] textBoxArray;
020.        private Label[] labelArray;
021.        private string tableName = "社員マスタ";
022.        private string keyName = "";
023. 
024.        public Form1()
025.        {
026.            InitializeComponent();
027.            controlInit();
028.        }
029. 
030.        private void controlInit()
031.        {
032.            // 接続文字列を作成するオブジェクト
033.            OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder();
034. 
035.            // 接続文字列を作成する為の情報を渡す
036.            builder.Driver = "MySQL ODBC 5.3 Unicode Driver";
037.            builder.Add("SERVER", "localhost");
038.            builder.Add("DATABASE", "lightbox");
039.            builder.Add("UID", "root");
040.            builder.Add("PWD", "");
041. 
042.            // 接続用のオブジェクト
043.            OdbcConnection myCon = new OdbcConnection();
044. 
045.            // 接続用のオブジェクトに接続文字列を渡す
046.            myCon.ConnectionString = builder.ConnectionString;
047. 
048.            // 実行後にレコードセットを取得する為のオブジェクトを作成
049.            OdbcDataReader myReader;
050. 
051.            try
052.            {
053.                // DB の接続
054.                myCon.Open();
055. 
056.                string myQuery = $"SELECT * from {tableName}";
057. 
058.                // SELECT 実行用のオブジェクトを作成
059.                OdbcCommand myCommand = new OdbcCommand();
060.                // 実行する為に必要な情報をセット
061.                myCommand.CommandText = myQuery;
062.                myCommand.Connection = myCon;
063. 
064.                // ここで SELECT を実行してその結果をオブジェクトに格納する
065.                myReader = myCommand.ExecuteReader();
066. 
067.            }
068.            catch (Exception ex)
069.            {
070.                return;
071.            }
072. 
073.            int fieldCount = myReader.FieldCount;
074. 
075.            textBoxArray = new TextBox[fieldCount];
076.            labelArray = new Label[fieldCount];
077. 
078.            for (int i = 0; i < fieldCount; i++) {
079. 
080.                // インスタンス作成
081.                textBoxArray[i] = new TextBox();
082.                // フォント設定
083.                textBoxArray[i].Font = new Font("MS UI Gothic", 12F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(128)));
084.                // 位置設定
085.                textBoxArray[i].Location = new Point(208, 42 + i * 35);
086.                // 名称設定
087.                textBoxArray[i].Name = "textBox"+(i+1);
088.                // 幅と高さの設定
089.                textBoxArray[i].Size = new Size(208, 23);
090.                // TAB の順序
091.                textBoxArray[i].TabIndex = i;
092. 
093.                Controls.Add(textBoxArray[i]);
094. 
095. 
096.                labelArray[i] = new Label();
097.                labelArray[i].AutoSize = true;
098.                labelArray[i].Location = new Point(100, 48 + i * 35);
099.                labelArray[i].Name = "label" + (i+1);
100.                labelArray[i].Size = new Size(40, 12);
101.                labelArray[i].TabIndex = 0;
102.                labelArray[i].Text = myReader.GetName(i);
103. 
104.                Controls.Add(labelArray[i]);
105. 
106.            }
107. 
108.            // 先頭の列名( 主キー )
109.            keyName = labelArray[0].Text;
110. 
111.            myReader.Close();
112.            myCon.Close();
113.            myCon.Dispose();
114. 
115.        }
116. 
117.        private void button1_Click(object sender, EventArgs e)
118.        {
119. 
120.            // 接続文字列を作成するオブジェクト
121.            OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder();
122. 
123.            // 接続文字列を作成する為の情報を渡す
124.            builder.Driver = "MySQL ODBC 5.3 Unicode Driver";
125.            builder.Add("SERVER", "localhost");
126.            builder.Add("DATABASE", "lightbox");
127.            builder.Add("UID", "root");
128.            builder.Add("PWD", "");
129. 
130.            // 接続用のオブジェクト
131.            OdbcConnection myCon = new OdbcConnection();
132. 
133.            // 接続用のオブジェクトに接続文字列を渡す
134.            myCon.ConnectionString = builder.ConnectionString;
135. 
136.            // 実行後にレコードセットを取得する為のオブジェクトを作成
137.            OdbcDataReader myReader;
138. 
139.            try
140.            {
141.                // DB の接続
142.                myCon.Open();
143. 
144.                string myQuery = $"SELECT * from {tableName} where {keyName} = '{textBoxArray[0].Text}'";
145. 
146.                // SELECT 実行用のオブジェクトを作成
147.                OdbcCommand myCommand = new OdbcCommand();
148.                // 実行する為に必要な情報をセット
149.                myCommand.CommandText = myQuery;
150.                myCommand.Connection = myCon;
151. 
152.                // ここで SELECT を実行してその結果をオブジェクトに格納する
153.                myReader = myCommand.ExecuteReader();
154. 
155.            }
156.            catch (Exception ex)
157.            {
158.                return;
159.            }
160. 
161.            if (myReader.Read())
162.            {
163.                for (int i = 1; i < myReader.FieldCount; i++)
164.                {
165.                    if (myReader.IsDBNull(i))
166.                    {
167.                        textBoxArray[i].Text = "";
168.                    }
169.                    else
170.                    {
171.                        textBoxArray[i].Text = myReader.GetValue(i).ToString();
172.                    }
173.                }
174.            }
175. 
176.            myReader.Close();
177.            myCon.Close();
178.            myCon.Dispose();
179. 
180.        }
181.    }
182.}