001.
using
System;
002.
using
System.Data.Odbc;
003.
using
System.Diagnostics;
004.
using
System.Windows.Forms;
005.
006.
namespace
DBAccess
007.
{
008.
class
Program
009.
{
010.
[STAThread]
011.
static
void
Main(
string
[] args)
012.
{
013.
014.
OdbcConnection myCon;
015.
OdbcCommand myCommand;
016.
OdbcDataReader myReader;
017.
018.
019.
OdbcConnectionStringBuilder builder =
new
OdbcConnectionStringBuilder();
020.
021.
022.
023.
024.
025.
builder.Driver =
"MySQL ODBC 8.0 Unicode Driver"
;
026.
027.
028.
029.
builder.Add(
"SERVER"
,
"localhost"
);
030.
builder.Add(
"DATABASE"
,
"lightbox"
);
031.
builder.Add(
"UID"
,
"root"
);
032.
builder.Add(
"PWD"
,
""
);
033.
034.
035.
Console.WriteLine(builder.ConnectionString);
036.
037.
038.
OpenFileDialog ofd =
new
OpenFileDialog();
039.
040.
041.
042.
043.
ofd.Filter =
"AccessとExcel|*.mdb;*.accdb;*.xls;*.xlsx|すべてのファイル(*.*)|*.*"
;
044.
ofd.FilterIndex = 1;
045.
ofd.Title =
"データベースを選択してください"
;
046.
ofd.RestoreDirectory =
true
;
047.
048.
if
(ofd.ShowDialog() == DialogResult.OK)
049.
{
050.
051.
builder.Clear();
052.
053.
if
(ofd.FileName.ToLower().IndexOf(
".xl"
) != -1 )
054.
{
055.
056.
builder.Driver =
"Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)"
;
057.
}
058.
else
059.
{
060.
builder.Driver =
"Microsoft Access Driver (*.mdb, *.accdb)"
;
061.
}
062.
063.
064.
builder.Add(
"dbq"
, ofd.FileName);
065.
}
066.
067.
068.
069.
070.
071.
072.
073.
using
(myCon =
new
OdbcConnection())
074.
{
075.
076.
077.
myCon.ConnectionString = builder.ConnectionString;
078.
079.
080.
try
081.
{
082.
083.
myCon.Open();
084.
}
085.
catch
(OdbcException ex)
086.
{
087.
088.
Console.WriteLine($
"Console : {ex.Message}"
);
089.
Debug.WriteLine($
"Debug : {ex.Message}"
);
090.
}
091.
092.
093.
if
(myCon.State != System.Data.ConnectionState.Open)
094.
{
095.
096.
Console.ReadLine();
097.
return
;
098.
}
099.
100.
101.
102.
103.
104.
string
myQuery =
"select * from 社員マスタ"
;
105.
106.
using
(myCommand =
new
OdbcCommand())
107.
{
108.
109.
myCommand.CommandText = myQuery;
110.
myCommand.Connection = myCon;
111.
112.
113.
114.
115.
116.
using
(myReader = myCommand.ExecuteReader())
117.
{
118.
119.
Console.WriteLine($
"定義されている列の数 : {myReader.FieldCount}"
);
120.
121.
122.
123.
124.
for
(
int
i = 0; i < myReader.FieldCount; i++)
125.
{
126.
if
(i != 0)
127.
{
128.
Console.Write(
","
);
129.
}
130.
Console.Write(myReader.GetName(i));
131.
}
132.
133.
Console.WriteLine();
134.
135.
136.
137.
138.
for
(
int
i = 0; i < myReader.FieldCount; i++)
139.
{
140.
if
(i != 0)
141.
{
142.
Console.Write(
","
);
143.
}
144.
Console.Write(myReader.GetDataTypeName(i));
145.
}
146.
147.
Console.WriteLine(
"\n"
);
148.
149.
150.
151.
152.
153.
while
(myReader.Read())
154.
{
155.
156.
Console.Write($
"{GetValue(myReader, "
社員コード
")},"
);
157.
Console.Write($
"{GetValue(myReader, "
氏名
")},"
);
158.
Console.Write($
"{GetValue(myReader, "
フリガナ
")},"
);
159.
160.
Console.Write($
"{GetValue(myReader, "
給与
")},"
);
161.
Console.Write($
"{GetValue(myReader, "
手当
")},"
);
162.
163.
Console.Write($
"{GetValue(myReader, "
作成日
").Substring(0, 10)},"
);
164.
Console.Write($
"{GetValue(myReader, "
更新日
")},"
);
165.
Console.Write(GetValue(myReader,
"生年月日"
));
166.
167.
168.
Console.WriteLine();
169.
170.
}
171.
172.
myReader.Close();
173.
174.
}
175.
176.
177.
}
178.
179.
180.
myCon.Close();
181.
}
182.
183.
184.
Console.WriteLine(
"\n処理が終了しました"
);
185.
186.
187.
Console.ReadLine();
188.
}
189.
190.
191.
192.
193.
static
string
GetValue(OdbcDataReader myReader,
string
strName)
194.
{
195.
196.
string
ret =
""
;
197.
int
fld = 0;
198.
199.
200.
fld = myReader.GetOrdinal(strName);
201.
202.
if
(myReader.IsDBNull(fld))
203.
{
204.
ret =
""
;
205.
}
206.
else
207.
{
208.
209.
ret = myReader.GetValue(fld).ToString();
210.
}
211.
212.
213.
return
ret;
214.
215.
}
216.
}
217.
}