using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.Odbc; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace BuildField { public partial class Form1 : Form { // 単独 //private TextBox textBox1; // 配列 private TextBox[] textBoxArray; private Label[] labelArray; public Form1() { InitializeComponent(); controlInit(); } private void controlInit() { // 接続文字列を作成するオブジェクト OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder(); // 接続文字列を作成する為の情報を渡す builder.Driver = "MySQL ODBC 5.3 Unicode Driver"; builder.Add("SERVER", "localhost"); builder.Add("DATABASE", "lightbox"); builder.Add("UID", "root"); builder.Add("PWD", ""); // 接続用のオブジェクト OdbcConnection myCon = new OdbcConnection(); // 接続用のオブジェクトに接続文字列を渡す myCon.ConnectionString = builder.ConnectionString; // 実行後にレコードセットを取得する為のオブジェクトを作成 OdbcDataReader myReader; try { // DB の接続 myCon.Open(); string myQuery = "SELECT * from 得意先マスタ"; // SELECT 実行用のオブジェクトを作成 OdbcCommand myCommand = new OdbcCommand(); // 実行する為に必要な情報をセット myCommand.CommandText = myQuery; myCommand.Connection = myCon; // ここで SELECT を実行してその結果をオブジェクトに格納する myReader = myCommand.ExecuteReader(); } catch (Exception ex) { return; } int fieldCount = myReader.FieldCount; textBoxArray = new TextBox[fieldCount]; labelArray = new Label[fieldCount]; for (int i = 0; i < fieldCount; i++) { // インスタンス作成 textBoxArray[i] = new TextBox(); // フォント設定 textBoxArray[i].Font = new Font("MS UI Gothic", 12F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(128))); // 位置設定 textBoxArray[i].Location = new Point(208, 42 + i * 35); // 名称設定 textBoxArray[i].Name = "textBox"+(i+1); // 幅と高さの設定 textBoxArray[i].Size = new Size(208, 23); // TAB の順序 textBoxArray[i].TabIndex = i; Controls.Add(textBoxArray[i]); labelArray[i] = new Label(); labelArray[i].AutoSize = true; labelArray[i].Location = new Point(100, 48 + i * 35); labelArray[i].Name = "label" + (i+1); labelArray[i].Size = new Size(40, 12); labelArray[i].TabIndex = 0; labelArray[i].Text = myReader.GetName(i); Controls.Add(labelArray[i]); } myReader.Close(); myCon.Close(); myCon.Dispose(); } private void button1_Click(object sender, EventArgs e) { string 社員コード = textBoxArray[0].Text; MessageBox.Show(社員コード); // 接続文字列を作成するオブジェクト OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder(); // 接続文字列を作成する為の情報を渡す builder.Driver = "MySQL ODBC 5.3 Unicode Driver"; builder.Add("SERVER", "localhost"); builder.Add("DATABASE", "lightbox"); builder.Add("UID", "root"); builder.Add("PWD", ""); // 接続用のオブジェクト OdbcConnection myCon = new OdbcConnection(); // 接続用のオブジェクトに接続文字列を渡す myCon.ConnectionString = builder.ConnectionString; // 実行後にレコードセットを取得する為のオブジェクトを作成 OdbcDataReader myReader; try { // DB の接続 myCon.Open(); string myQuery = $"SELECT * from 得意先マスタ where 得意先コード = {社員コード}"; // SELECT 実行用のオブジェクトを作成 OdbcCommand myCommand = new OdbcCommand(); // 実行する為に必要な情報をセット myCommand.CommandText = myQuery; myCommand.Connection = myCon; // ここで SELECT を実行してその結果をオブジェクトに格納する myReader = myCommand.ExecuteReader(); } catch (Exception ex) { return; } myReader.Read(); textBoxArray[1].Text = myReader.GetValue(1).ToString(); myReader.Close(); myCon.Close(); myCon.Dispose(); } } }