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