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.
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.
052.
myCon.Open();
053.
054.
string
myQuery =
"SELECT * from 得意先マスタ"
;
055.
056.
057.
OdbcCommand myCommand =
new
OdbcCommand();
058.
059.
myCommand.CommandText = myQuery;
060.
myCommand.Connection = myCon;
061.
062.
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.
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.
140.
myCon.Open();
141.
142.
string
myQuery = $
"SELECT * from 得意先マスタ where 得意先コード = {社員コード}"
;
143.
144.
145.
OdbcCommand myCommand =
new
OdbcCommand();
146.
147.
myCommand.CommandText = myQuery;
148.
myCommand.Connection = myCon;
149.
150.
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.
}